googleapis / google-api-php-client

A PHP client library for accessing Google APIs
Apache License 2.0
9.22k stars 3.52k forks source link

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

Closed basd82 closed 10 months ago

basd82 commented 11 months ago

Environment details

Steps to reproduce

Try to get oath to work The api reports json file is missing but it is there, and it is unchanged like downloaded from google dev.

Code example

google login.php

php
$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));
}

json auth file

{
  "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"
    ]
  }
}
basd82 commented 11 months ago

It seems it is not getting the content of te file, if change this

$client->setAuthConfig($authfile); to this:

$json = file_get_contents($authfile); $client->setAuthConfig($json);

it works

yash30201 commented 11 months ago

Hi @basd82 , I tried the exact same code as above and I'm not facing any such error. Can you please check on your side what is happening by debugging the decoding the setAuthConfig method?

yash30201 commented 10 months ago

Closing this issue as this seems inactive. Please feel free to reopen if the issue still persists.

R1pon commented 9 months ago

Riopn

R1pon commented 9 months ago

php $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)); }

R1pon commented 9 months ago

{ "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" ] } }