zohar
zohar

Reputation: 2378

Cannot retrieve authenticated data from Twitter using access_token

I am trying to create generic class in python which will do all the oAuth process and then will allow to retrieve data from any oAuth supporting service (for example Twitter,LinkedIn).

Edited: I have customer key and secret and access token key and secret,when I try to request any resource request I get the following error: {"error":"Could not authenticate with OAuth.","request":"\/1\/statuses\/retweeted_by_me.json}'

any idea why?

My Code is:

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.'

Upvotes: 0

Views: 178

Answers (2)

zohar
zohar

Reputation: 2378

I managed to fix it by changing self.connection.request(oauth_request.http_method, RESOURCE_URL) to self.connection.request(oauth_request.http_method, oauth_request.to_url())

Notice that will will work only if oauth_request.http_method is GET

Upvotes: 0

Mark S.
Mark S.

Reputation: 4019

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

This is the incorrect URL to use for OAuth. If you look at Twitter's 3-legged OAuth documentation, they state "The GET oauth/authorize endpoint is used instead of /oauth/authenticate". Change the URL to "https://api.twitter.com/oauth/authorize" and try again.

Upvotes: 1

Related Questions