Paypal rest api and Apps Script - paypal

I'm trying to use the rest api of paypal. How I can translate the Curl request to get the token using UrlfetchApp of Apps Script?
Request Sample
curl https://api.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp" \
-d "grant_type=client_credentials"
Thank you in advance

function tryPayPal() {
var tokenEndpoint = "https://api.sandbox.paypal.com/v1/oauth2/token";
var head = {
'Authorization':"Basic "+ Utilities.base64Encode("EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp"),
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded'
}
var postPayload = {
"grant_type" : "client_credentials"
}
var params = {
headers: head,
contentType: 'application/x-www-form-urlencoded',
method : "post",
payload : postPayload
}
var request = UrlFetchApp.getRequest(tokenEndpoint, params);
var response = UrlFetchApp.fetch(tokenEndpoint, params);
var result = response.getContentText();
Logger.log(result);
var resultObject = JSON.parse(result);
}

Related

How do I do an graphql axios call to supabase to

I try to do an axios call to graphql in the documentation i see:
curl -X POST https://<PROJECT_REF>.supabase.co/graphql/v1 \
-H 'apiKey: <API_KEY>'\
-H 'Content-Type: application/json' \
--data-raw '
{
"query":"{ accountCollection(first: 3) { edges { node { id } } } }"
}'
I am transforming that to a axios call like that:
var callObject = {
url:https://<PROJECT_REF>.supabase.co/graphql/v1,
method:"POST",
headers:{
"Authorization" : "Bearer " + apiKey,
"apikey": apiKey,
"Content-Type": "application/json"
}
}
var response = await axios(callObject)
Now I tried many things to add the --data-raw payload into the call. But i couldn't find the solution. It always shows me a 404
How can handle that?

The argument type 'Set<String>' can't be assigned to the parameter type 'Map<String, String>'

I'm trying to create an user authentication system using Auth0 in my Flutter app. The Auth0 REST documentation gave an example of cURL but I didn't find any flutter package which does the job of cURL. So, I used http.
Here's the code:
Future<String> getToken(String userId) async {
final response = await http.post(
Uri.parse('https://my-auth0-subdomain.auth0.com/oauth/token'), // I used my real subdomain
body: jsonEncode({
'grant_type=client_credentials',
'client_id=my_project_client_id', // I used my real client id
'client_secret=my_project_client_secret', // I used my real client secret
'audience=https://my-auth0-subdomain.auth0.com/api/v2/' // I used my real subdomain
}),
headers: {
'content-type: application/x-www-form-urlencoded'
},
);
final token = jsonDecode(response.body)["access_token"];
return token;
}
This gives me an error that The argument type 'Set<String>' can't be assigned to the parameter type 'Map<String, String>'. on line 10 (headers: {...}). I can resolve this error by using headers: {'content-type': 'application/x-www-form-urlencoded'},.
But this then gives the error from Auth0 {"error":"access_denied","error_description":"Unauthorized"}. The API is set up properly, because on running
curl --request POST \
--url 'https://my-auth0-subdomain.auth0.com/oauth/token' \
--header "content-type: application/x-www-form-urlencoded" \
--data grant_type=client_credentials \
--data 'client_id=my_project_client_id' \
--data client_secret=my_project_client_secret \
--data 'audience=https://my-auth0-subdomain.auth0.com/api/v2/'
it returns a "access_token", "scope", "expires_in" and "token_type".
Please help. It's very important.
Thanks in advance :)
Try to send data as url encoded using :
Map<String, String> myBody= {
'grant_type' : 'client_credentials',
'client_id' : 'my_project_client_id', // I used my real client id
'client_secret' : 'my_project_client_secret', // I used my real client secret
'audience ' : 'https://my-auth0-subdomain.auth0.com/api/v2/' // I used my real subdomain
};
Future<String> getToken(String userId) async {
final response = await http.post(
Uri.parse('https://my-auth0-subdomain.auth0.com/oauth/token'), // I used my real subdomain
body: myBody,
headers: {
'content-type: application/x-www-form-urlencoded'
},
);
final token = jsonDecode(response.body)["access_token"];
return token;
}

Flutter: MultipartRequest returns 403 Forbidden... CURL works fine

I try the following CURL command:
-X PUT \
-H 'Content-Type: application/octet-stream' \
--upload-file ${file_name} \
${url}
To upload a file into Google Storage (https://storage.googleapis.com/...?signature=...&...). This works just fine.
However, when I try to do it using the following Flutter code - I get a response with Error 403 (Forbidden):
Future<http.Response?> uploadVideo(
{required String uploadURL, required filePath}) async {
try {
ByteData bytes = await rootBundle.load(filePath);
var data =
http.MultipartFile.fromBytes('file', bytes.buffer.asInt64List());
Uri uri = Uri.parse(uploadURL);
var request = http.MultipartRequest(
'PUT',
uri,
);
request.files.add(data);
request.headers.addAll({
HttpHeaders.contentTypeHeader: 'Content-Type: application/octet-stream',
});
http.StreamedResponse response = await request.send();
return await http.Response.fromStream(response);
} on SocketException catch (e) {
showNoInternetError(e);
}
Any idea why this happens? The uploadURL is the same as the one I sent through CURL (very long string, with signature and ampersand signs etc).
Thanks a lot!!! <3

Getting 301 with Flutter OAuth

It's sad to see no concrete examples are available when it comes to using OAuth2 package of Flutter.
I'm a beginner, the example mentioned here is tough for me to crack(I learn mostly through videos), but my job requires me to implement OAuth by today itself. So I'm trying my shot here.
This is what I've:
Login API
curl --location --request POST 'https://domain/api/rest/oauth/token' \
--header 'Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'username=<username_here>' \
--data-urlencode 'password=<md5_of_password_here'
I've been given a sample username and password as well..
This is what I've implemented so far..
void checkAuthentication() async {
String username = 'username';
String password = 'password';
String basicAuth =
'Basic ' + base64Encode(utf8.encode('$username:$password'));
print(basicAuth);
var body = jsonEncode(
<String, dynamic>{
'grant_type': 'password',
'username': 'username',
'password': 'password',
},
);
Response r = await http.post(
Uri.https('domain', 'api/rest/oauth/token'),
headers: <String, String>{
'authorization': basicAuth,
'Content-Type': 'application/x-www-form-urlencoded',
'accept': 'application/json',
},
body: body,
);
print(r.statusCode);
print(r.headers);
print(r.body);
}
I'm getting 301 as result..
What can I do to get 200. Please guide me.

How to specify Flutter/dart HTTP protocol version (HTTP/1.1, --http1.1)

I am using dart:http to make post request but it seems I cannot specify HTTP1.1 version which is required by target api. Ideally I need to re-create following request in dart:
curl -X POST --http1.1 "https://api.dummyapi.com/v1/route" -H "accept: text/plain" -H "Authorization: 000000-000000-000000-000000" -H "Content-Type: application/json-patch+json" -d "{}"
My current version is this:
final url = Uri.parse('https://api.dummyapi.com/v1/route');
final response = await http.post(url, headers: {
'Authorization': '000000-000000-000000-000000',
'accept': 'text/plain',
'Content-Type': 'application/json'
});```
Checked the following code example
Future createUserExample(Map<String,dynamic> data) async {
final http.Response response = await http.post(
'https://api.dummyapi.com/v1/route',
headers: <String, String>{
'Authorization': '000000-000000-000000-000000',
'accept': 'text/plain',
'Content-Type': 'application/json'
},
body: data,
);
return response;