0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Django の JWT クライアント

Last updated at Posted at 2021-11-23

Django の JWT クライアントです。
Django のサーバーはこちらのものを使いました。
ジャンゴ(django)でJWTを使う方法
django-rest-framework-jwt を使っています。

Django のバージョン

$ django-admin --version
3.1.4

token を与えないでエラーが出ることを確認#

go_check.sh
http http://localhost:8000/api/blog/posts/

実行結果

$ ./go_check.sh 
HTTP/1.1 401 Unauthorized
Allow: GET, OPTIONS
Content-Length: 58
Content-Type: application/json
Date: Tue, 23 Nov 2021 09:03:33 GMT
Referrer-Policy: same-origin
Server: WSGIServer/0.2 CPython/3.9.7
Vary: Accept
WWW-Authenticate: JWT realm="api"
X-Content-Type-Options: nosniff
X-Frame-Options: DENY

{
    "detail": "Authentication credentials were not provided."
}

token の取得#

get_token.sh
http POST http://localhost:8000/api/token/ username=scott password=tiger123

実行結果

$ ./get_token.sh 
HTTP/1.1 200 OK
Allow: POST, OPTIONS
Content-Length: 221
Content-Type: application/json
Date: Tue, 23 Nov 2021 09:06:40 GMT
Referrer-Policy: same-origin
Server: WSGIServer/0.2 CPython/3.9.7
Vary: Accept
X-Content-Type-Options: nosniff
X-Frame-Options: DENY

{
    "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VybmFtZSI6InVjaGlkYSIsImV4cCI6MTYzODI2MzIwMCwiZW1haWwiOiJ0ZXN0QHRlc3QuY29tIiwib3JpZ19pYXQiOjE2Mzc2NTg0MDB9.XdmpSw16wwMTlhRFD_sRNkDzubGp4L9pEkC0bxDfWLs"
}

Python で取得

.env
url_base='http://localhost:8000/api/'
username='scott'
password='tiger123'
get_token.py
# ! /usr/bin/python
#
#
#   get_token.py
#
#		                Nov/23/2021
#
# ------------------------------------------------------------------
import  sys
import  os
import  json
import  requests
from dotenv import load_dotenv
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
dotenv_path = '.env'
load_dotenv(dotenv_path)
url_base = os.environ.get("url_base")
username = os.environ.get("username")
password = os.environ.get("password")

url= url_base + "token/"
args={}
args['username'] = username
args['password'] = password
#
rr=requests.post(url,args)
print(rr.text)
#
dict_data = json.loads(rr.text)
#
print("token:\t" + dict_data['token'])
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

実行結果

$ ./get_token.py 
*** 開始 ***
{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VybmFtZSI6InVjaGlkYSIsImV4cCI6MTYzODI2MzMxOSwiZW1haWwiOiJ0ZXN0QHRlc3QuY29tIiwib3JpZ19pYXQiOjE2Mzc2NTg1MTl9.6qwtmvfMAJV8TiEMltCTMqg2eqCs6G2hYZ4WVBVUdHc"}
token:	eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VybmFtZSI6InVjaGlkYSIsImV4cCI6MTYzODI2MzMxOSwiZW1haWwiOiJ0ZXN0QHRlc3QuY29tIiwib3JpZ19pYXQiOjE2Mzc2NTg1MTl9.6qwtmvfMAJV8TiEMltCTMqg2eqCs6G2hYZ4WVBVUdHc
*** 終了 ***

API を使う#

get_blog.py
# ! /usr/bin/python
#
#
#   get_blog.py
#
#		                Nov/23/2021
#
# ------------------------------------------------------------------
import  os
import  sys
import  json
import  requests
from dotenv import load_dotenv
# ------------------------------------------------------------------
def get_token_proc(url_base,username,password):
#
    url=url_base + "token/"
    args={}
    args['username'] = username
    args['password'] = password
#
    rr=requests.post(url,args)
#
    dict_data = json.loads(rr.text)
#
    return dict_data['token']
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")

# ------------------------------------------------------------------
#
dotenv_path = '.env'
load_dotenv(dotenv_path)
url_base = os.environ.get("url_base")
username = os.environ.get("username")
password = os.environ.get("password")
#
token = get_token_proc(url_base,username,password)
#
url=url_base + "blog/posts/"
args={}
headers = {}
headers['Authorization'] = 'JWT {}'.format(token)
headers['Content-Type'] = "application/json"
rr=requests.get(url,headers=headers)
array_data = json.loads(rr.text)

for unit_aa in array_data:
	print(unit_aa)
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

実行結果

$ ./get_blog.py 
*** 開始 ***
{'model': 'blog.post', 'pk': 1, 'fields': {'author': 1, 'title': 'test2', 'content': 'test2', 'created_at': '2019-06-04T15:43:29.899', 'updated_at': '2019-06-04T15:43:29.899', 'published_at': '2019-06-04T15:43:28'}}
{'model': 'blog.post', 'pk': 2, 'fields': {'author': 1, 'title': 'test1', 'content': 'test1\r\n\r\ntest1', 'created_at': '2019-06-04T15:43:19.760', 'updated_at': '2019-06-04T15:43:19.760', 'published_at': '2019-06-04T15:43:18'}}
*** 終了 ***
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?