简体   繁体   English

如何在python中获取访问令牌

[英]how to get access token in python

I want to make a rest api call and get access token and that access token will be there for 30 min.我想进行一个 rest api 调用并获取访问令牌,该访问令牌将在那里持续 30 分钟。 Again i need to refresh that token.我再次需要刷新该令牌。

I am not getting how to get the access token, tried numerous code snippets.我不知道如何获取访问令牌,尝试了大量代码片段。

Sample access token will be as below: KkcwAIyUm6XGbGUA0wejna6_8kk3Zuo66BigYo3gAGI.示例访问令牌如下:KkcwAIyUm6XGbGUA0wejna6_8kk3Zuo66BigYo3gAGI。

URL used in post man to get access token manually: [ https://xxx.xxx.com/restapi/vc/authentication/sessions/login?user.login=s_user&user.password=xxx]邮递员用于手动获取访问令牌的 URL:[ https://xxx.xxx.com/restapi/vc/authentication/sessions/login?user.login=s_user&user.password=xxx]

Then i need to use this access token in below URL to fetch data: [ https://xxx.xxx.com/restapi/vc/messages/id/879997?restapi.session_key=kEf-mTzu6Xnsa5HQKt8ml-9Wc-HX3wHLlphB2oQkrxE.&restapi.response_format=json]然后我需要在下面的 URL 中使用这个访问令牌来获取数据:[ https://xxx.xxx.com/restapi/vc/messages/id/879997?restapi.session_key=kEf-mTzu6Xnsa5HQKt8ml-9Wc-HX3wHLlphB2oQkrxE.&restapi. response_format=json]

Can anyone help with python code for this任何人都可以为此提供 python 代码帮助吗

Code i am trying for getting access token:我正在尝试获取访问令牌的代码:

    import requests
import json

with requests.Session() as session:
    req = requests.get(r'https://xx.xx.com/restapi/vc/authentication/sessions/login?user.login=s_user&user.password=xxxx')

print(req.content)

Output: b'\\n F0JQfxkxxwl-O9-cQKtJhdrNRKJg3ENLd_SckmBIqU0.\\n\\n'输出:b'\\n F0JQfxkxxwl-O9-cQKtJhdrNRKJg3ENLd_SckmBIqU0。\\n\\n'

Code working for hard coding access token from post man:来自邮递员的用于硬编码访问令牌的代码:

for m in data_list:
    response = requests.get("https://xxx.xx.com/restapi/vc/messages/id/"+m+"?restapi.session_key=kEf-mTzu6Xnsa5HQKt8ml-9Wc-HX3wHLlphB2oQkrxE.&restapi.response_format=json")
    data = response.json()
    cleaned_text=cleanhtml(data['response']['message']['body']['$'])
    body_list.append(cleaned_text)
    #print(cleaned_text)
    #print ('---------BODY ENDS HERE--------')
    tz_subject = data['response']['message']['subject']['$']

When i hard code the access token that i got from post man i am able to fetch results using below code , but i am not getting how to generate access token in python and then use it, refresh it when i expires当我对从邮递员那里获得的访问令牌进行硬编码时,我可以使用以下代码获取结果,但是我不知道如何在 python 中生成访问令牌然后使用它,当我到期时刷新它

Thanks you谢谢

The token data is probably in the req variable of the first snippet (which is actually a response).令牌数据可能在第一个片段(实际上是一个响应)的req变量中。

Usually the response data is in JSON format which can be decoded in a similar way as in the second snippet.通常响应数据是 JSON 格式,可以用与第二个代码段类似的方式解码。 In case of an XML format there are several parsers available, eg xml.etree.ElementTree from the Python standard library.对于 XML 格式,有多种解析器可用,例如 Python 标准库中的xml.etree.ElementTree

Details how the token is saved in the data and how to regenerate a token should be given by the provider of the REST API. REST API 的提供者应提供令牌如何保存在数据中以及如何重新生成令牌的详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM