根据https://developer.paypal.com/webapps/developer/docs/classic/permissions-service/ht_permissions-invoice/使用Paypal的权限API获取访问令牌(令牌:xxxxxx和tokenSecret:xxxxxxx)授予第三方权限后,我没有成功跟进代表第三方的其他API调用,因为不清楚应该如何生成X-PAYPAL-AUTHORIZATION标头。
基于另一个SO问题编写的示例node.js代码:Generating Authentication Header,我已经完成了一个Python端口
from hashlib import sha1
import hmac
from base64 import b64encode
from urllib import urlencode
from datetime import datetime
def paypal_urlencode(s):
encode = lambda x: x if x in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_+" else '%%%x' % ord(x)
return ''.join(map(encode, s.replace(' ','+')))
def paypal_authorisation(token, ep, consumer, method="POST", sandbox=True):
params = dict(
oauth_consumer_key=consumer['key'],
oauth_version='1.0',
oauth_signature_method="HMAC-SHA1",
oauth_token=token['key'],
oauth_timestamp=datetime.now().strftime('%s')
)
key = "&".join((paypal_urlencode(consumer['secret']), paypal_urlencode(token['secret'])))
sig_base = "&".join((method, paypal_urlencode(ep), paypal_urlencode("oauth_consumer_key=%(oauth_consumer_key)s&oauth_signature_method=%(oauth_signature_method)s&oauth_timestamp=%(oauth_timestamp)s&oauth_token=%(oauth_token)s&oauth_version=%(oauth_version)s" % params)))
h= hmac.new(key.encode('ascii'), sig_base.encode('ascii'), sha1)
signature=b64encode(h.digest())
return "token=%s,signature=%s,timestamp=%s" % (token['key'], signature, params['oauth_timestamp'])
但是,我收到验证错误代码10002.感谢调试中的任何建议。请注意,已成功接收访问令牌。
答案 0 :(得分:0)
来自PayPal开发者文档:
PayPal API Error Codes
错误代码1000:
此错误可能是由错误的API用户名,错误的API密码或无效的API签名引起的。确保所有这三个值都正确。为了您的安全,PayPal不会准确报告这三个值中的哪一个可能出错。
确保您使用的是正确的凭据(客户端ID,必须包含客户端密码)。如果您处于沙箱模式,则需要确保您使用的凭据来自业务Pro Sandbox帐户。如果您处于实时模式,请确保您使用的是现金PayPal凭证。
此样本直接来自PayPal开发者网站:
Complete Integration Guide for Rest API
Request Sample
curl 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"
对于Rest API,这里是您应该使用的身份验证和标头:
Authentication and Headers Rest API
客户端凭据位于您在PayPal开发者网站上提交的Rest API应用程序中。
万一你需要它,这里是你的第一个API调用的链接,它包括创建一个Rest API应用程序:
PayPal Rest API Making your first call