Python需求

2024-04-26 02:52:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用URL编码的标头发出请求。当我运行时,由于某种原因,我得到了错误500。如果代码正常工作,我们应该找回oauth\u令牌和oauth\u令牌密钥。你知道吗

遵循这些说明:http://developer.trademe.co.nz/api-overview/authentication/example-oauth-flow/

import hashlib
import hmac
import base64
import time
from rauth.service import OAuth1Service
import oauth
import rauth
import requests
import oauth2 as oauth1
from urllib.parse import parse_qs
import urllib


nonce = oauth1.generate_nonce()
time = int(time.time())
consumer_key = "5C82CC6BC7C6472154FBC9CAB24A29A2"

New_base_string ="POST&https%3A%2F%2Fsecure.tmsandbox.co.nz%2FOauth%2FRequestToken&oauth_callback%3Dhttp%253A%252F%252Fwww.website-tm-access.co.nz%252Ftrademe-callback%26oauth_consumer_key%" + str(consumer_key) +"3DC74CD73FDBE37D29BDD21BAB54BC70E422%26oauth_nonce%3" + str(nonce) + "%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3" + str(time) + "%26oauth_version%3D1.0%26scope%3DMyTradeMeRead%252CMyTradeMeWrite"
New_base_string= New_base_string.encode()

KEY = b"5C82CC6BC7C6472154FBC9CAB24A29A2&"

digest = hmac.new(KEY, New_base_string, hashlib.sha1).digest()

signature = (base64.b64encode(digest))

print(base64.b64encode(digest))


url = 'https://secure.tmsandbox.co.nz/Oauth/RequestToken?scope=MyTradeMeRead,MyTradeMeWrite '


headers = {'oauth_callback': "http://www.website-tm-access.co.nz%2Ftrademe-callback", 
          'oauth_consumer_key' : "C74CD73FDBE37D29BDD21BAB54BC70E422" ,
          'oauth_version': "1.0",
          'oauth_timestamp': time, #int(time.time()),
          'oauth_nonce' : nonce,
          'oauth_signature_method' : "HMAC-SHA1",
          'oauth_signature' : signature
          }


authorization = '5C82CC6BC7C6472154FBC9CAB24A29A2 ' + ', '.join([key + '="' + urllib.parse.quote_plus(str(value)) + '"' for key, value in headers.items()])
http_headers = {'Authorization': authorization}
print(authorization)



r = requests.get(url, headers=http_headers)

*错误:错误500


Tags: keyimporthttpnewbasestringtimeconsumer