【问题标题】:get access_token for a Twitter application which does not have a callback URL using Python使用 Python 获取没有回调 URL 的 Twitter 应用程序的 access_token
【发布时间】:2013-04-06 23:30:09
【问题描述】:

我创建了一个应用程序,其“回调 URL”字段为空白。我想以编程方式使用 oauth 端点 (https://dev.twitter.com/docs/api/1/post/oauth/access_token) 的 access_token 函数并获取 oauth_token 和 oauth_token_secret。

我可以通过我的浏览器执行此操作,我转到我的应用程序,然后通过单击按钮生成访问令牌。但是,我想通过 python 程序自动执行此操作。 https://dev.twitter.com/docs/api/1/post/oauth/access_token 声明“回调 URL”是强制性的。将其留空不起作用,因为会导致“401 Unauthorized Failed to validate oauth signature and token”响应。

我不想使用任何 twitter API 包装器或 oauth 库。

我已经尝试了以下代码 -

    import httplib
    import uuid
    from time import time

    host = 'api.twitter.com'
    url = "/oauth/request_token"
    req = httplib.HTTPSConnection(host)
    req.putrequest("GET", url)
    req.putheader("Host", host)

    uid = uuid.uuid4()
    req.putheader("oauth_consumer_key" , consumer_key)
    req.putheader("OAuth oauth_nonce" , uid.hex)
    req.putheader("oauth_signature_method" , "HMAC-SHA1")
    req.putheader("oauth_timestamp" , str(time.time()))
    req.putheader("oauth_version" , "1.0")
    req.putheader("oauth_callback" , "")
    req.putheader("oauth_signature" , consumer_secret)

    req.endheaders()        
    resp = req.getresponse()
    data = resp.read()
    print resp.status, resp.reason
    print data

这会返回“401 Unauthorized. Failed to validate oauth signature and token”

如何访问我创建的应用程序的 oauth_token 和 oauth_token_secret?

【问题讨论】:

    标签: python oauth twitter


    【解决方案1】:

    查看thread - 如果您想实现整个 oAuth2 握手。 否则,我将使用 tweepy,它会为您处理这些问题。

    【讨论】:

      猜你喜欢
      • 2012-05-26
      • 1970-01-01
      • 2012-08-25
      • 2012-06-07
      • 1970-01-01
      • 2012-03-07
      • 2013-07-03
      • 2013-06-06
      • 1970-01-01
      相关资源
      最近更新 更多