当我下载json时,我已经创建了一个OAuth 2.0客户端ID,如下所示:

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

我正在try 这样设置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

我在这里找到了一些有相同问题的主题,但所有建议下载类型为Other的json文件的人都找不到该选项

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

并且在下载时没有其他下载选项:

只需1个选项即可下载json

Download option

最大的问题是,我做错了什么?

推荐答案

如果将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));
}

Php相关问答推荐

为什么调用干预\映像引发错误驱动程序\GD\Driver not found?

如果再次调用SESSION_START(),会话.gc-max是否会重新启动?或者它是从第一次创建会话开始计算的?

使用箭头函数语法时,在回调范围内无法访问变量变量(警告:未定义的变量)

在WooCommerce产品变体SKU字段旁边添加自定义输入字段

获取并判断WooCommerce用户订阅结束日期

Symfony5:如何在不登录的情况下使API路由可访问?

我的邮箱中的链接在我的URL中添加了一个

Codeigniter 4中的未定义变量$HOME

限制联系人页面仅允许在WooCommerce中登录的用户

根据WooCommerce的定制订单状态增加产品库存

打折时更改 Woocommerce 产品名称

如何使用 PHP 创建简化的 twig 替换?

根据小计向 WooCommerce Checkout 添加佣金

VS Code 对 PHP 上从父类继承的方法报告误报

带有分类术语数组的 WordPress Elementor 自定义查询 - 如何要求所有术语都在返回的帖子中

PHP 简单 XML Xpath 上移树(带有名称的父级)

Woocommerce单个添加到购物车页面中的产品动态定价

NetBeans 不显示中文内容

WooCommerce 按 ID 限制产品

htaccess 无限循环问题