oauth2 POST - twitter
<p>i created a script that will get the users friend list (GET request) and i was successful. Now i am attempting to make a script that will follow a particular user (POST request) and i've been unsuccessful.</p>

<p>here is my oauth function (where the problem lies):</p>

<pre><code>def augment_POST(url,**kwargs) :
    secrets = hidden.oauth()
    consumer = oauth2.Consumer(secrets['consumer_key'], secrets['consumer_secret'])
    token = oauth2.Token(secrets['token_key'],secrets['token_secret'])

    oauth_request = oauth2.Request.from_consumer_and_token(consumer, token= token, http_method='POST', http_url=url, parameters=kwargs)
    oauth_request.to_postdata() # this returns post data, where should i put it?
    oauth_request.sign_request(oauth2.SignatureMethod_HMAC_SHA1(), consumer, token)
    return oauth_request.to_url()
</code></pre>

<p>my augment_GET function is the exact same thing except http_mehtod='GET'</p>

<p>for clarity:</p>

<pre><code>def follow_user(id):
    seedurl="https://api.twitter.com/1.1/friendships/create.json"
    print 'Attempting to follow: %d' % (id,)
    url = augment_POST(seedurl,user_id=id)
    connection = urllib.urlopen(url)
    data = connection.read()
    headers = connection.info().dict
</code></pre>

<p>any help will be greatly appreciated.</p>

<p>First it seems you need to <code>import urllib2</code> to make a POST request.<br>
You have to send the POST data that you get from the <code>to_postdata</code> method
using the <code>data</code> argument of <code>urlopen</code>:</p>

<pre><code>def augment_POST(url, **kwargs) :
    secrets = hidden.oauth()
    consumer = oauth2.Consumer(secrets['consumer_key'],
                               secrets['consumer_secret'])
    token = oauth2.Token(secrets['token_key'],
                         secrets['token_secret'])

    oauth_request = oauth2.Request.from_consumer_and_token(
        consumer,
        token= token,
        http_method='POST',
        http_url=url,
        parameters=kwargs
    )

    oauth_request.sign_request(oauth2.SignatureMethod_HMAC_SHA1(),
                               consumer, token)

    # this is the data that must be sent with you POST request
    return oauth_request.to_postdata()


def follow_user(id):
    url = "https://api.twitter.com/1.1/friendships/create.json"
    print 'Attempting to follow: %d' % id
    postdata = augment(url, method='GET', user_id=id)

    # Send the POST request with the data argument
    # The url is the same as the data is sent in the body of the request
    connection = urllib2.urlopen(url, data=postdata)

    data = connection.read()
    headers = connection.info().dict
</code></pre>

<hr>

<p>I would recommend to use the <code>requests_oauthlib</code> module which makes all this really easy:</p>

<pre><code>from requests_oauthlib import OAuth1Session

tokens = hidden.oauth()
client = OAuth1Session(tokens['consumer_key'],
                       tokens['consumer_secret'],
                       tokens['token_key'],
                       tokens['token_secret'])


def follow_user(id):
    url = "https://api.twitter.com/1.1/friendships/create.json"
    print 'Attempting to follow: %d' % id

    # for GET requests use client.get and the `params` argument
    # instead of the `data` argument
    response = client.post(url, data={'user_id': id})

    data = response.text
    # or even `data = response.json()` to decode the data
    headers = response.headers
</code></pre>

python twitter oauth-2.0 twitter-oauth