Qiita API Oauth with Django

Introduction

There is a convenient API for handling Qiita called Qiita API. https://qiita.com/api/v2/docs

Here is a sample to try using this with Django.

Only Oauth is dealt with in this article (otherwise it's just a request)

Register the application from the following URL. https://qiita.com/settings/applications

You will get a Client ID and a Client Secret.

Around authentication

GET /api/v2/oauth/authorize

Display the authorization screen for the user. If approved by the user, code will be added to the Callback URL specified in the application registration above.

In addition, specify scope and state with parameters.

Regarding the scope, here

state is that you can specify the value included in the query of the URL to redirect for CSRF countermeasures.

python


state = random.randint(1,100000)
request.session['qiita_state'] = state
return redirect(QIITA_OAUTH_URL + '?client_id=' + QIITA_CLIENT_ID + '&state=' + str(state) + '&scope=read_qiita+write_qiita')

Since state is meaningless unless it is a random value, it is randomly generated and saved in the session.

POST /api/v2/access_tokens

Now you have an access token. Use the code you got from GET / api / v2 / oauth / authorize earlier.

At the same time, use the client_id and client_secret obtained by registering the application.

python


@csrf_exempt
def qiita_oauth(request):
    if request.method == 'GET':
        state = request.session['qiita_state']
        if state == int(request.GET.get("state")):
            if "code" in request.GET:
                code = request.GET.get("code")
                body = {
                        'client_id': QIITA_CLIENT_ID,
                        'client_secret': QIITA_CLIENT_SECRET,
                        'code': code
                        }
                header = {
                        'Content-Type': 'application/json',
                        }
                req = urllib.request.Request(QIITA_ACCESSTOKEN_URL, json.dumps(body).encode(), header)
                try:
                    with urllib.request.urlopen(req) as res:
                        #Post-success processing
                except urllib.error.HTTPError as err:
                    #Processing after request failure
                except urllib.error.URLError as err:
                    #Processing after request failure
            else:
                #processing after state authentication failure

ʻIf state == int (request.GET.get ("state")): `checks if the state is correct.

Recommended Posts

Qiita API Oauth with Django
Twitter OAuth with Django
Hit the Twitter API after Oauth authentication with Django
Quine Post with Qiita API (Python)
GraphQL API with graphene_django in Django
CRUD with Django
Try slack OAuth authentication with flask (Slack API V2)
Recent ranking creation using Qiita API with Python
Authenticate Google with Django
Django 1.11 started with Python3.6
Upload files with Django
Output PDF with Django
Markdown output with Django
Use Gentelella with django
Getting Started with Django 1
Send email with Django
File upload with django
Extrude with Fusion360 API
Use LESS with Django
Pooling mechanize with Django
Use MySQL with Django
Start today with Django
Getting Started with Django 2
[First API] Try to get Qiita articles with Python
[Python] Get user information and article information with Qiita API
Do Django with CodeStar (Python3.6.8, Django2.2.9)
Use Trello API with python
Get started with Django! ~ Tutorial ⑤ ~
Minimal website environment with django
Use Twitter API with Python
Do Django with CodeStar (Python3.8, Django2.1.15)
API with Flask + uWSGI + Nginx
Deploy Django serverless with Lambda
Getting Started with Python Django (1)
Create a homepage with django
Get information with zabbix api
Get started with Django! ~ Tutorial ④ ~
Getting Started with Python Django (4)
Web application creation with Django
I touched the Qiita API
Getting Started with Python Django (3)
Combine FastAPI with Django ORM
Get started with Django! ~ Tutorial ⑥ ~
Web API with Python + Falcon
Save tweet data with Django
Play RocketChat with API / Python
Do AES encryption with DJango
[Translated article] Fast API is actually very compatible with Django
Create a web API that can deliver images with Django
Getting Started with Python Django (6)
Support yourself with Twitter API
Call the API with python3.
Combine two images with Django
Getting Started with Django with PyCharm
Use subsonic API with python3
Real-time web with Django Channels
(For beginners) Try creating a simple web API with Django
Double submit suppression with Django
Django REST framework with Vue.js
Successful update_with_media with twitter API
Use prefetch_related conveniently with Django