Question

I have a twitter account with an App made, currently it's setup so students can tweet from our website and as such their tweets show "via SchoolAppNameHere" at the bottom of their tweets.

Is it possible to use Twython to use the Appkey and secret key and then get auth tokens from a completely different so when I was to run the bit of code below it would tweet from an account what didn't create the app...

from twython import Twython

APP_KEY = ''
APP_SECRET = ''
OAUTH_TOKEN = ''
OAUTH_TOKEN_SECRET = ''

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
twitter.update_status(status="test")

Any ideas would be much appreciated :)

Edit, Updated example/explanation below:

Let's say the following image's are from the account "stackoverflowapp" and the app called "Stackoverflow Test App":

http://i.imgur.com/8sIpak5.png http://i.imgur.com/e6CYt6e.png

Using the following bit of code would tweet from the account "stackoverflowapp" with the tweet "test" via the applicationg called "Stackoverflow Test App"

from twython import Twython

APP_KEY = 'coN_kEY_123456789'
APP_SECRET = 'cOn_sEcr3t_123456789'
OAUTH_TOKEN = 'Acc3ss_tok3N_123456789'
OAUTH_TOKEN_SECRET = 'aCCeSS_tOkEn_sEcrET_123456789'

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
twitter.update_status(status="test")

Let's say that the following image is from the account "useraccount1" and the app is called "testing123":

http://i.imgur.com/vYJLmfr.png

So now that I have the access tokens to login to the account "useraccount1", how can I tweet via the app called "Stackoverflow test app" which was created by the user: "stackoverflowapp" example of what I tried is below:

from twython import Twython

APP_KEY = 'coN_kEY_123456789'
APP_SECRET = 'cOn_sEcr3t_123456789'
OAUTH_TOKEN = 'Acc3ss_123456789'
OAUTH_TOKEN_SECRET = 'aCCeSS_sEcrET_123456789'

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
twitter.update_status(status="test update")

Unfortunately, I get the error:

TwythonAuthError: Twitter API returned a 401 (Unauthorized), Could not authenticate you
Was it helpful?

Solution

This is of course, possible. When you create an application in Twitter, they give you your own authentication tokens for you to use immediately, as a convenience.

Use the access token string as your "oauth_token" and the access token secret as your "oauth_token_secret" to sign requests with your own Twitter account. Do not share your oauth_token_secret with anyone.

To get keys for other accounts for the same application, you need to request more keys for each account. This is described in detail here: https://dev.twitter.com/docs/auth/obtaining-access-tokens

Since it sounds like you're going to be doing the authorization yourself, the simpler PIN-based approach should probably be used.

You're using twython, obtaining these can be done using the library: https://twython.readthedocs.org/en/latest/usage/starting_out.html#authentication

get_authentication_tokens and get_authorized_tokens are the methods you're looking for.

from twython import Twython
import sys

APP_KEY = 'coN_kEY_123456789'
APP_SECRET = 'cOn_sEcr3t_123456789'

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

print( 'Visit %s and enter your PIN: ' % auth.get( 'auth_url' ) ),
pin = sys.stdin.readline().strip()

twitter = Twython( APP_KEY, APP_SECRET, auth.get( 'oauth_token' ), auth.get( 'oauth_token_secret' ) )
tokens = twitter.get_authorized_tokens( pin )

print( 'OAUTH_TOKEN: %s' % tokens.get( 'oauth_token' ) )
print( 'OAUTH_TOKEN_SECRET: %s' % tokens.get( 'oauth_token_secret' ) )

Store OAUTH_TOKEN and OAUTH_TOKEN_SECRET some place safe and reuse at will. Also, make sure you authorize the correct account when visiting the URL and getting the PIN.

All your API calls will be made on bahalf the account that authorized access via the tokens and your via line will be your original application. Use the appropriate tokens for each account you'd like to tweet from, it's not possible to mix and match.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top