Invalid_grant authorization code apple sign in .net backend


I have an IOS application that needs to authenticate to an in house login server. I do all the configuration need it for enable Apple Sign in in my app. I enable my application key to work with apple sign in and add de capability in Xcode.

I successfully do the login in the IOS app usign the native sign in button, but when I sent the authorization code to our backend for validation we are always getting invalid_grant error.

Our backend is in .net, the server have the key secret downladed from Apple member center, this is how the server do the validation (pictures):

In the _clientId field we put the bundle identifier of our app.

We tried a lot of things but we are always getting the invalid_grant error from apple. We also try to use the identityToken from the IOS application as client_secret but we are getting the same error.

Thanks in advance!

Post not yet marked as solved Up vote post of ralg1208 Down vote post of ralg1208


We had a similar issue with the authorization code.

At first we decoded it as UTF8, which caused problems, but decoding it as Unicode did the trick:

guard let appleIDCredentials = authorization.credential as? ASAuthorizationAppleIDCredential else { return }
let authCode = String(decoding: appleIDCredentials.authorizationCode!, as: Unicode.ASCII.self)

Hope that helps!

@roblack you saved me!!! thank you so much!