Error : {"invalid_client"} Login with Apple in Nodejs

559 Views Asked by At

I am facing this error | error: '{"error":"invalid_client"}'| while calling signIn with apple API with nodejs. I have tried many times but i got same error. I tried different codes, different generated ClientID's and PrivateKeys but got same error. I have also read questions on gitHub and developer.apple.com but none of the answer helped me out. Any help from anyone would be appreciated. Following is the code I m working on!

const getClientSecret = () => {
const headers = {
    alg: 'ES256',
    kid: KEY_ID
};
const timeNow = Math.floor(Date.now() / 1000);
const claims = {
    iss: TEARM_ID,
    aud: 'https://appleid.apple.com',
    sub: CLIENT_ID,
    iat: timeNow,
    exp: timeNow + 15777000
};

const token = jwt.sign(claims, PRIVATEKEY, {
    algorithm: 'ES256',
    header: headers
    // expiresIn: '24h'
});

return token;

}

const clientSecret = getClientSecret();

const params = {
    grant_type: 'authorization_code', // refresh_token authorization_code
    code: req.body.code, //apple Login Token
    //redirect_uri: [REDIRECT_URI],
    client_id: CLIENT_ID,
    client_secret: clientSecret
    // refresh_token:body.id_token
};

try {

    axios.request({
        method: 'POST',
        url: 'https://appleid.apple.com/auth/token',
        data: querystring.stringify(params),
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    }).then(response => {
        console.log('response', response);
    }).catch(error => {
        console.log('error1', error.response.data);
        res.send({
            success: false,
            error: error.response.data
        })
    })

} catch (error) {
    console.log('error', error);
}

Response

 statusCode: 400,
 error: '{"error":"invalid_client"}'
0

There are 0 best solutions below