我终于设法修复了代码,这是完整的版本:
import ast
import signaturegen
headers = {
"X-PAYPAL-SECURITY-USERID": "xxxxxxxxx.xxxx.com",
"X-PAYPAL-SECURITY-PASSWORD": "xxxxxxxx",
"X-PAYPAL-SECURITY-SIGNATURE": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"X-PAYPAL-REQUEST-DATA-FORMAT": "JSON",
"X-PAYPAL-RESPONSE-DATA-FORMAT": "JSON",
"X-PAYPAL-APPLICATION-ID": "APP-80W284485P519543T", #APP ID for sandbox
}
headers = {
"X-PAYPAL-SECURITY-USERID": settings.USERNAME,
"X-PAYPAL-SECURITY-PASSWORD": settings.PASSWORD,
"X-PAYPAL-SECURITY-SIGNATURE": settings.SIGNATURE,
"X-PAYPAL-REQUEST-DATA-FORMAT": "JSON",
"X-PAYPAL-RESPONSE-DATA-FORMAT": "JSON",
"X-PAYPAL-APPLICATION-ID": "APP-80W284485P519543T"
}
data = {"scope":"TRANSACTION_SEARCH", "callback":"http://www.example.com/success.html", "requestEnvelope": {"errorLanguage":"en_US"}}
req = urllib2.Request("https://svcs.sandbox.paypal.com/Permissions/RequestPermissions/", simplejson.dumps(data), headers)
res = ast.literal_eval(urllib2.urlopen(req).read())
token = res['token']
red_url = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_grant-permission&request_token=%s" % token
if red_url:
return HttpResponseRedirect(red_url)
token = "xxxxxxxxxxxxxxxxxx"
verification = "xxxxxxxxxxxxxxxxxx"
headers2 = {
"X-PAYPAL-SECURITY-USERID": "xxxxxxxxxxxx",
"X-PAYPAL-SECURITY-PASSWORD": "xxxxxxxxxxxxxxxx",
"X-PAYPAL-SECURITY-SIGNATURE": "xxxxxxxxxxxxx",
"X-PAYPAL-REQUEST-DATA-FORMAT": "JSON",
"X-PAYPAL-RESPONSE-DATA-FORMAT": "JSON",
"X-PAYPAL-APPLICATION-ID": "APP-80W284485P519543T",
}
url = "https://svcs.sandbox.paypal.com/Permissions/GetAccessToken/"
dat2 = {
"requestEnvelope": {"errorLanguage":"en_US"},
"token": "AAAAAAAYcambja9iJfUw",
"verifier": "iVUJ6c-6ZNk8M6Q9hkC12A"}
req2 = urllib2.Request("https://svcs.sandbox.paypal.com/Permissions/GetAccessToken/", simplejson.dumps(dat2), headers2)
res2 = ast.literal_eval(urllib2.urlopen(req2).read())
(timestamp, signature) = signaturegen.getAuthHeader(apiUser="xxxxxxxxxxxx", apiPass="xxxxxxxxxxxxxxxx", accessTok=res2["token"], secTok=res2["tokenSecret"], httpMethod="POST", scriptURI="https://api-3t.sandbox.paypal.com/nvp")
ultimate = {"X-PAYPAL-AUTHORIZATION": "timestamp="+timestamp+",token="+res2["token"]+",signature="+signature, "SUBJECT": settings.USERNAME}
da = {
"METHOD": "TransactionSearch",
"STARTDATE": "2012-01-01T05:38:48Z",
}
req3 = urllib2.Request("https://api-3t.sandbox.paypal.com/nvp", urllib.urlencode(da), ultimate)
res3 = urllib2.urlopen(req3).read()