phpgoogle-apigoogle-oauthgoogle-api-php-clientgoogle-fit-api

Fatal error: Uncaught Google_Exception: Invalid client secret JSON file. in /somepath/lib/vendor/google/apiclient/src/Google/Client.php


I have create an OAuth 2.0 Client IDs that looks like this when i download the json:

{
  "web": {
    "client_id": "topsecretstuff.apps.googleusercontent.com",
    "project_id": "health-42",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_secret": "GOCSPX-topsecretstuff",
    "redirect_uris": [
      "https://topsecret.domain.tld/oauth2callback.php",
      "https://topsecret.domain.tld/googlelogin.php"
    ]
  }
}

I am trying setup oauth authentication like this

$client = new Google\Client();

$authfile = 'somepath/client_secret.json';
$client->setAuthConfig($authfile);
$client->setRedirectUri('https://topsecret.domain.tld/oauth2callback.php');
$client->setAccessType('offline');        // offline access
$client->setIncludeGrantedScopes(true);   // incremental auth
$client->addScope(Google\Service\Fitness::FITNESS_ACTIVITY_READ);
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
if (isset($_SESSION['access_token']) && $_SESSION['access_token'])
{
    $client->setAccessToken($_SESSION['access_token']);
    echo "Ingelogd met access tolken: " . $_SESSION['access_token'];
}
else
{
    $redirect_uri = 'https://topsecret.domain.tld/oauth2callback.php';
    header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

But when it opens i get

Fatal error:  Uncaught Google_Exception: Invalid client secret JSON file. in somepath/lib/vendor/google/apiclient/src/Google/Client.php:171
Stack trace:
#0 somepath/client.php(10): Google_Client->setAuthConfig('/somepath...')
#1 somepath/googlelogin.php(32): include_once('/somepath...')
#2 {main}
  thrown in somepath/lib/vendor/google/apiclient/src/Google/Client.php on line 171

I found some topics here with same problem but the all sugest to download json file with type other can't find that option

My o auth2 config at google looks like this My oath credential config

And at download is don't have download option other:

Just 1 option download json

Download option

The big question what am i doing wrong ?


Solution

  • I found the answer/workaround if do file_get_contents of json in to paramater and pass that to setAuthConfig, same if i add the json inline, it works.

    So it seems its a bug in the api.

    so the working code looks like this:

    $client = new Google\Client();
    
    $authfile = 'somepath/client_secret.json';
    $json = file_get_contents($authfile);
    $client->setAuthConfig($json);
    $client->setRedirectUri('https://topsecret.domain.tld/oauth2callback.php');
    $client->setAccessType('offline');        // offline access
    $client->setIncludeGrantedScopes(true);   // incremental auth
    $client->addScope(Google\Service\Fitness::FITNESS_ACTIVITY_READ);
    $auth_url = $client->createAuthUrl();
    header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
    if (isset($_SESSION['access_token']) && $_SESSION['access_token'])
    {
        $client->setAccessToken($_SESSION['access_token']);
        echo "Ingelogd met access tolken: " . $_SESSION['access_token'];
    }
    else
    {
        $redirect_uri = 'https://topsecret.domain.tld/oauth2callback.php';
        header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
    }