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

后端 未结 2 1014
情书的邮戳
情书的邮戳 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:08

    Here is ACTUAL, WORKING code of how I got my POST or PUT to work, kindly supplied by Wes Barnes from Echo360 Lecture Capture. I don't want anyone else doing a 2-legged oauth POST/PUT to have to reinvent the wheel.

    import oauth2 as oauth
    import time
    import urllib2 as urllib
    
    echo_base_url = 'http://pilot.echo360.com/ess/scheduleapi/v1'
    
    consumer = oauth.Consumer(key ='xxxxx', secret='xxxx')
    client = oauth.Client(consumer)
    params = "<person><first-name>Jon</first-name><last-name>Doe</last-name><title>Super  Hero</title><email-address>jdoe17@echo360.com</email-address><block-alerts>false</block-alerts><time-zone>US/Eastern</time-zone><locale>en_US</locale><credentials><user-name>jdoe17@echo360.com</user-name><password>password</password></credentials><organization-roles><organization-role><organization-id>b1973c39-dc76-4cab-a4aa-3f9efd628df2</organization-id><role>role-name-admin</role></organization-role></organization-roles></person>"
    
    resp, content = client.request(
                    echo_base_url + "/people/",
                    method = "PUT",
                    body=params,
                    headers={'Content-type': 'application/xml'}
                    #force_auth_header=True
                    )
    print resp, content
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题