Here is my current code to do a POST in python using oauth2. For the life of me, I cannot get past the 401 error, and I think it might have something to do with my headers. I am just pasting my headers in here as they print out, as I have used another routine to generate them with myheaders=req.to_header(). This should show you what’s really in there. What looks wrong? Please help!
def post_or_put_me(myaction,myxml,myurl):
CONSUMER_KEY = 'my_admin_access'
CONSUMER_SECRET = 'xxxxxxxx'
consumer = oauth2.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
token = oauth2.Token(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
client = oauth2.Client(consumer, token)
myheaders = {'Authorization': 'OAuth realm="",oauth_body_hash="xxxxxxxx",oauth_nonce="84691521", oauth_timestamp="1351179163",oauth_consumer_key="my_admin_access",oauth_signature_method="HMAC-SHA1", oauth_version="1.0", oauth_signature="xxxxxxxxxx"'}
xml_string = ElementTree.tostring(myxml)
data = urllib.urlencode({'xml': xml_string})
resp, content = client.request(
myurl,
method=myaction,
body=data,
headers = myheaders,
)
I finally figured out that the 2-legged oauth2 POST/PUT builds its own headers; I do not have to populate them, except for the Content-Type (in my case). See the answer supplied here for full details of how to do both a 2-legged GET and a 2-legged POST/PUT: How do I send a POST using 2-legged oauth2 in python?.