403访问twitter api时出现Forbidden错误

时间:2014-10-15 11:31:55

标签: python twitter oauth

我正在尝试访问twitter api并且这样做我正试图获得一个持有人密钥,正如他们在twitter api文档中提到的那样

这是我到目前为止所写的内容

#!/usr/bin/python3

import oauth2 as oauth
import json
import base64
import urllib.request
from urllib.request import Request
from urllib.request import urlopen


def main():
    try:
        consumer_key = b'b1...H49'
        consumer_secret = b'0w...5H'
        base64_consumer_key_secret = base64.b64encode(consumer_key + b':' + consumer_secret)

        #step 2 : obtain bearer token
        print(base64_consumer_key_secret)
        request = Request('https://api.twitter.com/oauth2/token',b'grant_type=client_credentials')
        request.add_header('Authorization',b'Basic' + base64_consumer_key_secret)
        request.add_header('Content-Type', b'application/x-www-form-urlencoded;charset=UTF-8')
        request.add_header('User-Agent',b'Mozilla/5.0')
        response = urlopen(request)
   except Exception as e:
          print('unable to open url', e)
  else:
       data = json.load(response)
       if data['token_type'] != 'bearer':
          throw('Bad token_type: ' + data['token_type'])
       access_token = data['access_token']
       print ('access_token is ' + access_token)

 if __name__ == "__main__": 
      main()

我收到错误消息403禁止。我知道这个错误代码意味着我以正确的方式构建了请求,但我没有足够的权限。我已经用twitter创建了一个测试应用程序并获得了消费者密钥和秘密。有什么东西丢失或做错了吗?

0 个答案:

没有答案