avatarJen-Hsuan Hsieh (Sean)

Summary

This article provides a step-by-step guide on how to exchange Github's access token to JWT from a Django/DRF server for social login.

Abstract

The article begins by introducing the concept of exchanging access tokens for JWT from a Django/DRF server, which was previously covered for Facebook. The focus of this article is on Github, another popular platform for social login. The author outlines the agenda, which includes an overview, getting authorized code from Github, getting access token from Github, setting up the Django/DRF server, and validation with Postman. The article also provides context by mentioning that it is part of a series on building a ReactJS single page application with a Django API server and deploying it on Heroku. The author explains the roles of the Django server, ReactJS client, Github authorize server, and the user in the process. The article then provides detailed steps on how to create a new OAuth app from Github, retrieve the access token from Github, and exchange the access token to JWT. The author also provides code snippets and screenshots to illustrate the process.

Bullet points

  • The article provides a step-by-step guide on how to exchange Github's access token to JWT from a Django/DRF server for social login.
  • The author outlines the agenda, which includes an overview, getting authorized code from Github, getting access token from Github, setting up the Django/DRF server, and validation with Postman.
  • The article provides context by mentioning that it is part of a series on building a ReactJS single page application with a Django API server and deploying it on Heroku.
  • The author explains the roles of the Django server, ReactJS client, Github authorize server, and the user in the process.
  • The article provides detailed steps on how to create a new OAuth app from Github, retrieve the access token from Github, and exchange the access token to JWT.
  • The author provides code snippets and screenshots to illustrate the process.

Build Single page application with React and Django Part 5.2-Exchange Github’s access token to JWT from Django/DRF server

Introduction

We have introduced how to get the access token from Facebook and exchange to JWT in the the previous article.

Github is also a popular platform for social login. The target of this article is to understand how to get access token from Github. Then we want to exchange the access token to JWT from our local Django server.

Agenda

It includes the following topics.

  • Overview
  • Get authorized code form Github
  • Get access token from Github
  • Set Django/DRF server
  • Validation with Postman

About this series

The target of this series is to build a ReactJS single page application(SPA) with Django API server and deploy on Heroku.

Overview

There are four roles, Django server, ReactJS client, Github authorize server, and the user.

Copy right@A Layman

1.Get Authorized code form Github

Create a new OAuth apps from Github

  1. Login Github. Select Settings -> Developer settings -> OAuth Apps -> New OAuth App
  2. Copy the Client ID and Client Secret from the page
Copy right@A Layman

Create a ReactJS to retrieve the access token from Facebook

1.Install required packages

npm install react-github-login

2.Create a ReactJS component

import React from 'react'
import GitHubLogin from 'react-github-login';
import FacebookLogin from 'react-facebook-login';
const SignInForm = () => {
    const ResponseGithubOnSuccess = (response) => {
        console.log(response);
      }
const ResponseGithubOnFailure = (response) => {
        console.log(response);
      }
    
    return (
       <GitHubLogin
            clientId="51b1a8ee5b7cad1e6a85"
            redirectUri="http://localhost:3000/signin"
            onSuccess={ResponseGithubOnSuccess}
            onFailure={ResponseGithubOnFailure}/>
    )
}

3.We can use the component to get the authorized code from Facebook

Copy right@A Layman

2.Get Access token form Github

The next step is to get the access token from Github with the code, Client ID, and Client Secret. We can create an endpoint for the ReactJS component.

1.Add a new class in the views.py

2.Edit urls.py

from .views import social_auth_github
...
urlpatterns = [
    path("api/get-github-access-token/", social_auth_github)
]

3.Exchange the access token to JWT

1.Install required packages (same as Exchange Facebook’s access token to JWT from Django/DRF server for Social Login)

pip install python-social-auth
pip install social-auth-app-django
pip install social-auth-core

2.Edit settings.py

INSTALLED_APPS = [
    ...
    'social.apps.django_app.default',
    'social_django'
]
AUTHENTICATION_BACKENDS = [
    'social_core.backends.github.GithubOAuth2',
    'django.contrib.auth.backends.ModelBackend',
]
SOCIAL_AUTH_GITHUB_KEY = os.getenv("SOCIAL_AUTH_GITHUB_KEY")
SOCIAL_AUTH_GITHUB_SECRET = os.getenv("SOCIAL_AUTH_GITHUB_SECRET")
SOCIAL_AUTH_GITHUB_SCOPE = ['email']
GITHUB_CALLBACK='http://localhost:3000/signin'
SOCIAL_AUTH_GITHUB_USE_OPENID_AS_USERNAME = True
MIDDLEWARE = [
    ...
    'social_django.middleware.SocialAuthExceptionMiddleware'
]

3.Edit serializers.py (same as Exchange Facebook’s access token to JWT from Django/DRF server for Social Login)

class SocialAuthSerializer(serializers.Serializer):
    provider = serializers.CharField(max_length=255, required=True)
    access_token = serializers.CharField(max_length=4096, required=True, trim_whitespace=True)

4.Edit views.py (same as Exchange Facebook’s access token to JWT from Django/DRF server for Social Login)

5.Edit urls.py (same as Exchange Facebook’s access token to JWT from Django/DRF server for Social Login)

from .views import social_authurlpatterns = [
    ...
    path("api/social-auth/", social_auth)
]

4. Validation with Postman

  • Set provider as facebook on the body
  • Set Content-type as application/json on the header
Copy right@A Layman

Then we will get JWT!

References

Summary

Thanks for your patient. I am Sean. I work as a software engineer.

This article is my note. Please feel free to give me advice if any mistakes. I am looking forward to your feedback.

Please feel free to clap if this article can help you. Thank you.

You can also subscribe my page on Facebook.

Related topics

How to use the two-way binding in Knout.js and ReactJS?

Learn how to use SignalR to build a chatroom application

My reflection of :

IT & Network:

Database:

Software testing:

Debugging:

DevOps:

Software Development
Python
React
Oauth
Github
Recommended from ReadMedium