1

I am trying to allow users to sign in to my website using Google's library in PHP. Their example is Here. However, using the code they provided, I get the same error every time. My First file is signin.php :

<?php
require_once __DIR__.'/google-api-php-client/vendor/autoload.php';

session_start();

$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$client->setRedirectUri('https://' . $_SERVER['HTTP_HOST'] . '/authenticate.php');
$client->addScope("profile");
$client->addScope("email");
$client->addScope("openid");

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $client->setAccessToken($_SESSION['access_token']);
} else {
  $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/authenticate.php';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

And the second file is authenticate.php:

<?php
require_once __DIR__.'/google-api-php-client/vendor/autoload.php';

session_start();

$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$client->setRedirectUri('https://' . $_SERVER['HTTP_HOST'] . '/authenticate.php');
$client->addScope("profile");
$client->addScope("email");
$client->addScope("openid");

if (! isset($_GET['code'])) {
  $auth_url = $client->createAuthUrl();
  header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
  $yo = $client->authenticate($_GET['code']);
  print_r($yo);
  //$_SESSION['access_token'] = $client->getAccessToken();
   $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
  print_r($token);
  $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/signin.php';
  //header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

Every time I run this, I select my account and I am returned to the page. There is the necessary code returned, but it fails to authenticate it with the following error:

Array ( [error] => invalid_request [error_description] => Could not determine client ID from request. ) Array ( [error] => invalid_request [error_description] => Could not determine client ID from request. )
0

2 Answers 2

1

manually generate a client_secrets.json and make sure the path is correct.

Sign up to request clarification or add additional context in comments.

2 Comments

I have tried this, even setting the client_id through a command as well. Also, the authentication page loads, which requires a client_id on its own. So unfortunately, this is not the problem.
@DanielH you might have generated the wrong type of account; for a service account there should be no authentication page. see developers.google.com/api-client-library/php/auth/…
1

When you redirect using header(location:...) the session data that you've established is lost. Try doing a var_dump of your session variable after you're redirected back into your application and you should see it empty, at least that's what happened in my situation. Everything else you do appears correct to me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.