Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

oauth flow in github api v3 not working

So this should be pretty simple, but I can't seem to find my fail point. Hopefully someone else can point it out to me.

First I go to https://github.com/login/oauth/authorize?client_id=CLIENT_ID&scope=gist and this returns a code to me. Then I do this:

import requests, json

client_id = XXXX
client_secret = XXXX
code = XXXX

r = requests.post(
    'https://github.com/login/oauth/access_token', 
    data=json.dumps({
        'client_id':client_id, 
        'client_secret':client_secret,
        'code':code
    })
r.content  # this gives me a 404 header

When I go to my test user it shows me as authorized and my app shows as having one user, but I don't have a access token.

What am I doing wrong.

like image 994
Daniel Nill Avatar asked Dec 17 '22 01:12

Daniel Nill


1 Answers

Well I was right this was a really simple problem, but I'll leave this here incase others run into the same error.

When in doubt manually define your header. So you need:

header = {'content-type':'application/json'}

And then pass in the header:

r = requests.post(
    'https://github.com/login/oauth/access_token', 
    data=json.dumps({
        'client_id':client_id, 
        'client_secret':client_secret,
        'code':code
    }),
    headers=header
)

For me this solved the problem.

like image 145
Daniel Nill Avatar answered Jan 03 '23 17:01

Daniel Nill