0

I have built a Django webpage with a text area and I would like a user of the site to be able to tweet the content of the text area.

I have been able to post on my own account (with developer credentials), however I'm having trouble extracting the user's secret token (gained after logging in with python social auth), and without this they cant make a post.

The problem seems to be that the secret token cannot be found in the dictionary that holds this information. Bearing in mind that login via OAuth works correctly; what is it past this point, that I am not doing, or doing incorrectly that means that 'oauth_secret_token' cannot be found/ does not exist?

views.py

def form_handle(request):
    form = MyForm()
    if request.method=='POST':
        form = MyForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            text = cd.get('text')
            if 'tweet' in request.POST:
                user = request.user
                soc = user.social_auth.get(provider='twitter')
                access_token = soc.extra_data['access_token'] 
                access_secret = soc.extra_data['oauth_token_secret'] <-- error generated here.
                post_tweets().post_my_tweet(text, access_token, access_secret)
                form = MyForm()
                return render(request, 'hello.html',{'form':form})
    else:
        form = MyForm()
        return render(request, 'hello.html',{'form':form}) 

twitter_posting.py

from twitter import *
class post_tweets():
    ckey= "XXXXXXXXXXXXX"
    csecret= "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

    def post_my_tweet(self, tweet, atoken, asecret):
        t = Twitter(auth=OAuth(atoken, asecret, self.ckey, self.csecret))
        t.statuses.update(status= str(tweet))  

EDIT:

I have read the Tweepy documentation here: http://docs.tweepy.org/en/v3.2.0/auth_tutorial.html

And Twython documentation here: https://twython.readthedocs.org/en/latest/usage/basic_usage.html#updating-status

I am convinced that both of these libraries would be able to help me, however cant work out how to apply it to my code. Would someone from one of these comunities be able to give me an example of how I can modify this so it tweets from the users account?

1 Answers1

0

The answer is that the oauth token and secret are inside the second dimension of a 2d dictionary. The key to the dictionary is called 'access_token' it holds five KV pairs with keys: 'oauth_token', 'oauth_token_secret', 'x_auth_expires', 'user_id', and 'screen_name'.

This therefore is how you post to twitter from a users account using django:

views.py

def form_handle(request):
form = MyForm()
if request.method=='POST':
    form = MyForm(request.POST)
    if form.is_valid():
        cd = form.cleaned_data
        text = cd.get('text')
        if 'tweet' in request.POST:
            user = request.user
            soc = user.social_auth.get(provider='twitter')
            access_token = soc.extra_data['access_token'] ['oauth_token'] <-- changed
            access_secret = soc.extra_data['access_token']['oauth_token_secret'] <-- changed
            post_tweets().post_my_tweet(text, access_token, access_secret)
            form = MyForm()
            return render(request, 'hello.html',{'form':form})
else:
    form = MyForm()
    return render(request, 'hello.html',{'form':form}) 

twitter_posting.py is unchanged from the question above. If you would like to view the twitter data returned upon login you can create a custom pipeline that prints out all the data (this is how I solved the problem). Without turning this into a pipeline tutorial I've added some helpful links below.

http://psa.matiasaguirre.net/docs/pipeline.html https://github.com/omab/python-social-auth/tree/master/social/pipeline