Posting a retweet via twython gives 401 whereas I

2019-07-28 03:45发布

问题:

I am using twython twitter API.

I am obtaining the oauth_token and secret as follows -

twitter = Twython(
        settings.TWITTER_KEY,
        settings.TWITTER_SECRET,
        )
    tw_callback_url = request.build_absolute_uri(reverse('save_twitter_token'))
    twitter_auth = twitter.get_authentication_tokens(callback_url=tw_callback_url)
    request.session['twitter_auth'] = twitter_auth

and then saving it after signing it with oauth_verifier as follows -

oauth_verifier = request.GET['oauth_verifier']
temp_o_token_secret = request.session['twitter_auth']['oauth_token_secret']
temp_o_token = request.session['twitter_auth']['oauth_token']
twitter = Twython(settings.TWITTER_KEY, settings.TWITTER_SECRET,
              temp_o_token, temp_o_token_secret)
final_step = twitter.get_authorized_tokens(oauth_verifier)
final_oauth_token = final_step['oauth_token']
final_oauth_token_secret = final_step['oauth_token_secret']

Once this is done, when I do -

twitter = Twython(settings.TWITTER_KEY, 
                  settings.TWITTER_SECRET, 
                  acct_inst.oauth_token, 
                  acct_inst.oauth_token_secret)
try:
    user_timeline = twitter.get_home_timeline()
except TwythonError as e:
    print user_timeline

and I get the desired output.

But when I do -

twitter = Twython(settings.TWITTER_KEY,
                  settings.TWITTER_SECRET,
                  ACCOUNT.oauth_token,
                  ACCOUNT.oauth_token_secret)
res = twitter.retweet(id=twitter_id)

where twitter_id is the status_id of a tweet.

I get 401 unauthorized error - *** TwythonAuthError: Twitter API returned a 401 (Unauthorized), An error occurred processing your request.

Any idea what I am missing?

回答1:

Try to use the id_str from the status instead of the id