【问题标题】:Access Google Calendar API using Application-Specific Password使用应用程序专用密码访问 Google Calendar API
【发布时间】:2020-02-14 23:08:59
【问题描述】:

我正在编写一个 cron 脚本来每天检查我的 Google 日历。 我想知道是否可以使用特定于应用程序的密码 (见:https://support.google.com/accounts/answer/185833?hl=it) 并在我的脚本中插入生成的密码。 OAUTH 需要用户交互,因为我正在编写一个脚本,所以我不能按照这种方式进行操作。 我还阅读了有关“服务帐户”的信息,但希望我可以通过使用特定于应用程序的密码来避免它。有什么不同?有什么提示吗?

非常感谢 弗朗切斯科

EDIT1:我正在尝试的代码:

<?php
require __DIR__ . '/vendor/autoload.php';

$client = new Google_Client();
//The json file you got after creating the service account
putenv('GOOGLE_APPLICATION_CREDENTIALS=test-calendario-268115-5452ff6f57e8.json');
$client->useApplicationDefaultCredentials();
$client->setApplicationName("test_calendar");
$client->setScopes(Google_Service_Calendar::CALENDAR);
$client->setAccessType('offline');

$service = new Google_Service_Calendar($client);

$calendarList = $service->calendarList->listCalendarList();

EDIT2: $service->calendarList->listCalendarList() 使用空列表:

<?php
require __DIR__ . '/vendor/autoload.php';

$client = new Google_Client();
//The json file you got after creating the service account
putenv('GOOGLE_APPLICATION_CREDENTIALS=test-calendario-268115-5452ff6f57e8.json');
$client->useApplicationDefaultCredentials();
$client->setApplicationName("test_calendar");
$client->setScopes(Google_Service_Calendar::CALENDAR);
$client->setAccessType('offline');

$service = new Google_Service_Calendar($client);

$listEvents = $service->events->listEvents("...@group.calendar.google.com");// taken from sharing calendar settings
$events = $listEvents->getItems();
print_r($events);

【问题讨论】:

  • 您是否启用了两步验证?
  • 是的,我愿意...我已经按照链接中的说明生成了一个新的 16 字符密码
  • 服务帐户是为服务器-服务器交互而设计的。我相信这是你的情况,(cron-Calendar API)。阅读此Using OAuth 2.0 for Server to Server Applications

标签: google-api google-calendar-api google-oauth google-api-php-client


【解决方案1】:

有两种方法可以向 Google Calendar API 进行身份验证

  • OAuth2 提示用户访问。 (不打算进行服务器-服务器交互,但可以在用户生成刷新令牌并将其存储在服务器以后可以访问的位置时进行。)
  • 使用授权的服务帐户凭据。 (服务器-服务器的理想选择)。

没有其他方法可以访问用户数据,您必须获得他们的许可。

查看Using OAuth 2.0 for Server to Server Applications。

服务帐号

require_once __DIR__ . '/vendor/autoload.php';

// Use the developers console and download your service account
// credentials in JSON format. Place the file in this directory or
// change the key file location if necessary.
putenv('GOOGLE_APPLICATION_CREDENTIALS='.__DIR__.'/service-account.json');

/**
 * Gets the Google client refreshing auth if needed.
 * Documentation: https://developers.google.com/identity/protocols/OAuth2ServiceAccount
 * Initializes a client object.
 * @return A google client object.
 */
function getGoogleClient() {
    return getServiceAccountClient();
}

/**
 * Builds the Google client object.
 * Documentation: https://developers.google.com/api-client-library/php/auth/service-accounts
 * Scopes will need to be changed depending upon the API's being accessed. 
 * array(Google_Service_Analytics::ANALYTICS_READONLY, Google_Service_Analytics::ANALYTICS)
 * List of Google Scopes: https://developers.google.com/identity/protocols/googlescopes
 * @return A google client object.
 */
function getServiceAccountClient() {
    try {   
        // Create and configure a new client object.        
        $client = new Google_Client();
        $client->useApplicationDefaultCredentials();
        $client->addScope([YOUR SCOPES HERE]);
        return $client;
    } catch (Exception $e) {
        print "An error occurred: " . $e->getMessage();
    }
}

从ServiceAccount.php 窃取的代码

【讨论】:

  • 好的。我尝试过另一种方法,但它对我不起作用(链接:stackoverflow.com/questions/50656151/…)。我不必编写日历,只需阅读它......我不确定我是否可以将它作为公共可访问的日历阅读并放弃日历 API。
  • 我无法使用提供的示例检索任何日历
  • 记住服务帐户不是您。默认情况下,您将访问可能没有任何内容的服务帐户谷歌日历帐户。您需要获取服务帐户电子邮件 ID 并与其共享您自己的日历,然后它也可以访问该日历。
  • 我完全按照示例将虚假电子邮件添加到我的日历共享设置中......但我只能得到一个空的“项目”:$service->calendarList->listCalendarList()
  • 你能提供你用来生成 JWTClient 的代码吗?通常这是令人困惑的部分。删除敏感数据 b4 发布。并将其发布为对您的问题的编辑
【解决方案2】:

您似乎遇到了这种情况,this Calendar help page 中提到了这种情况。

请记住,它也适用于服务帐户。

The accepted answer to this question...

很好地解释了你可以使用CalendarList: insert方法将日历添加到日历列表中。

【讨论】:

    猜你喜欢
    • 2012-08-09
    • 2022-09-23
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 2012-05-02
    • 2014-01-08
    • 1970-01-01
    • 2013-03-19
    相关资源
    最近更新 更多