1

I am looking for a way to do this with a Python script:

curl -X POST -d '{"username": "user","password": "password","scopes": ["download"]}' https://api.fakedomain.com/v2.0/oauth/token

When I execute this from a linux command line, it outputs a client access token. I am trying to make this work through a Python script. I have tried the following:

import requests

url = 'https://api.fakedomain.com/v2.0/oauth/token'
creds = {"username": "<user>",
     "password": "<password>",
     "scopes": ["download"]}
r = requests.post(url, data=creds)
print(r.text)

The script completes with exit code 0, but I can't find the access token anywhere. Any ideas would be greatly appreciated.

GED125
  • 476
  • 4
  • 18

2 Answers2

1

Have you tried the json keyword instead of data?

My requests code looks like this:

import requests
url = 'https://fake-website.com/token'
data = {'key': 'val'}
requests.post(url, json=data)
petezurich
  • 9,280
  • 9
  • 43
  • 57
  • You nailed it! That worked, thank you so much! I have to wait a few minutes to award it to you. You're too fast! – GED125 Feb 08 '20 at 22:04
0

For conversions of CURL commands to Python code you also can use this web app.

It generates:

import requests
data = '{"username": "user","password": "password","scopes": ["download"]}'
response = requests.post('https://api.fakedomain.com/v2.0/oauth/token', data=data)
petezurich
  • 9,280
  • 9
  • 43
  • 57