I have a working GET using 2-legged oauth2 in python. Here is the WORKING GET code:
import:
import oauth2 import urllib
call:
resourceUrl = "https://test.mysite:8443/ess/scheduleapi/v1/people" request = build_request(resourceUrl,'GET') u = urllib2.urlopen(request.to_url()) people_data = u.read()
query build function:
def build_request(url, method): params = { 'oauth_version': "1.0", 'oauth_nonce': oauth2.generate_nonce(), 'oauth_timestamp': int(time.time()) } consumer = oauth2.Consumer(key='mykey',secret='mysecret') params['oauth_consumer_key'] = consumer.key req = oauth2.Request(method=method, url=url, parameters=params) signature_method = oauth2.SignatureMethod_HMAC_SHA1() req.sign_request(signature_method, consumer, None) return req #end build_request
So, I thought that I could copy the GET part that I thought I needed, plus combine it with the syntax I got from some urllib2 documentation and prepare a working POST. Not this way. Keep in mind that I have the same imports and the same build_request function. Here is the BROKEN POST code. Please advise!
call:
myurl = "https://test.mysite:8443/ess/scheduleapi/v1/people" myaction = 'POST' myxml = somexmlIalreadygenerated person_added, err = post_or_put_me(myaction,myxml,myurl)
POST function:
def post_or_put_me(action,xml,url) request = build_request(url,action)
Here is my second attempt:
def post_or_put_me(action,xml,url): myrequest = build_request(url,'POST') CONSUMER_KEY = 'admin_access' CONSUMER_SECRET = 'xxxxxxxxxx' consumer = oauth2.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET) token = oauth2.Token(key=CONSUMER_KEY, secret=CONSUMER_SECRET) client = oauth2.Client(consumer, token) resp, content = client.request( url, method=action, body=urllib.urlencode(str(xml)), headers= myrequest.headers, force_auth_header=True, ) print 'resp, content are', resp, content