Django の JWT クライアント

Django の JWT クライアントです。
Django のサーバーはこちらのものを使いました。
[ジャンゴ(django)でJWTを使う方法](https://dev-yakuza.posstree.com/django/jwt/)
[django-rest-framework-jwt](https://github.com/jpadilla/django-rest-framework-jwt/) を使っています。

Django のバージョン

```text
$ django-admin --version
3.1.4
```

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

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

実行結果

```text
$ ./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 の取得#

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

実行結果

```text
$ ./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 で取得

```text:.env
url_base='http://localhost:8000/api/'
username='scott'
password='tiger123'
```

```py: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")
# ------------------------------------------------------------------
```

実行結果

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

#API を使う#

```py: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")
# ------------------------------------------------------------------
```

実行結果

```text
$ ./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'}}
*** 終了 ***
```