简体   繁体   中英

How do I login given an API endpoint and get token?

import requests,json

payload = {
    'username': 'employee',
    'userpassword': 'abc123'
}

login_route = '/eNextWebAPI/login'
url = 'https://lb-clus-08-01.mycompany.local/myinstance'


with requests.Session() as s:
    p = s.post(url, data=payload)
    print(p.text)

I tried to post on login page with the code above, but I got:

Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\urllib3\connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\urllib3\connectionpool.py", line 346, in _make_request
    self._validate_conn(conn)

...
...

ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\adapters.py", line 440, in send
    timeout=timeout
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\urllib3\connectionpool.py", line 639, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\urllib3\util\retry.py", line 388, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='-----', port=443): Max retries exceeded with url: /myinstance (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)'),))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/User/.PyCharmCE2017.1/config/scratches/API.py" line 13, in <module>
    p = s.post(url, data=payload)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\sessions.py", line 555, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\adapters.py", line 506, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='......', port=443): Max retries exceeded with url: /myinstance (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)'),))

Process finished with exit code 1

The reason is because your endpoint is using an untrusted SSL certificate. If you are just using this for testing purposes then you can quickly get around it by adding an argument to post method.

with requests.Session() as s:
    p = s.post(url, data=payload, verify=False)
    print(p.text)

But you would ideally have to get a valid certificate. Try googling or checking out this answer Where could I buy a valid SSL certificate? for more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM