Tasneem Haider
Tasneem Haider

Reputation: 379

How to get access token using oauth2 for Github api using python

I am building an app to access Github api in python using django. I am new to this building this kind of app for the first time. I specified an link to get the access of a user's account like this

<a href="https://github.com/login/oauth/authorize?client_id=something&scope=something,something"></a>

Now I can access the user's account. Now the problem is, I want to use oauth2 so that it doesn't ask for username and password for each request. my question is how to get the access_token and where to store it. How to pass post parameter of POST https://github.com/login/oauth/access_token such as client_id, scope, code in my django view. And how to link different access for different user. Please help me. I am having problem in getting the basics right.

Upvotes: 2

Views: 1152

Answers (1)

ceaess
ceaess

Reputation: 68

I recommend using Python Social Auth and then leveraging their Github backend after setting it up to work with your django app.

So, you would roughly be able to pass the access token using something like:

import requests

user = User.objects.get(...)
social = user.social_auth.get(provider='github')
response = requests.get(
   'https://github.com/login/oauth/authorize? 
    client_id=something&scope=something,something',
    params={'access_token': social.extra_data['access_token']}
)
retrieved_data = response.json()['items']

You can also make the token available in your template if you'd like to continue using a link. And, you don't need to pass the scope as a query parameter, certainly, Python Social Auth can also handle that for you

Upvotes: 3

Related Questions