0

I'm facing issues for sign in with the apple. I go through many blogs and official documentation but nothing works.

The flow of my application is the same as mentioned in the documentation: Application will send authorization code to the backend server, backend server generates a client secret and it will send it to the apple server in order to get access_token.

Getting the same response every time: response: {"error: "invalid_client"}

So here is my code:

Generating client_secret

used jsonwebtoken package.

let client_secret = jwt.sign({}, privateToken, {
    algorithm: 'ES256',
    expiresIn: 1578513833,
    audience: "https://appleid.apple.com",
    subject: process.env.APPLE_CLIENT_ID,
    issuer: process.env.ISS,
    keyid: process.env.KID
});

Payload to send to apple server

let body = {
  client_id: process.env.APPLE_CLIENT_ID,
  client_secret: client_secret,
  code: authorize_code,
  grant_type: 'authorization_code'
}

Request to apple server

let appleResponse = await axios.post(
  'https://appleid.apple.com/auth/token',
  queryString.stringify(body), {
  headers: {
       'Content-Type': 'application/x-www-form-urlencoded'
  }
});

Can anyone please help me to solve this error.

Thank you in advance.

Darshil Mehta
  • 122
  • 1
  • 6

1 Answers1

0

For the last 3 days, I was getting the same error "invalid_client" from the API

Turns out that I needed to encode the HttpBody using this extension

Note: for you, there might be another reasons for the error, make sure to sign valid JWT.

Ziad Kamel
  • 37
  • 1
  • 6