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. )