Reuse OAuth1 authorization tokens with rauth

前端 未结 1 1931
花落未央
花落未央 2021-02-04 19:01

I have the following implementation of a twitter client using rauth (OAuth1), based on the twitter-timeline-cli.py script in

相关标签:
1条回答
  • 2021-02-04 19:54

    I was misunderstanding the whole process. We do not need to save the request_token, request_token_secret and pin, but the access_token and the access_token_secret.

    The process is actually:

    1. Use request_token, request_token_secret and pin to get access_token and access_token_secret
    2. Save access_token and access_token_secret (to database, or whatever)
    3. Next time, reuse access_token and access_token_secret

    This is my corrected test code:

    from rauth.service import OAuth1Service
    
    class TwitterClient:
    
        KNOWN_USERS = { # (access_token, access_token_secret)
            'user1' : ("xxx", "yyy")
        }
    
        def __init__(self):
            # Get a real consumer key & secret from https://dev.twitter.com/apps/new
            self.twitter = OAuth1Service(
                name='twitter',
                consumer_key=TWITTER_CONSUMER_KEY,
                consumer_secret=TWITTER_CONSUMER_SECRET,
                request_token_url='https://api.twitter.com/oauth/request_token',
                access_token_url='https://api.twitter.com/oauth/access_token',
                authorize_url='https://api.twitter.com/oauth/authorize',
                base_url='https://api.twitter.com/1/')
    
        def new_session(self):
            request_token, request_token_secret = self.twitter.get_request_token()
            authorize_url = self.twitter.get_authorize_url(request_token)
            print 'Visit this URL in your browser: ' + authorize_url
            pin = raw_input('Enter PIN from browser: ')
            session = self.twitter.get_auth_session(request_token,
                                                    request_token_secret,
                                                    method='POST',
                                                    data={'oauth_verifier': pin})
            print session.access_token, session.access_token_secret # Save this to database
            return session
    
        def reuse_session(self, user):
            access_token, access_token_secret = self.KNOWN_USERS[user]
            session = self.twitter.get_session((access_token, access_token_secret))
            return session
    
        def init_session(self, user):
            if user in self.KNOWN_USERS : session = self.reuse_session(user)
            else                        : session = self.new_session()
            return session
    
        def list_tweets(self, user):
            session = self.init_session(user)
            params = {'include_rts': 1,  # Include retweets
                      'count': 10}       # 10 tweets
    
            r = session.get('statuses/home_timeline.json', params=params)
    
            for i, tweet in enumerate(r.json(), 1):
                handle = tweet['user']['screen_name'].encode('utf-8')
                text = tweet['text'].encode('utf-8')
                print '{0}. @{1} - {2}'.format(i, handle, text)
    
    tc = TwitterClient()
    
    tc.list_tweets('user1')
    
    0 讨论(0)
提交回复
热议问题