Yuriy F
Yuriy F

Reputation: 361

User authentication for Twitter API using Python application

I am working on a module for an application written in Python (it is open-source). I want the module to allow a user to log into Twitter and update status (post a tweet). I have Python script I wrote using the twitter import that requires a developer (me) to put in consumer/authentication token/secrets. I don't want to have the tokens pre-programmed into the module, but rather have a user log in to update THEIR status (obviously).

I need a general direction to pursue. I envision the user clicking on a Share! menu option, a browser popping up with the Twitter login page, a user logging in and the application storing the credentials, and then updating the status from there. I have the module working, but like I said before, I need to input token information from my application's apps.twitter.com page. I want the user to be able to log in.

Here is a snip of code using the twython library:

from twython import Twython

#APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET
OAUTH_TOKEN = '...'
OAUTH_TOKEN_SECRET = '...'
APP_KEY = '...'
APP_SECRET = '...'

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
if (twitter.update_status(status='The status is updated from Python!')):
    print("Working!")
else:
    print("Not working!")

The code above works and updates my Twitter status, but I do not want other users of this module to be updating my status, but rather have them log in and update their status.

Thanks in advance.

EDIT What I have now:

import urllib.parse
import oauth2 as oauth

consumer_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXX'
consumer_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
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'

consumer = oauth.Consumer(consumer_key, consumer_secret)
client = oauth.Client(consumer)
resp, content = client.request(request_token_url, "GET")

if resp['status'] != '200':
    raise Exception("Invalid response %s." % resp['status'])

request_token = dict(urllib.parse.parse_qsl(content))
print("Request Token:")
print("    - oauth_token        = %s" % request_token[b'oauth_token'].decode())
print("    - oauth_token_secret = %s" % request_token[b'oauth_token_secret'].decode())

print("Go to the following link in your browser:")
print("%s?oauth_token=%s" % (authorize_url, request_token[b'oauth_token'].decode()))

accepted = 'n'
while accepted.lower() == 'n':
    accepted = input('Have you authorized me? (y/n) ')

oauth_verifier = input('What is the PIN? ')
token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret'])

token.set_verifier(oauth_verifier)
client = oauth.Client(consumer, token)

resp, content = client.request(access_token_url, "POST")
access_token = dict(urllib.parse.parse_qsl(content))

print(access_token.keys())
print(access_token.values())
print("You may now access protected resources using the access tokens above.")

import twitter
api = twitter.Api(consumer_key=' ', consumer_secret=' ',
                  access_token_key=' ', access_token_secret=' ')
tweet = api.PostUpdate("First tweet from an authenticated user.")

I get to the point where I authorize my application with the login, the webpage redirects to the callback_url, just like it should, but I don't get a PIN to input. Where does that come in?

Upvotes: 0

Views: 1493

Answers (1)

iJaydip
iJaydip

Reputation: 1

i know it late but it can be help others

you can find verify token after your app verify and it will be show into header.

Upvotes: -1

Related Questions