【问题标题】:OAuth generates expired bearer tokenOAuth 生成过期的不记名令牌
【发布时间】:2014-10-08 19:48:13
【问题描述】:

我一直在关注 Twitter 的 3-legged oauth 设置指南: https://dev.twitter.com/docs/auth/implementing-sign-twitter

第 1 步:获取请求令牌

对于他们的身份验证,第 1 步需要发出一个包含 base64 编码的公钥和密钥的发布请求。

key = "CONSUMER_KEY"
secret = "CONSUMER_SECRET"
auth = base64.encodestring("%s:%s" % (key, secret)).replace("\n", "")

data = {}
data["grant_type"] = "client_credentials"
headers = {}
headers["Authorization"] = "Basic " + auth
headers["Content-Type"] = "application/x-www-form-urlencoded;charset=UTF-8"
headers["Accept-Encoding"] = "gzip"

response = requests.post("https://api.twitter.com/oauth2/token",
                         headers=headers, data=data)

第一个请求返回一个有效的响应代码 200 以及一个访问令牌。响应如下所示: {u'access_token': u'AAAAAAAAAAAAAAAAAAAAAAAAHHHHH... ...vncbi', u'token_type': u'bearer'}

第 2 步:重定向用户

这就是问题所在。根据文档,用户只需重定向到格式如下的授权 url:

https://api.twitter.com/oauth/authenticate?oauth_token=AAAAAAAAAAAAAAAAAAAAAHHHHH... ...vncbi

但是,当我进入此页面时,我收到一条错误消息:

我错过了什么吗?正在生成 access_token 没有问题。我不确定是否显示此消息,因为我在此过程的早期设置错误。我也不确定如何检查 oauth 令牌是否已过期。

【问题讨论】:

    标签: python twitter oauth


    【解决方案1】:

    其实你一直在关注https://dev.twitter.com/docs/api/1.1/post/oauth2/token,这是完全不同的,例如仅用于公共资源,而不是像状态更新这样的私有资源。对于三步一结帐https://gist.github.com/ib-lundgren/4487236 或更好http://twython.readthedocs.org/en/latest/

    如果您只想访问用户时间线等公共资源,您可以通过以下代码进行。

    # OBS: If you want to look at per user details and make status updates
    # you want the OAuth1 version. This is only for publicly available
    # resources such as user timelines.
    
    from requests_oauthlib import OAuth2Session
    from oauthlib.oauth2 import BackendApplicationClient
    
    # Credentials you get from registering a new application
    client_id = '<the id you get from github>'
    client_secret = '<the secret you get from github>'
    
    # TODO remove
    
    client_id = 'VVq5UniipB5nXFAqtTA'
    client_secret = 'PlaHnaSDbeY4eYkv8XiqxS1nzGWyKoq5WYSNjdeaw'
    
    client_id = 'I1Xi7fOeYnA9jabyvGUaZxY20'
    client_secret = 'k5PZpINooRpjAfQccGwLUr2ZMEtRJtoX8cKaooHjKewWupxRBG'
    
    token_url = 'https://api.twitter.com/oauth2/token'
    
    client = BackendApplicationClient(client_id)
    twitter = OAuth2Session(client_id, client=client)
    
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
    }
    twitter.fetch_token(token_url, headers=headers, auth=(client_id, client_secret))
    
    # Only public resources available to this application-only clients.
    r = twitter.get('https://api.twitter.com/1.1/statuses/user_timeline.json?count=100&screen_name=twitterapi')
    print r.content
    

    确保您使用的是 github 版本的库

    pip install git+https://github.com/idan/oauthlib.git
    pip install git+https://github.com/requests/requests-oauthlib.git
    

    【讨论】:

      猜你喜欢
      • 2014-04-14
      • 2016-03-10
      • 2011-07-24
      • 2020-11-08
      • 2021-07-23
      • 2023-03-05
      • 2015-11-30
      • 1970-01-01
      • 2015-08-01
      相关资源
      最近更新 更多