3

I'm trying to PUT some results to a URL. My payload is correctly formatted and the URL is correct as well. The problem is that it requires two-factor authentication using Google's Authenticator besides a username and password. I can't find how to authenticate using the requests package in Python. http://docs.python-requests.org/en/master/user/authentication/

I only have a username and a password so I tried both requests' Basic authentication and Digest authentication. Both return a 401 status code.

import requests
from requests.auth import HTTPDigestAuth
import json

url = 'https://some_url.com'
payload = json.dumps(some_data)
username = 'username'
password = 'password'

r = requests.put(url, payload, auth=(username, password))
print(r.status_code) #gives 401

r2 = requests.put(url, payload, auth=HTTPDigestAuth(username, password))
print(r2.status_code) #gives 401 as well
tstnbrgn
  • 31
  • 4
  • Are you sure, you need a `payload=` to login? Are you sure, you have to use `.put(...` to login? It's common to use [`.Session`](http://docs.python-requests.org/en/master/user/advanced/) and **first** login and **second** access url resources. – stovfl Dec 27 '18 at 15:42
  • @stovfl Thanks for your comment. I tried the following as well, which unfortunately does not work either. `with requests.Session() as sess: sess.auth = (username, password) r = sess.put(url, payload) print(r.status_code)` – tstnbrgn Dec 27 '18 at 16:06
  • *"does not work either."*:Yes it's the same, you are allready doing, **login + payload + put** within **one** request! Read, **skip the Cookie part**, [how-to-use-python-requests-to-login-to-website](https://stackoverflow.com/questions/38110760/how-to-use-python-requests-to-login-to-website-store-cookie-then-access-anothe). [Edit] your Question and explaine in detail your meaning of *"two-factor authentication"* – stovfl Dec 27 '18 at 18:32
  • Honestly, i am confusing about why you did not show us the api's document link. – KC. Dec 28 '18 at 08:44

0 Answers0