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?
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With