代码之家  ›  专栏  ›  技术社区  ›  zohar Seeni Abirami

无法使用access_token从Twitter检索经过身份验证的数据

  •  0
  • zohar Seeni Abirami  · 技术社区  · 12 年前

    我正在尝试在python中创建泛型类,它将完成所有oAuth过程,然后允许从任何支持oAuth的服务(例如Twitter、LinkedIn)中检索数据。

    编辑时间: 我有客户密钥和机密以及访问令牌密钥和机密,当我尝试请求任何资源请求时,我会收到以下错误: {"error":"Could not authenticate with OAuth.","request":"\/1\/statuses\/retweeted_by_me.json }'

    知道为什么吗?

    我的代码是:

    import httplib
    import time
    import oauth as oauth
    
    # settings for the local test consumer
    SERVER = 'api.twitter.com'
    
    RESOURCE_URL = 'https://api.twitter.com/1/statuses/retweeted_by_me.json'
    
    CONSUMER_KEY = 'MY_CUSTOMER_KEY'
    CONSUMER_SECRET = 'MY_CUSTOMER_SECRET'
    ACCESS_TOKEN_KEY = 'MY_ACCESS_TOKEN_KEY'
    ACCESS_TOKEN_SECRET = 'MY_ACCESS_TOKEN_SECRET'
    
    # example client using httplib with headers
    class SimpleOAuthClient(oauth.OAuthClient):
    
        def __init__(self, server):
            self.server = server
            self.connection = httplib.HTTPSConnection(self.server)
    
        def access_resource(self, oauth_request):
            # via post body
            # -> some protected resources
            self.connection.request(oauth_request.http_method, RESOURCE_URL)
            response = self.connection.getresponse()
            return response.read()
    
    def run_example2():
      print '** OAuth Python Library Example **'
      client = SimpleOAuthClient(SERVER, )
      consumer = oauth.OAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET)
      signature_method_hmac_sha1 = oauth.OAuthSignatureMethod_HMAC_SHA1()
      pause()
    
      # access some protected resources
      print '* Access protected resources ...'
      pause()
      token = oauth.OAuthToken('ACCESS_TOKEN_KEY', 'ACCESS_TOKEN_SECRET')
      oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, token=token, http_method='GET', http_url=RESOURCE_URL)
      oauth_request.sign_request(signature_method_hmac_sha1, consumer, token)
      print 'REQUEST (via post body)'
      print 'parameters: %s' % str(oauth_request.parameters)
      pause()
      params = client.access_resource(oauth_request)
      print 'GOT'
      print 'non-oauth parameters: %s' % params
      pause()
    
    
    def pause():
        print ''
        time.sleep(1)
    
    if __name__ == '__main__':
        run_example2()
        print 'Done.'
    
    2 回复  |  直到 12 年前
        1
  •  1
  •   Mark S.    12 年前

    AUTHORIZATION_URL='授权https://api.twitter.com/oauth/authenticate'

    这是用于OAuth的不正确URL。如果你看看推特 3-legged OAuth documentation ,它们声明“使用GET oauth/authorify端点而不是/oauth/authenticate”。将URL更改为“https://api.twitter.com/oauth/authorize“然后再试一次。

        2
  •  0
  •   zohar Seeni Abirami    12 年前

    我设法通过更改来修复它 self.connection.request(oauth_request.http_method, RESOURCE_URL) self.connection.request(oauth_request.http_method, oauth_request.to_url())

    请注意,只有oauth_request.http_method为GET时,它才会起作用