我尝试获取朋友列表,实际上不能使用任何api,比如twitter和OAuth(实际上是在研究任务) 我成功地做了request_token和access_token。但在它之后我尝试得到朋友/ list.json并得到401(32我们无法授权你)
def get_friends(self):
address = "https://api.twitter.com/1.1/friends/list.json?" + self.user_id
params = {
'oauth_token': self.token,
'oauth_timestamp': int(time.time()),
'oauth_nonce': self.get_nonce(),
'oauth_signature_method': 'HMAC-SHA1',
'oauth_version': '1.0',
'oauth_consumer_key': self.consumer_key
}
params['oauth_signature'] = self.get_sign(address, params, 'GET')
http_request = Request(address)
http_request.add_header("Authorization", self.get_header(params))
try:
received = (urlopen(http_request,
bytes('', 'ascii')).read())
except Exception as e:
print(e.read())
def get_sign(self, address, params, method):
sorted_params = sorted(params.items())
params_string = ''
for param in sorted_params:
params_string += '{}={}&'.format(param[0], param[1])
params_string = quote(params_string[:-1], '')
string = method + '&' + quote(address, '') + '&' + params_string
raw = string.encode()
decoder = (self.cons_secret + '&').encode()
hashed = hmac.new(decoder, raw, sha1)
sign = quote(str(b64encode(hashed.digest()))[2:-1])
return sign
答案 0 :(得分:0)
验证您的OAuth凭据,有时即使一切正确,您也会收到“Peer not authenticated”或“Unauthorized access”等错误。这是因为对该api版本的调用将具有端点。