Bastian
Bastian

Reputation: 5855

Django users post to twitter

I use Django-social-auth to authenticate the users of a Django project. So I guess I have all the necessary information about a Twitter user to make a post on their behalf on their Twitter account. So do I really need to install a new app? or with the info at hand how would I do that? Isn't it just a matter of posting to a Twitter API with the relevant info?

Upvotes: 1

Views: 890

Answers (3)

Bastian
Bastian

Reputation: 5855

So if you use Django Social Auth you can do:

import urllib
import settings
import oauth2 as oauth

try:
    twitter_user = user.social_auth.get(provider='twitter')
except:
    return
if not twitter_user.tokens:
    return
access_token = twitter_user.tokens['oauth_token']
access_token_secret = twitter_user.tokens['oauth_token_secret']

token = oauth.Token(access_token,access_token_secret)
consumer_key = settings.TWITTER_CONSUMER_KEY
consumer_secret = settings.TWITTER_CONSUMER_SECRET
consumer = oauth.Consumer(consumer_key,consumer_secret)
client = oauth.Client(consumer,token)

data = {'status':'Your tweet goes here'}
request_uri = 'https://api.twitter.com/1/statuses/update.json'
resp, content = client.request(request_uri, 'POST', urllib.urlencode(data))

Upvotes: 2

super9
super9

Reputation: 30121

You could just extract the code into your own project and that will work. But the benefits of using an open source library is that there's a good chance when Twitter or Social Network X changes it's API, the library, if popular, would get updated as opposed to you needing to make the change.

Upvotes: 2

pcx
pcx

Reputation: 994

Yes, I believe if you have the User's twitter account details that are necessary, then you can write an (django) app which will do so. You can use something like python-twitter to post to twitter.

You should be able to find more information regarding Twitter's api here.

Upvotes: 0

Related Questions