[PDB Tech] OAuth client application help request: cannot get user profile
Bartosz Miklaszewski
bartosz at miklaszewski.com
Mon Jun 26 14:43:37 PDT 2023
well, 5 min later I have figure it out, that I was requesting only email in authorize url, and asking for profile and email, so by adding email+profile in auth url fixed the problem ! ?
Bart
________________________________
From: Pdb-tech <pdb-tech-bounces at lists.peeringdb.com> on behalf of Bartosz Miklaszewski <bartosz at miklaszewski.com>
Sent: 26 June 2023 22:36
To: pdb-tech at lists.peeringdb.com <pdb-tech at lists.peeringdb.com>
Subject: [PDB Tech] OAuth client application help request: cannot get user profile
Hi,
I am struggling with this for a while, maybe someone would be abloe to help.
In short, i try use flask with oauth but failed on getting token back, moved to django, and I was able to pass token.
Now I am receiving token and try to request user profile, but getting Resposne 403
cant figure out why,
My code:
from django.shortcuts import render
from django.http import HttpRequest, HttpResponse, JsonResponse
from django.shortcuts import redirect
import requests
# Create your views here.
redirect_url_peeringdb = 'https://auth.peeringdb.com/oauth2/authorize/auth?response_type=code&client_id=<client_id>&redirect_uri=https://localhost:8000/oauth2/login/redirect&scope=email&state=1234zyx'
client_id = "<client_id>"
client_secret = "<client_secret>"
def home(request: HttpRequest) -> JsonResponse:
return JsonResponse({ "msg": "Hello World" })
def peeringdb_login(request: HttpRequest):
return redirect(redirect_url_peeringdb)
def peeringdb_login_redirect(request: HttpRequest):
code = request.GET.get('code')
print("### 1 ### code: ", code)
user = exchange_code(code)
print("### 2 ### email: ", user)
return JsonResponse({ "user": user})
def exchange_code(code: str):
print("### 3 ### code: ", code)
data = {
"client_id": "<client_id>",
"client_secret": "<client_secret>",
"grant_type": "authorization_code",
"code": code,
"redirect_uri": "https://localhost:8000/oauth2/login/redirect",
"scope": "email"
}
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
response = requests.post("https://auth.peeringdb.com/oauth2/token/", data=data, headers=headers)
print("### 4 ### response: ", response)
print("### 5 ### response.json: ", response.json())
credentials = response.json()
print("### 6 ### credentials: ", credentials)
access_token = credentials['access_token']
print("### 7 ### access_token: ", access_token)
response1 = requests.get("https://auth.peeringdb.com/profile/v1", headers={
"Authorization": "Bearer %s" % access_token
})
print("### 8 ### response1: ", response1)
print("### 9 ### response1: ", response1.json())
user = response1.json()
print("### 10 ### user: ", user)
i am getting error:
127.0.0.1 - - [26/Jun/2023 21:21:16] "GET /oauth2/login HTTP/1.1" 302 -
### 1 ### code: <code>
### 3 ### code: <code>
### 4 ### response: <Response [200]>
### 5 ### response.json: {'access_token': '<access_token>', 'expires_in': 36000, 'token_type': 'Bearer', 'scope': 'email', 'refresh_token': '<refresh_token>'}
### 6 ### credentials: {'access_token': '<access_token>', 'expires_in': 36000, 'token_type': 'Bearer', 'scope': 'email', 'refresh_token': '<refresh_token>'}
### 7 ### access_token: <access_token>
### 8 ### response1: <Response [403]>
127.0.0.1 - - [26/Jun/2023 21:21:17] "GET /oauth2/login/redirect?code=<code>&state=1234zyx HTTP/1.1" 500 -
Traceback (most recent call last):
File "/Users/miklab01/Library/Python/3.9/lib/python/site-packages/requests/models.py", line 971, in json
return complexjson.loads(self.text, **kwargs)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
any one have idea where i am missing something?
i think issue might be with the url i send and autorization... ?
https://auth.peeringdb.com/profile/v1/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.peeringdb.com/pipermail/pdb-tech/attachments/20230626/4504f2e9/attachment-0001.htm>
More information about the Pdb-tech
mailing list