ã¯ããã«
ãã«ãã¤ã®ããï¼ nikkieã§ãã
éå»ã«è§¦ã£ãdjoserã®æãåºãã§ãã
ç®æ¬¡
- ã¯ããã«
- ç®æ¬¡
- PyCon JP 2021 ã¹ã¿ããæ´»åã®ä¸ã§ä½ã£ãDjangoã¢ããª
- djoserã®Sample usage
- ãµã³ãã«ã¢ããªã®è¨å®ãè¦ã
- çµããã«
PyCon JP 2021 ã¹ã¿ããæ´»åã®ä¸ã§ä½ã£ãDjangoã¢ããª
ãããã¼ã¶ã«ã¬ãã¥ã¼ã¢ããªãDjangoã§ä½ãã¾ããã
ã¬ãã¥ã¼æ©è½ã¯Webã¢ããªã¨ãã¦ä½ã£ãã®ã§ããã管çè
ç¨ã®ãã¼ã¿ç®¡çAPIãDjango REST Frameworkã§å®è£
ãã¦ãã¾ã1ã
akiyokoããã®ãç¾å ´ã§ä½¿ãã Django REST Framework ã®æç§æ¸ãã«ãã¡ããã¡ãå©ãã¦ããã ãã¾ããã
ãã¼ã¿ç®¡çAPIã¯ç®¡çè ããæ±ããªããããdjoserã使ã£ã¦èªè¨¼ãå®è£ ãã¾ãã2ã
ãããã£ã¦ã©ããã£ããã ã£ããã¨æãåºããããä»åæãåããã¾ããã
djoserã®Sample usage
ãã¡ãã«æ²¿ã£ã¦é²ãã¦ããã¾ãã
ãµã³ãã«ã¢ããªãåãã
djoserã®ãªãã¸ããªã«ã¯ãµã³ãã«ã¢ããªãããã¾ãã
ã¾ãã¯ãªãã¸ããªãã¯ãã¼ã³
% git clone [email protected]:sunscrapers/djoser.git -b 2.2.2 % cd djoser
Python 3.11.8ã§ä»®æ³ç°å¢ãä½ã£ã¦ãã¾ãã
% pip install -e . % pip install 'webauthn<1'
annotated-types==0.6.0 asgiref==3.8.1 asn1crypto==1.5.1 cbor2==5.6.3 certifi==2024.2.2 cffi==1.16.0 charset-normalizer==3.3.2 cryptography==42.0.7 defusedxml==0.8.0rc2 Django==5.0.6 django-templated-mail==1.1.1 djangorestframework==3.15.1 djangorestframework-simplejwt==5.3.1 -e git+ssh://[email protected]/sunscrapers/djoser.git@5842524091a00ffbaecc667605da8135a8a60d0d#egg=djoser future==1.0.0 idna==3.7 oauthlib==3.2.2 pycparser==2.22 pydantic==2.7.1 pydantic_core==2.18.2 PyJWT==2.8.0 pyOpenSSL==24.1.0 python3-openid==3.2.0 requests==2.31.0 requests-oauthlib==2.0.0 six==1.16.0 social-auth-app-django==5.4.1 social-auth-core==4.5.4 sqlparse==0.5.0 typing_extensions==4.11.0 urllib3==2.2.1 webauthn==0.4.7
manage.pyãå®è¡ãã¾ãã
% cd testproject % ./manage.py migrate % ./manage.py runserver 8088
models.W042
ãåºã¦ãã¾ããï¼Djangoã®ãã¼ã¸ã§ã³ã¢ããã®å½±é¿ãªã®ããªï¼ï¼
Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
HINT: Configure the DEFAULT_AUTO_FIELD setting or the AppConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
ä»åã¯ç¡è¦ãã¦é²ãã¦ãã¾ã
ãã¼ã¯ã³èªè¨¼ãä½é¨ãã
å¥ã®ã¿ã¼ããã«ã«ã¦
ã¦ã¼ã¶ãä½ãã¾ãã
ï¼curl
ã§ã¯--data
ãæå®ããã¨ãPOSTã«ãªãã®ã§-X POST
ã¯çç¥ï¼
% curl http://127.0.0.1:8088/auth/users/ --data 'username=djoser&password=alpine12' {"email":"","username":"djoser","id":1}
APIãä»ãã¦ãã°ã¤ã³ãã¾ãã
èªè¨¼ã«ä½¿ããã¼ã¯ã³ãè¿ãã¾ã
% curl http://127.0.0.1:8088/auth/token/login/ --data 'username=djoser&password=alpine12' {"auth_token":"0d0bca572490495255f1f626d96bfb61ffaba86a"}
ãªã¯ã¨ã¹ããããã«ãã¼ã¯ã³ãå«ãã¦ãèªåã®æ å ±ã確èª
% curl -L http://127.0.0.1:8088/auth/users/me/ -H 'Authorization: Token 0d0bca572490495255f1f626d96bfb61ffaba86a' {"email":"","id":1,"username":"djoser"}
ãã°ã¢ã¦ã
% curl http://127.0.0.1:8088/auth/token/logout/ --data '0d0bca572490495255f1f626d96bfb61ffaba86a' -H 'Authorization: Token 0d0bca572490495255f1f626d96bfb61ffaba86a'
ãã¼ã¯ã³ã使ããªããªãã¾ããã
% curl -L http://127.0.0.1:8088/auth/users/me/ -H 'Authorization: Token 0d0bca572490495255f1f626d96bfb61ffaba86a' {"detail":"Invalid token."}
ãµã³ãã«ã¢ããªã®è¨å®ãè¦ã
ããã¸ã§ã¯ãã®urls.pyãè¦ãã¨ãè²ã
æå¹ã«ãªã£ã¦ãã¾ãã
https://github.com/sunscrapers/djoser/blob/2.2.2/testproject/urls.py
djoser.social.urls
ãªã©ãæå¹ã«ãªã£ã¦ããã£ã½ãï¼
settingsãè¦ãã¨
https://github.com/sunscrapers/djoser/blob/2.2.2/testproject/settings.py
INSTALLED_APPS
ã«"social_django"
AUTHENTICATION_BACKENDS
ã«FacebookãGoogleãSteamã®æå®- ãããã®è¨å®å¤ï¼ä¾ï¼
SOCIAL_AUTH_FACEBOOK_KEY
ï¼
- ãããã®è¨å®å¤ï¼ä¾ï¼
ã¨ãã½ã¼ã·ã£ã«ãã°ã¤ã³ãè¨å®ã§ããé°å²æ°ã§ãï¼å®¿é¡äºé ï¼
çµããã«
éå»ã«ä½¿ã£ãdjoserã®æãåºãã§ããã
ç¨æããã¦ãããµã³ãã«ã¢ããªãåããã¾ããã
ãã¼ã¯ã³èªè¨¼ãæãåºãã¾ããï¼