Error: Invalid_Client using python

I'm trying to send authentication request for apple using Python, but always giving Error: Invalid_Client

here's my code :
Code Block
def login_with_apple(self, code):
client_secret = generate_apple_client_secret()
apple_url = "https://appleid.apple.com/auth/token"
header = {'content-type': 'application/x-www-form- urlencoded'}
data = 'client_id=' + settings.CLIENT_ID + '&client_secret=' + client_secret + '&code=' + code + '&grand_type=authorization_code'
data = urllib.quote(data)
resp = requests.post(apple_url, data=data, headers=header)
access_token = None
if resp.status_code in [200, 201]:
try:
access_token = resp.json()
except ValueError:
access_token = dict(parse_qsl(resp.text))
if not access_token or 'acces_token' not in access_token:
raise OAuth2Error(
'Error retrieving access token: %s' % resp.content
)
return access_token


How I generates my client_secret:

Code Block
def generate_apple_client_secret():
private_key_file =
open('/home/omar/Downloads/Privat-Key-file.p8', 'rb').read()
year = new_date_time.now().year
month = new_date_time.now().month
day = new_date_time.now().day
now = int(datetime.datetime(year, month, day).strftime('%s'))
headers = {"alg": 'ES256', "kid": private_key_file}
claims = {
"iss": settings.SOCIAL_AUTH_APPLE_TEAM_ID,
"iat": now,
"exp": now + int(datetime.timedelta(days=180).total_seconds()),
"aud": "https://appleid.apple.com",
"sub": settings.CLIENT_ID
}
client_secret = jwt.encode(
headers=headers, claims=claims, key=private_key_file, algorithm='ES256'
)
return client_secret


Replies

Did you ever solve this?