0

So i'm writing a program that post's data to a url and get's the response. In postman it requires a token. So when I tried to make it in python it's giving me a response [401].

The problem I have is trying to get the token first and then passing it to my post_data method.

I'm going to put *** by the URL and username and password for privacy concerns.

import requests
import json
import pprint
import urllib

def get_token():
    tokenurl='***'
    data={
            'grant_type':'password',
            'username':'***',
            'password':'***'
            }
    token=requests.post(tokenurl,data=data)
    print(token)    
get_token()

def post_data():
    url1='***'
    data={"***"
      }

    data_json = json.dumps(data)
    headers = {'Content-type': 'application/json'}
    response = requests.post(url, data=data_json, headers=headers)
    pprint.pprint(response.json())

4

1 回答 1

1

在 post_data() 函数中,您可以将生成的令牌添加到标头

headers = {'Content-type': 'application/json','Authorization': 'token ***'}

*** 是您生成的令牌

于 2019-05-09T17:35:29.683 回答