title: Python urllib2 open request fails with urllib2.HTTPError: HTTP Error 401: Unauthorized
body: <p>Below is my python code using urllib2 library and it keeps failing with an Unauthorized error although I am using the correct API key.  If I user curl, the POST/GET works just fine.Anyone got ideas? Thanks.</p>
  
  <p>Adding the curl commands below that works just fine</p>
  
  <pre><code>Create Credential 
  curl -X POST 'https://myurl.com' \
       -H 'Content-Type: application/json' \
       -u 'XXXXXXXXXX:' \
       -d @- &lt;&lt; EOF
  { 
    "vendorAccountId": "1234567",
    "type": "my_role"
  }
  EOF
  </code></pre>
  
  <p>Below is the python code which doesn't work.
  Basically, the line of code where it is failing is: response = opener.open(request)</p>
  
  <pre><code>import boto3
  import json
  import logging
  import signal
  import requests
  from urllib2 import build_opener, HTTPHandler, Request
  import urllib2
  
  
  LOGGER = logging.getLogger()
  LOGGER.setLevel(logging.INFO)
  
  
  def main():
          auth_token = "XXXXXXXXXX"
          account_id = "1234567"
          request_type = "CreateCredentials"
          content_type = ""
          request_body = json.dumps({})
  
           if request_type == "CreateCredentials":
              target_url = 'https://myurl.com'
              request_method = "POST"
              content_type = "application/json"
              request_body = json.dumps({
                  "vendorAccountId": account_id,
                  "type": "my_role"
              })
  
          handler = urllib2.HTTPHandler()
          opener = urllib2.build_opener(handler)
  
          request = urllib2.Request(target_url, data=request_body)
          request.add_header("Content-Type", content_type)
          request.add_header("Content-Length", len(request_body))
          request.add_header("Authorization", auth_token)
          request.get_method = lambda: request_method
          response = opener.open(request) #*****Fails here******
  
  
  if __name__ == "__main__":
      main()
  </code></pre>