Search code examples
pythondjangotwitterpython-social-auth

Tweet on behalf of users who have authenticated my app to post tweet?


I have an app, using this app in which user can sign-up via twitter and I have set permission of Read, write, and direct messages , So when user sign-up to my project he authorized me that I can post on-behalf of him. Also during signup I have got his oauth-token and oauth-token secret.

But I don't know how I can post tweet on behalf of that user. I try to submit a form at https://api.twitter.com/1.1/statuses/update.json with parmas like status , my app key and secret , user oauth_token and secret but in response I am always getting this

{"errors":[{"message":"Bad Authentication data","code":215}]}

I am using python-social-auth for sign up via twitter.

Any help would be appreciated. Thanks


Solution

  • You need to specify the user access_token with your request, that's the way Twitter knows which is the current authenticated user. With python-social-auth you can do:

    # given a user instance (and assuming Django ORM)
    social = user.social_auth.get(provider='twitter')
    backend = social.get_backend()
    form_data = {
        'status': 'The new twitter status goes here'
    }
    response = backend.get_json(
        'https://api.twitter.com/1.1/statuses/update.json', 
        method='POST',
        auth=backend.oauth_auth(social.extra_data['access_token']),
        data=form_data
    )