Google api 使用特定于应用程序的密码访问Google日历API

Google api 使用特定于应用程序的密码访问Google日历API,google-api,google-calendar-api,google-oauth,google-api-php-client,Google Api,Google Calendar Api,Google Oauth,Google Api Php Client,我正在使用cron脚本每天检查我的谷歌日历。 我想知道是否可以使用特定于应用程序的密码 (见:) 并在我的脚本中插入生成的密码。 OAUTH需要用户交互,因为我正在编写一个脚本,所以无法按照这种方式进行操作。 我也读过关于“服务帐户”的文章,但我希望我可以通过使用特定于应用程序的密码来避免它。有什么区别?有什么提示吗 非常感谢 弗朗西斯科 EDIT1:我正在尝试使用的代码: <?php require __DIR__ . '/vendor/autoload.php'; $client =

我正在使用cron脚本每天检查我的谷歌日历。 我想知道是否可以使用特定于应用程序的密码 (见:) 并在我的脚本中插入生成的密码。 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();

有两种方法可以通过Google日历API进行身份验证

  • OAuth2提示用户访问。(不是预期的服务器-服务器交互,但如果用户生成刷新令牌,然后将其存储在服务器稍后可以访问的位置,则可以执行此操作。)
  • 使用授权服务帐户凭据。(非常适合服务器)
没有其他方法可以访问用户数据,您必须拥有他们的权限

调查

服务帐户

撕下的代码您似乎遇到了这种情况,如中所述

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


很好地解释了您可以使用该方法将日历添加到日历列表。

是否启用了两步验证?是的,我启用了。。。我按照linkService中的说明生成了一个新的16个字符的密码。帐户是为服务器交互而设计的。我相信这就是你的情况,(cron日历API)。把这个通读一遍好吗。我试着跟随另一个脚步,但它对我不起作用(链接:)。我不需要写日历,但只要读一下就可以了。。。我不确定是否可以将其作为公共可访问日历阅读,并放弃caleandar API。我无法使用提供的示例检索任何日历请记住,服务帐户不是您。默认情况下,您将访问服务帐户google calendar帐户,该帐户上可能没有任何内容。您需要使用服务帐户电子邮件id并与其共享您自己的日历,然后它也可以访问该日历。我正是按照此示例将假电子邮件添加到我的日历共享设置中。。。但是我只能用:$service->calendarList->listcendarlist()获得一个空的“items”,你能提供你用来生成JWTClient的代码吗?通常这是令人困惑的部分。删除敏感数据b4过帐。并将其作为您的问题的编辑发布
<?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);

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();
    }
}