无法启用 Google 日历云 API

Cannot enable the Google Calendar Cloud API

我与 Google Workspace 客户支持进行了交谈,但他们未能找到有关此问题的适当文档。他们指示我到这里。

非常简单,我正在尝试启用 Google 云 API 在用户的日历中创建一个事件(并希望添加一个 Google 会议 URL到它)根据这个文档:https://developers.google.com/calendar/api/guides/create-events.

我正在关注快速入门 PHP 代码示例,它首先指示通过 Composer 安装 SDK 并在 PHP 中授权 Google Cloud API。 link 在这里:https://developers.google.com/calendar/api/quickstart/php.

该页面提供的脚本(第 2 步)最初失败(在第 3 步期间)并出现以下错误:

PHP Fatal error:  Uncaught InvalidArgumentException: file "credentials.json" does not exist in /path/to/vendor/Google/google/apiclient/src/Client.php:984

堆栈跟踪: #0 /path/to/app_root/quickstart.php(17): Google\Client->setAuthConfig('credentials.jso...') #1 /path/to/app_root/quickstart.php(63): getClient() #2 {主要} 在第 984

行 /path/to/vendor/Google/google/apiclient/src/Client.php 中抛出

我已经按照此处的说明创建了一个服务帐户:

虽然说明中没有包括创建服务帐户和生成 JSON 密钥的步骤,但我在网上找到了该解决方案,并且已由 Google Workspace 支持代理验证。我下载并重命名了该文件并将其放在脚本根目录中(尽管在不完整的文档中也未指定路径,但已由 Google Workspace 支持代理验证)。我再次 运行 脚本并收到此错误:

PHP Fatal error:  Uncaught InvalidArgumentException: missing the required redirect URI in /path/to/vendor/Google/google/auth/src/OAuth2.php:694
Stack trace:
#0 /path/to/vendor/Google/google/apiclient/src/Client.php(406): Google\Auth\OAuth2->buildFullAuthorizationUri(Array)
#1 /path/to/app_root/quickstart.php(38): Google\Client->createAuthUrl()
#2 /path/to/app_root/quickstart.php(63): getClient()
#3 {main}
  thrown in /path/to/vendor/Google/google/auth/src/OAuth2.php on line 694

我试图创建一个 OAuth 重定向 URI 并重新生成 JSON 密钥,但是,出现了同样的错误。这是我从服务帐户 KEYS 部分下载的身份验证 JSON 文件,其中包含已编辑的任何帐户特定信息:

{
  "type": "service_account",
  "project_id": "massive-clone-{REDACTED}",
  "private_key_id": "{REDACTED}",
  "private_key": "-----BEGIN PRIVATE KEY-----{REDACTED}\n-----END PRIVATE KEY-----\n",
  "client_email": "calendar-api@massive-clone-{REDACTED}.iam.gserviceaccount.com",
  "client_id": "{REDACTED}",
  "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_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/calendar-api%40massive-clone-{REDACTED}.iam.gserviceaccount.com"
}

虽然快速入门指南似乎在故障排除部分解决了这个问题,但该建议似乎并不适用,因为在第 1 步中没有“按钮”。来自故障排除部分:

Uncaught InvalidArgumentException: missing the required redirect URI

This error occurs when the credentials.json file used contains a client ID of the wrong type. This code requires an OAuth client ID of type Other, which will be created for you when using the button in Step 1. If creating your own client ID please ensure you select the correct type.

任何人都可以帮助我了解如何获得适当的 credentials.json 文件吗?我不确定这个过程应该如何进行,但是 Google Workspace 支持代理说他们找不到任何文档并将我引导到这里。如果我的问题需要用不同的方式说明,请指教。

您的代码正在尝试使用服务帐户。该示例使用 OAuth 客户端机密。

Setting up OAuth 2.0