I do have a problem with apple signin in python but it's mostly not python, just mysterious stuff.... So, I read this article https://medium.com/@aamishbaloch/sign-in-with-apple-in-your-django-python-backend-b501daa835a9 It should be quite easy to implement it but my implementation doesn't work, so what I did is: I've added appleapiprovider class which being used in rest framework, this AppleProvider being called this way:
provider = AppleApiProvider()
provider.verify_token(token_sent_from_ios_device) # NOT JWT TOKEN, just code.
and here is what happening in the method verify_token
def verify_token(self, token):
client_id, client_secret = self.get_key_and_secret()
headers = {'content-type': "application/x-www-form-urlencoded"}
data = {
'client_id': client_id,
'client_secret': client_secret,
'code': access_token,
'grant_type': 'authorization_code',
'redirect_uri': 'https://example-app.com/redirect'
}
res = requests.post('https://appleid.apple.com/auth/token', data=data, headers=headers)
def get_key_and_secret(self):
headers = {
'kid': KID
}
payload = {
'iss': ISS,
'iat': timezone.now(),
'exp': timezone.now() + timedelta(days=180),
'aud': 'https://appleid.apple.com',
'sub': 'com.APP.staging.signin',
}
client_secret = jwt.encode(
payload,
open('/etc/apple.pem', 'rb').read(),
algorithm='ES256',
headers=headers
).decode("utf-8")
return 'com.APP.staging.signin', client_secret
And in result I get
{error: 'invalid_grant'}
Any idea ? It's not a problem with client_id and client_secret, I tried to use there just absolutely random strings and it says, invalid_client Possible duplicate is: New Apple Sign in keeps throwing Error HTTP 400 Invalid_grant but there's no exact solution to my problem, it should be quite small and I don't know how to debug it.