title: How do I send a POST using 2-legged oauth2 in python?
body: <p>I have a working GET using 2-legged oauth2 in python.  Here is the <strong>WORKING</strong> GET code:</p>
  
  <p>the imports:</p>
  
  <pre><code>import oauth2 
  import urllib #for url-encode
  import urllib2 #for getting and receiving data from server
  import time #Unix timestamp import oauth2
  </code></pre>
  
  <p>the call:</p>
  
  <pre><code>resourceUrl = "https://test.mysite:8443/ess/scheduleapi/v1/people"
  request = build_request(resourceUrl,'GET')
  u = urllib2.urlopen(request.to_url())
  people_data = u.read()
  </code></pre>
  
  <p>the function to build the request:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>So, I thought I could copy the part of the GET that I thought I'd need, plus combine it with the syntax I got off of some urllib2 documentation, and cook up a working POST.  Not so.  Keep in mind I have the same imports and the same build_request function.  Here is the <strong>BROKEN</strong> POST code.  Please advise!</p>
  
  <p>the call:</p>
  
  <pre><code>myurl =  "https://test.mysite:8443/ess/scheduleapi/v1/people" 
  myaction = 'POST'
  myxml = somexmlIalreadygenerated
  person_added, err = post_or_put_me(myaction,myxml,myurl)
  </code></pre>
  
  <p>the function to POST:</p>
  
  <pre><code>def post_or_put_me(action,xml,url)
      request = build_request(url,action) # use same header-generating code as GET did?
      post_data = urllib.urlencode(xml)
      req = urllib2.Request(request,post_data)
      try:
          u = urllib2.urlopen(req)
          post_or_put_returned_data = u.read()
          print 'LENGTH  :', len(post_or_put_returned_data)
          print 'DATA    :', post_or_put_returned_data
      except urllib2.HTTPError, e:
          server_error = 'HTTPError = ' + str(e.code)
      except urllib2.URLError, e:
          server_error = 'URLError = ' + str(e.reason)
      except httplib.HTTPException, e:
          server_error = 'HTTPException'
      except Exception:
          import traceback
          server_error = 'generic exception: ' + traceback.format_exc()
      #endtry
  
      if server_error:
          err_msg = server_error
      else:   
          succ_msg = 'you had a successful post or put'
      #endif
  
      return succ_msg, err_msg
  #end post_or_put_me
  </code></pre>
  
  <p>Here's my second attempt:</p>
  
  <pre><code>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
  </code></pre>