Created
October 31, 2019 03:38
-
-
Save satomacoto/a63feee4254ae5d2a380c11a71bf335b to your computer and use it in GitHub Desktop.
access api with jwt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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