当前位置: 首页 > 知识库问答 >
问题:

使用服务器帐户和PHP使用Google日历API进行身份验证

郎德馨
2023-03-14

我正在努力解决这个问题:我正在尝试使用一个服务帐户,从PHP脚本在Google日历中创建一个事件。

以下是我所做的:

  • 创建了一个Google Cloud Project
  • 启用日历API
  • 创建了一个OAuth 2.0服务帐户,其中包含客户端ID、电子邮件地址和公钥
  • 下载了密钥文件并将其保存在我的网站中
  • 使用服务帐户中创建的电子邮件地址共享我的日历(具有管理共享权限)

这是我的代码:

<?php

require_once 'google_api_src/Google_Client.html" target="_blank">php';
require_once 'google_api_src/contrib/Google_CalendarService.php';

const CLIENT_ID = 'xxxxxxxxxxxxx.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = 'xxxxxxxxxxxxx@developer.gserviceaccount.com';

// Make sure you keep your key.p12 file in a secure location, and isn't
// readable by others.
const KEY_FILE = 'google_api_src/xxxxxxxxx-privatekey.p12';

$client = new Google_Client();
$client->setApplicationName("Hall Booking");

session_start();
if (isset($_SESSION['token']))
{
    $client->setAccessToken($_SESSION['token']);
}

// Load the key in PKCS 12 format
$key = file_get_contents(KEY_FILE);
$client->setClientId(CLIENT_ID);

$client->setAssertionCredentials(new Google_AssertionCredentials(
    SERVICE_ACCOUNT_NAME,
    array('https://www.googleapis.com/auth/calendar', "https://www.googleapis.com/auth/calendar.readonly"),
    $key));

$service = new Google_CalendarService($client);

//Save token in session
if ($client->getAccessToken())
{
    $_SESSION['token'] = $client->getAccessToken();
}  

?>

我已经尽可能地调试了代码,但在调用SetAssertionCredentials之后,标记总是设置为null。没有PHP错误。

有什么想法是错误的,或者如何进一步调试请?我必须对配置进行任何更改吗。api src文件夹中的php(到目前为止我还没有)?应用程序名称重要吗?它是用来干什么的?

共有2个答案

秦涵映
2023-03-14

签出演示:http://amazewebs.com/demo

或者得到一对一的帮助:http://amazewebs.com/go-premium

我得到了这个工作与服务号,你是使用错误的URL在您的代码,或者至少我有它的工作与我的方法没有问题...

在此处查看我的代码:http://amazewebs.com

或此处:

<!-- language: php -->

<?php
ini_set('display_errors', 1);
require_once '../google-api/Google_Client.php';
require_once '../google-api/contrib/Google_CalendarService.php';

session_start();

const CLIENT_ID = '<YOUR-CLIENT-ID-HERE>.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = '<YOUR-SERVICE-EMAIL-HERE>@developer.gserviceaccount.com';
const KEY_FILE = '<YOUR-FINGERPRINT-HERE>-privatekey.p12';

$client = new Google_Client();
$client->setApplicationName("<PUT-YOUR-PROJECT-NAME-HERE");
$client->setUseObjects(true); //IF USING SERVICE ACCOUNT (YES)


if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}

/* This next snippet of code is commonly written wrong, be sure to use the correct URL specified as below: */

$key = file_get_contents(KEY_FILE);
$client->setClientId(CLIENT_ID);
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME, 'https://www.google.com/calendar/feeds/<YOUR-CALENDAR-ID-HERE-WITHOUT-THE-TRAILING"@group.calendar.google.com">/private/full/',
$key)
);

$client->setClientId(CLIENT_ID);
$cal = new Google_CalendarService($client);
$event = new Google_Event();
$event->setSummary('<PUT-AN-EVENT-TITLE-HERE-OR-PASSED-VARIABLE>');
$event->setLocation('<PUT-A-LOCATION-HERE-OR-PASSED-VARIABLE>');

...The Full script can be found @ AmazeWebs.com

...我已经为您下载并打包了正确路径结构的所有文件:http://amazewebs.com/downloads/google-api.zip

或者,如果您想直接从google下载,您可以下载每个单独的文件并自行打包:http://google-api-php-client.googlecode.com/svn/trunk/src/

祝你好运:)

郤玉书
2023-03-14

好的-结果证明上面的代码工作正常。我的误解是认为Google\u CalendarService执行了身份验证。事实上,只有在实际调用日历时(例如插入事件),才执行身份验证。

但是,我认为在会话中保存令牌的位置不对-应该在执行操作之后进行。

回答我的其他问题:不,您不必对配置进行任何更改。php。应用程序名称可以设置为任何名称(但我不知道它的用途)。

我遇到的另一个问题是:我使用“primary”日历ID创建事件。这返回了一条成功消息,但我无法在日历中看到该事件(我只有该帐户的一个日历)。只有当我将“primary”改为“primary”时mycalendarid@gmail.com“它开始工作了。听起来像是API中的一个bug?

 类似资料:
  • 下面是示例代码:https://code.google.com/p/google-api-dotnet-client/wiki/oauth2#service_accounts 授权失败:dotnetOpenAuth.Messaging.ProtocolException:发送直接消息或获取响应时出错。 内部异常是System.net.WebException:远程服务器返回错误:(400)Bad

  • 我正在处理一个使用PHP的项目,需要使用PHP客户端库实现Google CloudAPI,但身份验证似乎对我不起作用。我已经创建了一个服务帐户并授予了项目所有者权限,我不想通过使用GOOGLE_DEFAULT_CREDENTIALS环境变量进行身份验证,我想使用服务帐户身份验证。 致命错误:未捕获异常“Google\Cloud\Core\exception\ServiceException”,消息

  • 问题内容: 我正在尝试使用oauth API通过Java API对Google服务帐户进行身份验证。我希望使用它来访问Google Bigquery。我的API请求返回了“无效的赠款”。 这是代码,它是基本身份验证示例的副本(不是Bigquery的。而是另一个Google API): SERVICE_ACCOUNT_EMAIL是以下格式的电子邮件地址:XXXXXXX@developer.gserv

  • 我的团队目前正在开发一个应用程序,使用Admin SDK在GCP中列出公司的域用户,用于入职和非入职目的。 我们使用一个服务帐户来完成这个任务,并且在Google Admin's advanced settings中添加了 作用域。管理SDK API被激活,我们可以在凭据区域中看到服务帐户。 当我们使用参数 和 调用https://www.googleapis.com/admin/director

  • 我试图利用Google Drive API将我们的应用程序创建的日志文件发送到我们公司的Google Drive帐户,我们为Android开发共享该帐户。大多数示例展示了如何使用oAuth2与最终用户的Google Drive帐户进行身份验证,但我们希望文件只发送到我们的Google Drive帐户。 经过一些搜索,我发现我应该为我们的应用程序创建一个API项目,并使用与该API项目相关联的服务帐

  • GoogleCredential凭证=newGoogleCredential.Builder(). setTransfer(TRANSPORT). setJsonFactory(JSON_FACTORY). setServiceAccount tId("SOMETHING@developer.gserviceaccount.com"). setServiceAccount tScopes(Bigq