自动生成访问令牌

为了从私有 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/'

我的问题是我需要能够合并两个脚本才能使该过程自动化。


翻阅古今
浏览 144回答 2
2回答

慕运维8079593

尝试将以下更改添加到代码的第一部分,然后使用底部的访问令牌将其传递给getPerformance()函数:#Added json import hereimport jsonimport requestsurl = "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 herejson = response.read()data = json.loads(json)accessToken = data['access_token']然后,无论您在哪里调用getPerformanceFunction(),都希望将其更改为getPerformance(accessToken)。您也需要将函数定义更改为此。

慕容森

根据上面@Cutter 的回复,进行以下更改对我有用:import requestsimport 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"])
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python