skip to Main Content

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 GoogleClient();

$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(GoogleServiceFitness::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 ?

2

Answers


  1. Chosen as BEST ANSWER

    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 GoogleClient();
    
    $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(GoogleServiceFitness::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));
    }
    

  2. The very first thing you can start with is to make sure that the client_secret.json exits and is readable. Perhaps all you need to add is the __DIR__documentation and / in the path to the file like in the EXAMPLE 2

    example debugging code:

    <?php
    
    echo "Current directory is: " . __DIR__ . '<br>' . PHP_EOL;
    
    echo "================ EXAMPLE 1 ==================" . '<br>' . PHP_EOL;
    
    $authfile = 'somepath/client_secret.json';
    
    if (file_exists($authfile)) {
        echo "File exists: " . $authfile . '<br>' . PHP_EOL;
        if (is_readable($authfile)) {
            echo "File is readable: " . $authfile . '<br>' . PHP_EOL;
    
            try {
                $authfileContent = file_get_contents($authfile);
                $json = json_decode($authfileContent, null, 512, JSON_THROW_ON_ERROR);
    
                echo "File is a valid JSON format: " . $authfile . '<br>' . PHP_EOL;
                
            } catch (Throwable $exception) {
                echo "File is NOT a valid JSON format: " . $authfile . '<br>' . PHP_EOL;
                echo "Exception msg: " . $exception->getMessage() . '<br>' . PHP_EOL;
            }
    
        } else {
            echo "File is NOT readable: " . $authfile . '<br>' . PHP_EOL;
        }
    } else {
        echo "File DOES NOT exist: " . $authfile . '<br>' . PHP_EOL;
    }
    
    echo "================ EXAMPLE 2 ==================" . '<br>' . PHP_EOL;
    
    # note adding __DIR__ and /
    $authfile = __DIR__ . '/somepath/client_secret.json';
    
    if (file_exists($authfile)) {
        echo "File exists: " . $authfile . '<br>' . PHP_EOL;
        if (is_readable($authfile)) {
            echo "File is readable: " . $authfile . '<br>' . PHP_EOL;
    
            try {
                $authfileContent = file_get_contents($authfile);
                $json = json_decode($authfileContent, null, 512, JSON_THROW_ON_ERROR);
    
                echo "File is a valid JSON format: " . $authfile . '<br>' . PHP_EOL;
                
            } catch (Throwable $exception) {
                echo "File is NOT a valid JSON format: " . $authfile . '<br>' . PHP_EOL;
                echo "Exception msg: " . $exception->getMessage() . '<br>' . PHP_EOL;
            }
    
    
        } else {
            echo "File is NOT readable: " . $authfile . '<br>' . PHP_EOL;
        }
    } else {
        echo "File DOES NOT exist: " . $authfile . '<br>' . PHP_EOL;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search