How to make a put request when api requires two-factor authentication using Python's requests package?

Viewed 1343

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
0 Answers
Related