编辑:我修复了我原来的问题,并在我的回答中显示了一个metor示例.
尝试在Meteor中获取PayPal API应用程序的令牌时,我收到错误500:
token = EJSON.stringify(Meteor.http.call "POST", "https://api.sandbox.paypal.com/v1/oauth2/token",
headers:
"Accept": "application/json"
"Accept-Language": "en_US"
auth: "user:pass"
params:
"grant_type":"client_credentials"
);
console.log("Token: "+token);
Run Code Online (Sandbox Code Playgroud)
输出此代码:
Token: {"statusCode":500,"headers":{"server":"Apache-Coyote/1.1","date":"Fri, 15 Mar 2013 05:04:43 GMT","content-length":"0","connection":"close"},"data":null,"error":{}}
Run Code Online (Sandbox Code Playgroud)
显然,PayPal正在向我返回错误500.我无法弄清楚可能导致这种情况的原因.当然Auth是实际数据,而不是user:pass.
为什么我得到错误500?
编辑:编译的Javascript var令牌;
token = EJSON.stringify(Meteor.http.call("POST", "https://api.sandbox.paypal.com/v1/oauth2/token", {
headers: {
"Accept": "application/json",
"Accept-Language": "en_US"
},
auth: "user:pass",
params: {
"grant_type": "client_credentials"
}
}));
console.log("Token: " + token);
Run Code Online (Sandbox Code Playgroud)
这是使用meteor进行paypal API调用的示例实现.
在程序启动时,获取令牌.始终更换clientid
并clientsecret
与您自己的.
token = EJSON.parse(Meteor.http.post("https://api.sandbox.paypal.com/v1/oauth2/token",
headers:
"Accept": "application/json"
"Accept-Language":"en_US"
auth: "clientid:clientsecret"
params:
"grant_type":"client_credentials"
#encoding: "base64"
).content).access_token;
Run Code Online (Sandbox Code Playgroud)
现在,创建一个付款,显示在Meteor.methods
方法中(并返回客户端要访问的URL):
buySingleItem: () ->
console.log "Starting new payment, user id: "+Meteor.userId()
result = Meteor.http.post("https://api.sandbox.paypal.com/v1/payments/payment",
headers:
"Authorization":"Bearer "+token
"Content-Type": "application/json"
data:
{
"intent":"sale"
"redirect_urls":
"return_url":"http://mysite.herokuapp.com/done",
"cancel_url":"http://mysite.herokuapp.com/cancel"
"payer":
"payment_method":"paypal"
"transactions":[
{
"amount":
"total":"3.00",
"currency":"USD"
"description":"My item description."
}
]
}
)
payment = result.data
console.log "PayPal redirect: "+payment.links[1].href
return payment.links[1].href
Run Code Online (Sandbox Code Playgroud)
这将在Meteor中创建PayPal结账样式付款.