使用PHP将事件导出到Google日历

时间:2018-12-04 12:13:13

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

我想使用PHP将日历事件从我的网站导出到现有的Google日历(请注意:这不是已登录用户的日历,而是链接到我自己的帐户的预定义日历)。我在官方文档中找不到完整的示例。该文档示例在命令行中使用PHP。但是,我想在我的网站上设置它,而不使用命令行。我发现以下帖子:Google Calendar API (v3) - PHP can write but not read?,它使用curl,所以我尝试使用它:

$api_key = 'api-key-from-google-console';
$calendar_id = "calendar_id";

$start = array(
  "dateTime" => $date . "T" . $start_time . ":00",
  "timeZone" => "Africa/Johannesburg"
);

$end = array(
  "dateTime" => $date . "T" . $end_time . ":00",
  "timeZone" => "Africa/Johannesburg"
);

$headerarray = array(
  'Content-type: application/json',
  'Authorization: Bearer ' . $api_key,
  'X-JavaScript-User-Agent: Google APIs Explorer'
);

$post_data = array(
  "start"       => $start,
  "end"         => $end,
  "summary"     => $title,
  "description" => $description,
  "key"         => "$api_key"
);

$url = 'https://www.googleapis.com/calendar/v3/calendars/' . $calendar_id . '/events';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerarray);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = json_decode($response);

但是,这返回一个错误,提示我的凭据无效。该代码有什么问题?我正在使用活动的API密钥,但是当我查看Google文档时,我不清楚在此curl代码中应如何正确使用它。在Stackoverflow的示例中,也没有其他指令。

1 个答案:

答案 0 :(得分:1)

首先,您应该考虑使用服务帐户。服务帐户就像是虚拟用户。例如,它有自己的Google日历帐户。但是,因为它是用户,所以您可以与该服务帐户共享您的个人google日历,并且无需登录即可访问它。

假设您愿意使用google apis php客户端库,则可以使用ServiceAccount.php对代码进行身份验证,然后在需要时调用它。我建议关注php quickstart

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

我还没有尝试使它与curl配合使用,但是如果您想这样做可能会帮助https://developers.google.com/api-client-library/php/auth/service-accounts