Paypal rest api и приложения Script

Я пытаюсь использовать остальные api для PayPal. Как я могу перевести запрос Curl для получения токена с помощью UrlfetchApp приложений 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"

Заранее благодарю

1 ответ

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);
}

licensed under cc by-sa 3.0 with attribution.