Hello, I would like to ask you if anybody try to create a python library or robot keywords for Authorization code with PKCE (Oauth2.0) . At the moment I tried to develop something in python, but it doesn’t work.
`from http.server import BaseHTTPRequestHandler, HTTPServer
from lib2to3.pgen2 import token
from urllib import parse
import random
import string
import hashlib
import base64
import json
from typing import Any
import webbrowser
import requests
from oauthlib.oauth2 import WebApplicationClient
from IPython.display import clear_output
def generate_code() → tuple[str, str]:
rand = random.SystemRandom()
code_verifier = ‘’.join(rand.choices(string.ascii_letters + string.digits, k=128))
code_sha_256 = hashlib.sha256(code_verifier.encode('utf-8')).digest()
b64 = base64.urlsafe_b64encode(code_sha_256)
code_challenge = b64.decode('utf-8').replace('=', '')
return (code_verifier, code_challenge)
def access_token(clientId, redirectUri, authUri, tokenUri, scope):
client = WebApplicationClient(clientId)
code_verifier, code_challenge = generate_code()
auth = client.prepare_request_uri(authUri, scope=scope, redirect_uri=redirectUri, code_challenge= code_challenge, code_challenge_method = "S256" )
# print(auth_uri)
webbrowser.open_new(auth)
print(lol.headers["location"])
data = {
"code": 123,
"client_id": clientId,
"grant_type": "authorization_code",
"scope": scope,
"redirect_uri": redirectUri,
"code_verifier": code_verifier,
}
response = requests.post(tokenUri, data=data, verify=False)
print(response)
return response
# access_token = response.json()["access_token"]
# clear_output()
# print("Logged in successfully")
# headers = { "Authorization": "Bearer " + access_token }
# print(headers)
# response = requests.get("https://avdonl0t0callcenter0fe.z6.web.core.windows.net/search/Purchases?query=1", headers=headers, verify=False)
# print(response)
# print(json.dumps(response.json(), indent=4))
print(access_token(
“Test”,
“ww.w.xxxxxxxx.windows.net”,
“ww.w.xxxxxxxxx/connect/authorize”,
“ww.w…xxxxxxxxx/connect/token”,
“scope”))`