我正在构建一个Python脚本来自动在Twitter上发布状态更新。我仔细阅读并遵循了创建签名的API文档,并请求更新状态。但我在做请求时出错了。你知道吗
HTTP Error 401: Authorization Required
我在Stackoverflow和其他资源中看到了更多类似的问题,但是还没有找到解决这个问题的方法。你知道吗
def sendTweet(self):
url = 'https://api.twitter.com/1.1/statuses/update.json'
message = 'Testing'
oauths = [
['oauth_consumer_key', '1234567890'],
['oauth_nonce', self.generate_nonce()],
['oauth_signature', ''],
['oauth_signature_method', 'HMAC-SHA1'],
['oauth_timestamp', str(int(time.time()))],
['oauth_token', '1234567890'],
['oauth_version', '1.0']
]
signature = self.sign(oauths, message)
oauths[2][1] = signature
count = 0
auth = 'OAuth '
for oauth in oauths:
count = count + 1
if oauth[1]:
auth = auth + self.quote(oauth[0]) + '="' + self.quote(oauth[1]) + '"'
if count < len(oauths):
auth = auth + ', '
headers = {
'Authorization': auth,
'Content-Type': 'application/x-www-form-urlencoded'
}
payload = {'status' : message.encode('utf-8')}
req = urllib2.Request(url, data=urllib.urlencode(payload), headers=headers)
try:
response = urllib2.urlopen(req)
data = response.read()
print data
except (urllib2.HTTPError, urllib2.URLError) as err:
print err
这就是self.sign
函数的作用
def sign(self, oauths, message):
signatureParams = ''
count = 0
values = [
['oauth_consumer_key', oauths[0][1]],
['oauth_nonce', oauths[1][1]],
['oauth_signature_method', oauths[3][1]],
['oauth_timestamp', oauths[4][1]],
['oauth_token', oauths[5][1]],
['oauth_version', oauths[6][1]],
['status', message]
]
customerSecret = '1234567890'
tokenSecret = '1234567890'
for value in oauths:
count = count + 1
signatureParams = signatureParams + self.quote(value[0]) + '=' + self.quote(value[1])
if count < len(oauths):
signatureParams = signatureParams + '&'
signature = 'POST&' + self.quote(self.endPoint) + '&' + self.quote(signatureParams)
signinKey = self.quote(customerSecret) + '&' + self.quote(tokenSecret)
signed = base64.b64encode(self.signRequest(signinKey, signature))
return signed
下面是helper函数。你知道吗
def generate_nonce(self):
return ''.join([str(random.randint(0, 9)) for i in range(8)])
def quote(self, param):
return urllib.quote(param.encode('utf8'), safe='')
def signRequest(self, key, raw):
from hashlib import sha1
import hmac
hashed = hmac.new(key, raw, sha1)
return hashed.digest().encode('base64').rstrip('\n')
这就是我打印auth字符串时的样子。你知道吗
OAuth oauth_consumer_key="1234567890",
oauth_nonce="78261149",
oauth_signature="akc2alpFWWdjbElZV2RCMmpGcDc0V1d1S1B3PQ%3D%3D",
oauth_signature_method="HMAC-SHA1", oauth_timestamp="1540896473",
oauth_token="1234567890",
oauth_version="1.0"
这和我读Twitter文档时应该是一样的。但我还是得到了401授权所需的错误。。我只是看不出这里出了什么问题。你知道吗
密钥的访问级别是Read, write, and direct messages
所有键都被1234567890
替换,但在实际代码中我使用的是真正的键。你知道吗
有人知道我做错了什么吗?提前谢谢!你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐