I am trying to run a REST API with python. I can invoke the API successfully from the browser, but I get authentication errors when doing so from python or from the command line.
I've already authenticated myself with the brower in order to be able to access the API. It is a complicated multi-factor auth which I don't want to have to do via python, if I even could.
So my question: Is there a way to extract the token from the browser session and re-use that in my python application?
Or maybe I am already authenticated and I just need to identify myself?
Thanks!
I am trying to run a REST API with python. I can invoke the API successfully from the browser, but I get authentication errors when doing so from python or from the command line.
I've already authenticated myself with the brower in order to be able to access the API. It is a complicated multi-factor auth which I don't want to have to do via python, if I even could.
So my question: Is there a way to extract the token from the browser session and re-use that in my python application?
Or maybe I am already authenticated and I just need to identify myself?
Thanks!
Share Improve this question asked Feb 5 at 13:47 TreninTrenin 2,0631 gold badge15 silver badges20 bronze badges1 Answer
Reset to default 1Well, Yes, there a way to extract the token from the browser session and re-use that in your python application. Here's how:
Open your browser's Developer Tools (F12), go to the Application or Storage tab, which way you like, and find the Cookies or Local Storage for the token.
Add the token to your request headers:
import requests
url = "https://api.example.com/endpoint"
headers = {
"Authorization": "Bearer YOUR_TOKEN_HERE"
}
response = requests.get(url, headers=headers)
Also remember that if your token expires, you might need to refresh it or reauthenticate.
This lets you bypass multi-factor auth in Python by reusing the browser session's token.