0

I'm trying to log into the vRealize Operations Rest API using basic auth method from this question:

HTTP Basic Authentication not working in Python 3.4

So I use the second code sample:

import urllib.parse
import urllib.request
import urllib.response

userName = "username"
passWord  = "password"
top_level_url = "URL"

# create an authorization handler
p = urllib.request.HTTPPasswordMgrWithDefaultRealm()
p.add_password(None, top_level_url, userName, passWord);

auth_handler = urllib.request.HTTPBasicAuthHandler(p)

opener = urllib.request.build_opener(auth_handler)

urllib.request.install_opener(opener)

try:
    result = opener.open(top_level_url)
    messages = result.read()
    print (messages)
except IOError as e:
    print (e)

and I get a blue font: HTTP Error 401: Unauthorized

So I think this is an issue with trusting the certificate or it has something to do with the headers. How can I work around this? Any advice would be appreciated.

Community
  • 1
  • 1
PA_Commons
  • 269
  • 2
  • 6
  • 14
  • Are you **sure** the username and password are correct? Can you use them to log in to the website in a browser? – John Gordon Feb 18 '16 at 17:11
  • Yes sir, I can paste my url into the browser and access the server with the same credentials that are in my python program. We have a powershell developer who was able to get into the site, he code includes headers and trusts all certs. Those are the main differences from the code above. – PA_Commons Feb 18 '16 at 17:17
  • Are you running this code on the same computer as your browser? (i.e. maybe the API server is configured to only allow connections from specific client machines) – John Gordon Feb 18 '16 at 17:18
  • Yes sir, I am running the code on the same machine. – PA_Commons Feb 18 '16 at 17:33
  • 1
    I tried that code myself and it works, so I can only assume that your URL, username or password are wrong. – John Gordon Feb 18 '16 at 17:48
  • well the burden of proof would lie with me however since you can't access the private network I'm on you'll have to take my word for it. The URL, username and password are all correct. I believe it has to do with trusting the self-signed certificate. – PA_Commons Feb 18 '16 at 18:13
  • VMware originally wrote a module called Nagini for 2.7.9 versions of python and later, I assume they did this for a similar reason as to the problem I'm having, however being a novice with python makes it a little difficult to troubleshoot this myself. – PA_Commons Feb 18 '16 at 18:18
  • If it were a certificate issue, I assume the error wouldn't be `HTTP 401: Unauthorized`. – John Gordon Feb 18 '16 at 18:22
  • You are probably right good sir, I'm on the VMware developer forums asking a similar question. I'm surprised they don't support 3.3. – PA_Commons Feb 18 '16 at 18:42
  • 1- test your code with an analog of http://httpbin.org (you can install it locally) so that we could try it too 2- Why do your think Python version has any significance here? Does it work on Python 3.5 or any other Python version e.g., Python 2.7 (change the imports)? – jfs Feb 19 '16 at 11:53
  • The Module created by VMware says version 2.7.9 or later. I assume that doesn't mean 3.x? Regardless, the module makes use of Requests. Requests isn't available for 3.X is it? – PA_Commons Feb 19 '16 at 14:50

0 Answers0