3

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

4

1 回答 1

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

}
于 2013-05-22T16:01:19.877 回答