How do I send a POST using 2-legged oauth2 in python?

后端 未结 2 1023
情书的邮戳
情书的邮戳 2020-12-09 23:32

I have a working GET using 2-legged oauth2 in python. Here is the WORKING GET code:

the imports:

import oauth2 
import urllib #for          


        
2条回答
  •  孤街浪徒
    2020-12-10 00:13

    This is the code I have been using to make a POST request to Twitter using oauth2. Hope it helps you to figure out the syntax.

    import oauth2 as oauth, urllib
    
    def oauth_req(url, key, secret, http_method="POST", post_body=None, http_headers=None):
        CONSUMER_KEY = YOUR_KEY
        CONSUMER_SECRET = YOUR_SECRET
        consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
        token = oauth.Token(key=key, secret=secret)
        client = oauth.Client(consumer, token)
        resp, content = client.request(
            url,
            method=http_method,
            body=urllib.urlencode({'status': post_body}),
            headers=http_headers,
            force_auth_header=True,
        )
        return content
    
    oauth_req('http://api.twitter.com/1/statuses/update.json', KEY, SECRET, post_body=MESSAGE)
    

提交回复
热议问题