0
votes

Attempting to authenticate using Google on iOS with backend server, code used to work with older google client libraries.

$client = new Google_Client();
$oauth2 = new Google_Service_Oauth2($client);
$client->setApplicationName('MyApp');
$client->setClientId($google_config["clientId"]);
$client->setClientSecret($google_config["clientSecret"]);
$client->setRedirectUri("");
$client->setDeveloperKey($google_config["developerKey"]));
Log::error("Google/authcode", $request->get('token'));
if (! isset($_SESSION['access_token'])) {
    try {
    $client->authenticate($request->get('token'));
    } .... THROWS exception: Error fetching OAuth2 access token, message: 'invalid_grant:

Attempting to validate token against:

https://www.googleapis.com/oauth2/v1/tokeninfo?id_token= https://www.googleapis.com/oauth2/v2/tokeninfo?id_token= https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=

all work successfully shows that token is valid.

EDIT: JUST FOUND OUT after adding verifyIdToken call, that: Invalid issuer, https://accounts.google.com != accounts.google.com seems like this is why I am getting a failure. I am guessing that iOS sdk vs PHP is setting things up differently? Or perhaps this is not relevant and is assumed that it is not authorization of the server from iOS device. Since if I keep changing things on server side code i.e. chang OAUTH2_ISSUER to be https://accounts.google.com, I would get wrong recipient error, which makes sense since I am using tokenid obtained by iOS client to authorize server.

Older version of iOS app works fine as well. But token is completely different although validates just fine as well. Any help is greatly appreciated.

1

1 Answers

0
votes

I think I misunderstood documentation. In order to authorize server one needs to pass googleUser.serverAuthCode from

- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)googleUser
 withError:(NSError *)error { ....

If only validating backend server with authorized on iOS side user googleUser.authentication.accessToken to the server which in turn would need to be validated or parsed on the server to get email and other data.