Github OAuth2 🔑 Guide
Last updated
Was this helpful?
Last updated
Was this helpful?
The GitHub
provider lets you sign in 🔓 users using their GitHub accounts through OAuth2.
To use GitHub OAuth2 in your app, you need to set up a GitHub App. Here's a step-by-step guide to obtain the necessary credentials (client_id
, client_secret
, redirect_uri
) and configure your app.
Log in with your GitHub account or create one if you don't have it.
Go to GitHub Developer Settings.
Click on New OAuth app.
Enter App name and Authorization callback URL
Press Register
Copy the Client_ID
Press Generate a new client secret
scope='user,repo'
will be enough for authentication as it returns really a lot of data for you to work with.⚠️ Note: It's best to store your GitHub App settings in a
.env
file for 🔐 security. Add the following to your.env
file:
Use the python-dotenv
package to load these variables in your Django project.
First, import the needed 📦 class and set up your GitHub App ⚙️ settings:
⚠️ Note: Make sure that your
redirect_uri
matches the callback URL you set in your GitHub OAUTH2 app settings and in your Django URLs.
This method creates the link 🔗 you need to send the user to so they can log in using Facebook.
Parameters:
state
(str, optional): A random string 🔀 to protect against cross-site request forgery attacks.
Returns:
str
: The URL 🌐 to use for GitHub login.
Example:
This method uses the code from GitHub to get an access token 🔑.
Parameters:
code
(str): The authorization code 🔢 you got from the callback URL.
Returns:
str
: The access token 🔑.
Example:
This method gets the user's profile information from Facebook.
Parameters:
access_token
(str): The access token 🔑 you got from get_access_token
.
Returns:
dict
: The user's profile information 📋.
Example:
For a comprehensive list of user profile fields and the necessary permissions, refer to the GitHub Scope Reference
Example:
🔒 Use Environment Variables: Always use environment variables to store important information like client_id
and client_secret
. This helps keep your credentials safe 🛡️.
🔗 Match Redirect URI: Make sure the redirect_uri
is the same in both your GitHub App settings and your code to avoid errors 🚫 during the login process.
⚠️ Error Handling: Handle any possible errors 🐞 during the login and token exchange process to ensure a smooth user experience 😊.
Now you're ready to use GitHub for authenticating users in your app 🚀. Follow these steps and best practices to make sure everything runs securely 🔐 and smoothly ✨.