laravel-sign-in-with-apple icon indicating copy to clipboard operation
laravel-sign-in-with-apple copied to clipboard

Client error: `POST https://appleid.apple.com/auth/token` resulted in a `400 Bad Request` response: {"error":"invalid_client"}

Open dhivya-picco opened this issue 3 years ago • 15 comments

I have configured all the configuration for apple signin. It is redirect to apple site to get username and password. after that in callback, it is showing like this

dhivya-picco avatar Jul 24 '21 09:07 dhivya-picco

I am getting the same issue, did you have any luck resolving this?

haid45 avatar Jul 28 '21 06:07 haid45

Yes I am getting this same issue , before a month its working fine on my site

rohail-office avatar Aug 11 '21 09:08 rohail-office

It is because, your client_secret token is expired, you need to generate a new token and replace it with old one, it will be fixed then. you can add max 6 months expire time of JWT token, after 6 months you have to generate new one. https://developer.apple.com/documentation/sign_in_with_apple/generate_and_validate_tokens

iamaz007 avatar Aug 16 '21 10:08 iamaz007

@iamaz007 I create new app and new client secret and update the existing configuration but its giving the same error

rohail-office avatar Aug 16 '21 10:08 rohail-office

@iamaz007 I create new app and new client secret and update the existing configuration but its giving the same error

you don't need to create new app in apple, just generate a new client_secret, make sure you have selected right algorithm it is "ES256" for apple JWT in code, after implementing new client_secret, clear your Laravel cache, to do this, simply go to boostrap/cache and delete all files from there except .gitIgnore

iamaz007 avatar Aug 16 '21 11:08 iamaz007

Thanks I will try this

rohail-office avatar Aug 16 '21 12:08 rohail-office

@iamaz007
Unfortunately I am facing this same issue after removing the Laravel cache and deleting all files except .gitignore form boostrap/cache folder .

This is how , I am creating my client secret

require 'jwt'

key_file = 'key.txt' team_id = 'XYZ' client_id = 'XYZ' key_id = 'XYZ'

ecdsa_key = OpenSSL::PKey::EC.new IO.read key_file

headers = { 'kid' => key_id }

claims = { 'iss' => team_id, 'iat' => Time.now.to_i, 'exp' => Time.now.to_i + 86400*180, 'aud' => 'https://appleid.apple.com', 'sub' => client_id, }

token = JWT.encode claims, ecdsa_key, 'ES256', headers puts token

rohail-office avatar Aug 16 '21 12:08 rohail-office

I am also getting this error: GuzzleHttp\Exception\ClientException Client error: POST https://appleid.apple.com/auth/token resulted in a 400 Bad Request response: {"error":"invalid_client"}

I recreated my JWT token to make sure it was not expired and I made sure the ES256 algorithm was being used.

Has anyone with this issue found a solution?

Thank you in advance!

Update: I tried validating my JWT token at https://jwt.io/ and it came up as an invalid signature. I'm not sure if this is what was causing the invalid_client error or not, but I was not able to resolve it. I ended up switching to use the library at https://github.com/patrickbussmann/oauth2-apple It takes care of generating the token, so all you have to do is fill in the config values from your Apple account and it works. Good luck all.

alexfraundorf-com avatar Oct 06 '21 19:10 alexfraundorf-com

I ended up generating the client secret using lcobucci/jwt

<?php
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Signer\Key\InMemory;

$now = new \DateTimeImmutable();

$jwtConfig = Configuration::forSymmetricSigner(
    new Sha256(),
    InMemory::file(__DIR__ . '/AuthKey.pem')
);

$token = $jwtConfig->builder()
    ->issuedBy('XXXXXXXX')
    ->issuedAt($now)
    ->expiresAt($now->modify('+1 hour'))
    ->permittedFor('https://appleid.apple.com')
    ->relatedTo('com.example.service-id')
    ->withHeader('kid', 'XXXXXXXX')
    ->getToken($jwtConfig->signer(), $jwtConfig->signingKey());

echo $token->toString();

more info is here

karser avatar Feb 16 '22 01:02 karser

@iamaz007 I create new app and new client secret and update the existing configuration but its giving the same error

you don't need to create new app in apple, just generate a new client_secret, make sure you have selected right algorithm it is "ES256" for apple JWT in code, after implementing new client_secret, clear your Laravel cache, to do this, simply go to boostrap/cache and delete all files from there except .gitIgnore

Plz, how to generate new client secret ?

KaviiChathuranga avatar Jun 29 '22 13:06 KaviiChathuranga