Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

python - Using Twython to send a tweet, twitter api error

I'm trying to make python send a tweet for me using Twython but for some reason everything I'm trying isn't working.

I've followed the Twython README but still unable to acheive what I want.

Below is my latest attempted code:

from twython import Twython, TwythonError

APP_KEY = "KEYHERE"
APP_SECRET = "SECRETHERE"

twitter = Twython(APP_KEY, APP_SECRET)
auth = twitter.get_authentication_tokens()

OAUTH_TOKEN = auth['oauth_token']
OAUTH_TOKEN_SECRET = auth['oauth_token_secret']

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

try:
    twitter.update_status(status='See how easy this was?')
except TwythonError as e:
    print e

On running the above code I get the following traceback error:

Twitter API returned a 401 (Unauthorized), Invalid or expired token

Does anyone know what I'm doing wrong and more importantly how do I fix this?

I dont have enough points for a bounty, but I would really appreciate the help!

Thanks in advance

edit

Traceback (most recent call last):
  File "C:esttweet.py", line 20, in <module>
    final_step = twitter.get_authorized_tokens(oauth_verifier)
  File "C:Python27libsite-packageswythonapi.py", line 313, in get_authorized_tokens
    raise TwythonError('Unable to decode authorized tokens.')
TwythonError: Unable to decode authorized tokens.

The above is the traceback recieved from the code supplied by @justhalf

Thanks SMNALLY

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is a far simpler way to update your post as opposed to the way Twython actually shows you. It will take more work on your API console space though, so let me begin.

  1. Firstly, you will need to go to your apps page. After you've selected the application that you are using right now, take a look at the page you are given. You should be at the Details tab by default. enter image description here

  2. Now that you are where you're supposed to be, click on the Settings tab right next to the Details tab as shown above.

  3. After that, scroll down until you see this: enter image description here

  4. Click the option as shown above. Now after you've selected the option scroll down until you see a blue button saying Update this twitter's application settings.

  5. Now, head back over to your Details tab. Go to the bottom and generate your required tokens, please note that you might have to click the buttons a couple of times to get it to work (also, make sure that your access level is Read, write, and direct messages when the token is generated): enter image description here

  6. Now you have everything necessary for connecting and posting to your twitter account. You have the Consumer key and the Consumer Secret, the Access token and the Access token secret. You have everything that you need.

Okay, now head over to your code editor, and write the following boiler-plate code (these keys wont work, I just got rid of the application, so no hacking here :P I've given them simply as an indication of the length of the keys that you should expect):

from twython import Twython

APP_KEY = ''  # Customer Key here
APP_SECRET = ''  # Customer secret here
OAUTH_TOKEN = '1936951807-z5bBNING8P1TU2onWvJh5dh8hoYlYAmNOaAx2OX'  # Access Token here
OAUTH_TOKEN_SECRET = 'QWJEZ7ridSeZGdxJELSBk7mupCpMA9q9sLCou5ywg'  # Access Token Secret here

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

twitter.update_status(status="Hello from Python! :D")

After this, check your twitter, you should see a new tweet saying "Hello from Python! :D".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...