Skip to content

Instantly share code, notes, and snippets.

@satomacoto
Created October 31, 2019 03:38
Show Gist options
  • Save satomacoto/a63feee4254ae5d2a380c11a71bf335b to your computer and use it in GitHub Desktop.
Save satomacoto/a63feee4254ae5d2a380c11a71bf335b to your computer and use it in GitHub Desktop.
access api with jwt
import json
from urllib.request import urlopen
from urllib.request import Request
from urllib.parse import urlencode
# set username, password, auth_url, api_url
params = urlencode({"username": username, "password": password}).encode()
# get token
request = Request(auth_url, params)
with urlopen(request) as response:
token = json.loads(response.read())["token"]
print(token)
# access
request = Request(api_url, None, {
'Authorization': "JWT %s" % token
})
response = urlopen(request).read()
print(response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment