Pao*_*177 1 paypal-sandbox google-apps-script
我有这个函数应该给我 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"
Run Code Online (Sandbox Code Playgroud)
来源: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());
}
Run Code Online (Sandbox Code Playgroud)
CLIENT_ID 和 SECRET 是个人的,是从我的 PayPal 帐户复制的。
这个修改怎么样?
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())
}
Run Code Online (Sandbox Code Playgroud)
如果这对您没有用,我很抱歉。