php脚本在谷歌日历上设置事件

时间:2017-10-16 10:07:41

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

我正在使用PHP开发一个Web应用程序来预约。

我有fullcalendar,它运行正常。我还需要直接在我的谷歌日历上保存活动。有可能吗?

在Google API中,我只找到“如何在用户日历上添加活动”,而不是我的日历。

1 个答案:

答案 0 :(得分:0)

如果您使用服务帐户,则可以通过授予日历服务帐户权限直接保存到您自己的日历中。

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

如果您对此感兴趣的是一篇关于服务帐户如何运作以及如何设置服务帐户的文章,则会从我的php sample project for Google calendar中删除代码。 Google development for beginners service accounts