我正在尝试捕获API在身份验证过程中返回的访问令牌。从API进行身份验证过程的官方方法是:
from xyz_api import accessToken
app_id = "your_app_id"
app_secret = "your_app_secret"
app_session = accessToken.SessionModel(app_id, app_secret)
response = app_session.auth()
上面将返回一个类似于:的json
{
"code" : 200,
"data" : {
"authorization_code": "some random code"
},
}
现在,以下代码用于生成URL:
authorization_code = “your_authorization_code”
app_session.set_token(authorization_code)
url = app_session.generate_token()
现在这是我开始有问题的地方。
现阶段,API作者推荐的是:
1. Use the generated URL and copy paste it into a browser.
2. The browser will then do the authentication and return the access token to a redirect
url (I used http://localhost:5000).
3. Copy and paste the access token from redirect URL
我想要什么:
To be able to finish the authentication and get the access_token from
python code itself.
我尝试过的:使用request.get(url(,但它不起作用。
有没有任何方法可以在不需要打开浏览器的情况下仅使用python来完成这一切?
PS: The API I am trying to use is: https://api-docs.fyers.in/v1#authorization
进一步调查
在进一步的调查中,我发现了以下内容:
API使用oauth
从API作者提供的以下代码中获得的URL
url=app_session.generate_token((
与我编写以下代码时相同:
oauth = OAuth2Session('app_id')
authorization_url, state = oauth.authorization_url(url)
在这两种情况下返回的url的形式都是:
https://api.fyers.in/api/v1/genrateToken?authorization_code=some_auth_code&appId=my_app_id
如果我在浏览器中复制粘贴这个URL,它会将access_token发送回我的redirect_URL(http://localhost:5000)
我正在尝试使用oauth::fetch_token获取访问令牌,并使用以下代码,但失败了:
oauth1=OAuth2Session('app_id',state=state,redirect_uri="http://localhost:5000"(
token=oauth1.fetch_token('https://api.fyers.in/api/v1/genrateToken/',client_secret=‘app_secret’,代码="e_auth_code_return"(
非常感谢您的帮助。
您只能使用Python访问。。。你不需要在浏览器中手动执行任何操作。。
此API不确定您是否拥有.
我相信你注册了一个应用程序,得到了你的app_secret
步骤
response = app_session.auth()
用authorization_code
回答您
"data" : {
"authorization_code": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqaGdqZzg3NiQ3ODVidjVANjQ3NTZ2NSZnNyM2OTg3Njc5OHhkIjoiWElHVFVYVjBSSSIsImV4cCI6MTU2MTU5NzM5Ny41NjQxNTV9.Agl-Uus63NforrUEdbG7YUlPcbFXu9hLYm4akGuIBkU"
您可以访问类似的授权令牌
import json
r = json.dumps(reponse)
authorization_token = r['data']['authorization_code']
必须将授权令牌AND您的app_id
作为查询字符串传递给https://api.fyers.in/api/v1/genrateToken?
然后,REAL用户将被要求接受登录您的应用程序如果被接受,用户将被重定向到我想象中你在应用程序注册中通知的URL。
让我知道是否合理