最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can I re-use browser authentication with python REST? - Stack Overflow

programmeradmin0浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

Well, 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.

发布评论

评论列表(0)

  1. 暂无评论