我有这个功能应该给我cURL中的访问令牌
curl -v https://api.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "client_id:secret" \
-d "grant_type=client_credentials"
源:https://developer.paypal.com/docs/api/overview/#get-an-access-token
但是当我尝试在谷歌应用脚本中运行该功能(应该是相同的)时,错误总是:“无效令牌”。我做错了什么?
function lol(){
var request = UrlFetchApp.fetch("https://api.sandbox.paypal.com/v1/oauth2/token", {
"Accept": "application/json",
"Accept-Language": "en_US",
"CLIENT_ID":"SECRET",
"grant_type":"application/x-www-form-urlencoded"
})
Logger.log(request.getContentText());
}
CLIENT_ID& SECRET是个人信息并从我的PayPal账户中复制。
答案 0 :(得分:1)
这次修改怎么样?
Content-Type
为application/x-www-form-urlencoded
。-u
选项是基本授权。-H
是标题。grant_type
为client_credentials
。反映上述观点的剧本如下:
function myFunction(){
var client_id = "client_id"; // Please input your client_id
var secret = "secret"; // Please input your client secret
var options = {
method: "post",
headers : {
"Authorization" : " Basic " + Utilities.base64Encode(client_id + ":" + secret),
"Accept": "application/json",
"Accept-Language": "en_US"
},
payload: {"grant_type": "client_credentials"}
};
var request = UrlFetchApp.fetch("https://api.sandbox.paypal.com/v1/oauth2/token", options);
Logger.log(request.getContentText())
}
如果这对你没用,我很抱歉。