为了账号安全,请及时绑定邮箱和手机立即绑定

自动生成访问令牌

自动生成访问令牌

翻阅古今 2022-07-05 19:11:12
为了从私有 API 中提取数据,我需要使用我的身份验证密钥和凭据生成访问令牌。我当前的代码分为两部分。第一个生成访问令牌:import requests url = "https://api.abcdef.com/AuthorizationServer/Token" payload = "{\r\n    \"grant_type\" : \"password\",\r\n    \"username\" : \"user@aldfh.com\",\r\n  \"password\" : \"kajshdgfkuyb\",\r\n    \"scope\" : \"API\"\r\n}" headers = {   'Content-Type': 'application/json',   'Authorization': 'Basic VGFibGVhdV9DaW94QFRhYmxlYXVfQ2lveDo0Ix ' } response = requests.request("POST", url, headers=headers, data = payload) print(response.text.encode('utf8'))响应如下所示:    {"access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpY0JVSWQiOjQ1OTg0MjEsIm5hbWUiOiJyYW15YS5nb3RldHlAY2lveGhlYWx0aC5jb20iLCJpc3MiOiJodHRwczovL2FwaS5pbmNvbnRhY3QuY29tIiwic3ViIjoidXNlcjoxNTMyMDI2MiIsImF1ZCI6IlRhYmxlYXVfQ2lveEBUYWJsZWF1X0Npb3giLCJleHAiOjE1Nzk2Mjg1NzcsImlhdCI6MTU3OTYyNDk3OCwiaWNTY29wZSI6IjgiLCJpY0NsdXN0ZXJJZCI6IkMzMSIsImljQWdlbnRJZCI6MTUzMjAyNjIsImljU1BJZCI6MTQ5NiwibmJmIjoxNTc5NjI0OTc4fQ.rEZiMHPsE1inwuWFME1oV_oD54TqkU00-uml3NjCkClW3R-_bVC7A3PxI4zGlJms1rvsZkgO3XX8-1coeV6_jtI-l3nCHixVk2nboepqAspoxT3o9w4vhBhZzvs-TAsqyk4fCrSwwHFXwn8xOMdfrqZqknXHLlVtKlGJg_Uy3bmwEiioocMN3BRZE_269_v5Ez4b94_juUHLPDWye7kS5-8cs4Izsk7HePn-Sm_-FLEqEeb2C09NUGWU8SdyA3EtQhMAiHkU-wN8uQ8wKcWoUfO7WtrSO4zbicFZHgA9Cw","token_type":"bearer","expires_in":3600,"refresh_token":"pDYllH2UsVIYq3Pn3Dg==","scope":"Api","resource_server_base_uri":"https://api-c31.it.com/itAPI/","refresh_token_server_uri":"https://api-c31.it.com/AuthorizationServer/Token","agent_id":162,"team_id":24355,"bus_no":4421}'访问令牌是输出的一部分,我将其粘贴到以下代码中以生成响应:def getPerformance():# api-endpoint #Give the specified url ,accessToken    BASEURL = 'https://api-c31.ict.com/tAPI/'我的问题是我需要能够合并两个脚本才能使该过程自动化。
查看完整描述

2 回答

?
慕运维8079593

TA贡献1876条经验 获得超5个赞

尝试将以下更改添加到代码的第一部分,然后使用底部的访问令牌将其传递给getPerformance()函数:


#Added json import here


import json

import requests



url = "https://api.abcdef.com/AuthorizationServer/Token"


payload = "{\r\n    \"grant_type\" : \"password\",\r\n    \"username\" : \"user@aldfh.com\",\r\n  \"password\" : \"kajshdgfkuyb\",\r\n    \"scope\" : \"API\"\r\n}"


headers = {

     'Content-Type': 'application/json',

     'Authorization': 'Basic VGFibGVhdV9DaW94QFRhYmxlYXVfQ2lveDo0Ix '

     }


response = requests.request("POST", url, headers=headers, data = payload)


#Note the changes here


json = response.read()

data = json.loads(json)



accessToken = data['access_token']

然后,无论您在哪里调用getPerformanceFunction(),都希望将其更改为getPerformance(accessToken)。您也需要将函数定义更改为此。


查看完整回答
反对 回复 2022-07-05
?
慕容森

TA贡献1853条经验 获得超18个赞

根据上面@Cutter 的回复,进行以下更改对我有用:


import requests

import json


 url = "https://api.abcdef.com/AuthorizationServer/Token"


 payload = "{\r\n    \"grant_type\" : \"password\",\r\n    \"username\" : \"user@aldfh.com\",\r\n  \"password\" : \"kajshdgfkuyb\",\r\n    \"scope\" : \"API\"\r\n}"


 headers = {

   'Content-Type': 'application/json',

   'Authorization': 'Basic VGFibGVhdV9DaW94QFRhYmxlYXVfQ2lveDo0Ix '

 }


 response = requests.request("POST", url, headers=headers, data = payload)

 testresp = response.text

 data = json.loads(testresp)

#将函数定义更改为:


def getPerformance(data):


# api-endpoint 

#Give the specified url ,accessToken

# =============================================================================

    BASEURL = 'https://api-c31.ict.com/API/'

    accessToken = (data["access_token"])


查看完整回答
反对 回复 2022-07-05
  • 2 回答
  • 0 关注
  • 141 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信