0

I'm working on a django project and I'm trying to implement Github login using username and password.

Here's the content of my views.py file:

@login_required
def github_access_via_username(request):
    if request.method == 'POST':
        form = GitHubUserPassAuthForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')

            payload = {
                "scopes" : [
                    "repo", "admin:org", "admin:public_key", "admin:repo_hook", "admin:org_hook",
                    "gist", "notifications", "user", "delete_repo", "write:discussion", "admin:gpg_key",
                ],
                "note" : "Permissions",
            }
            response = requests.post('https://api.github.com/authorizations', params=payload, auth=(username, password))
            #json_response = response.json()
            #access_token = json_response['token']
            #user = Github(access_token)
            return HttpResponse(response.status_code)
    else:
        form = GitHubUserPassAuthForm()
    return render(request, 'core/github_login.html', {'form':form})

Here's the output of print(reponse)

{'message': 'Problems parsing JSON', 'documentation_url': 'https://developer.github.com/v3/oauth_authorizations/#create-a-new-authorization'}
[23/Sep/2018 19:55:42] "POST /core/github_access_via_username/ HTTP/1.1" 200 5020

I wasn't getting anything so I decided to return the status_code, and it returned a 400. I'm stuck. Any eye opener please?

6
  • @stovfl Done! Please check it out Commented Sep 23, 2018 at 20:08
  • Message from github says:problems parsing json. Maybe try ˋjson.dumps(payload)ˋ to encode the dict to json explicitly. Commented Sep 23, 2018 at 20:28
  • Possible duplicate of Using python requests module to create an authenticated session in Github Commented Sep 23, 2018 at 20:35
  • Related: PyGithub and python-oauth2 Commented Sep 23, 2018 at 20:45
  • @klaas I tried that already (after I posted the question), but response['message'] said Validation Falied Commented Sep 23, 2018 at 22:48

1 Answer 1

2

I figured it out! According to the documentation at GitHub, you need your client id and client secret keys to get or create new authorizations. So I modified the payload dict and response variable as follows:

...
payload = {
    ...            
    "client_secret" : settings.GITHUB_CLIENT_SECRET,
}
response = requests.put('https://api.github.com/authorizations/clients/{}'.format(settings.GITHUB_CLIENT_ID), auth=(username, password), data=json.dumps(payload))
...

...and that worked!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.