使用oauth2从Twitter获取数据,但得到的错误为:
Unicode-objects must be encoded before hashing
使用下面的代码,
def oauth_req(url, key, secret, http_method="GET", post_body="",http_headers=None):
consumer = oauth2.Consumer(key=API_KEY, secret=API_SECRET)
token = oauth2.Token(key=key, secret=secret)
client = oauth2.Client(consumer, token)
resp, content = client.request(url, method=http_method, body=post_body, headers=http_headers)
return content
data = oauth_req(url, TOKEN_KEY, TOKEN_SECRET)
还尝试了我传递给def的变量的utf8编码值.
File "", line 1, in
data = oauth_req(url, TOKEN_KEY, TOKEN_SECRET)
File "", line 6, in oauth_req
body=post_body, headers=http_headers)
File "C:\Python35-32\lib\site-packages\oauth2_init_.py", line 673, in request
req.sign_request(self.method, self.consumer, self.token)
File "C:\Python35-32\lib\site-packages\oauth2_init_.py", line 493, in sign_request
self['oauth_body_hash'] = base64.b64encode(sha1(self.body).digest())
TypeError: Unicode-objects must be encoded before hashing