Please take a look at the code below:
@login_required
def dashboard(request):
code = request.GET.get('code', '')
payload = {
"client_id" : settings.GITHUB_CLIENT_ID,
"client_secret" : settings.GITHUB_CLIENT_SECRET,
"code" : code,
"state" : settings.STATE,
}
response = requests.post('https://github.com/login/oauth/access_token', params=payload)
# final_response = requests.get('https://api.github.com/user', auth=GitHubTokenAuth(access_token))
return HttpResponse(response)
# authenticated_user = final_response.json()
# return render(request, 'core/dashboard.html', {'authenticated_user':authenticated_user})
The response
variable is returning values similar to:
access_token=eiwfbvdsvefieebrferferwfreferfersfwrb&scope=a%20list%20of%20scopes&token_type=bearer
How do I access the value of access_token
so I can use as seen in the value of the commented out final_response
variable?
Thanks in anticipation!
You can use the parse_qs
method of urllib
:
>>> from urllib.parse import parse_qs
>>> parse_qs(response.text)
{'access_token': ['eiwfbvdsvefieebrferferwfreferfersfwrb'], 'scope': ['a list of scopes'], 'token_type': ['bearer']}
Note that you will get a list
for each key. To access the first element, use the following syntax:
>>> result = parse_qs(response.text)
>>> result['access_token'][0]
'eiwfbvdsvefieebrferferwfreferfersfwrb'