如何编写无需Postman即可获取Bearer令牌的python脚本?

时间:2019-04-16 21:49:43

标签: python-3.x oauth-2.0

根据邮递员为我生成的内容,我有以下脚本。 该脚本可以正常运行,但我真的不明白为什么。

import requests
import json
import getpass

url = "https://sso-test.example.com/as/token.oauth2"
generic_user = 'foo-oauth.gen'

pw = getpass.getpass('Enter password for %s: ' % generic_user)
payload = "grant_type=password&username=foo-oauth.gen&password=%s" % pw

# This bit was generated by postman.
headers = {
    'Content-Type': "application/x-www-form-urlencoded",
    'Authorization': "Basic WhereInThEHeCkDoeThiscomEFrom",
    'cache-control': "no-cache",
    'Postman-Token': "HowisTh-isgen-480d-8569-c9866544b60"
    }

# Get the Bearer token.
response = requests.request("POST", url, data=payload, headers=headers)

rdict = json.loads(response.text)
access_token = rdict['access_token']

url = "https://tool-001.example.com/api/v1/details/"
payload = ""
headers = {
    'Authorization': "Bearer %s ,Basic WheredoesThisComeFrom" % access_token,
    'X-XSRF-Header': "example.com",
    'cache-control': "no-cache",
    'Postman-Token': "df6f062f-815b-49a1-adc1-IsthiSevenSafetoPost"
    }

# Get data from the API.
response = requests.request("GET", url, data=payload, headers=headers, verify=False)

print(response.text)

我不明白如何生成headers。 没有邮递员怎么写这样的脚本?

0 个答案:

没有答案