Reputation: 703
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?
Upvotes: 0
Views: 228
Reputation: 703
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!
Upvotes: 2