python3 unicode对象_python-3.x – Python 3.4错误“必须在散列之前编码Unicode对象”

使用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值