我创建了一个 OAuth 2.0 客户端 ID,当我下载 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"
]
}
}
我正在尝试像这样设置 oauth 身份验证
$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));
}
但是当它打开时我明白了
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
我在这里发现一些主题有同样的问题,但所有建议下载其他类型的 json 文件找不到该选项
下载时没有其他下载选项:
只需 1 个选项下载 json
最大的问题是我做错了什么?
如果将 json 的 file_get_contents 传入参数并将其传递给 setAuthConfig,我找到了答案/解决方法,如果我内联添加 json,它会起作用。
所以这似乎是 api 中的一个错误。
所以工作代码如下所示:
$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));
}
在应用程序环境的虚拟主机的 apache conf 文件中,声明 client_secret.json 文件的完整路径:
SetEnv GOOGLE_APPLICATION_CREDENTIALS '/full/path/to/client_secret.json'
然后在您的生产代码中,替换:
$authfile = 'somepath/client_secret.json';
$json = file_get_contents($authfile);
$client->setAuthConfig($json);
与
$client->useApplicationDefaultCredentials();
这是引用 client_secret.json 文件的更安全/标准的方法
您可以开始的第一件事是确保
client_secret.json
存在并且可读。也许您需要添加的只是文件路径中的 __DIR__
- documentation 和 /
,如示例 2 所示
调试代码示例:
<?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;
}