简体   繁体   English

Google Calendar API v3 PHP和JSON

[英]Google Calendar API v3 PHP and JSON

Have a public Google Calendar where all I want to do is get the events from "today" up to n (or max) events. 有一个公开的Google日历,我要做的就是将事件从“今天”获取到最多n(或最多)个事件。 I've been running around in circles trying to figure it out, it's either depretiated information or the soutions don't seem to work. 我一直在兜圈子,试图找出答案,这要么是已弃用的信息,要么是似乎无法使用。 A good solution path is using PHP to generate the initial list of events with css goodness and javascript array so I can update a details DIV when the user clicks on an event. 一个好的解决方案是使用PHP生成具有CSS优势和javascript数组的事件的初始列表,因此当用户单击事件时,我可以更新详细信息DIV。

Using PHP and uploaded the latest API to the server, have a calendar project with a read only account, an OAuth 2.0 client ID, and API key. 使用PHP并将最新的API上传到服务器,拥有一个带有只读帐户,OAuth 2.0客户端ID和API密钥的日历项目。 Got close with a few tutorials but hit a snag because you need the './credentials/calendar-api-quickstart.json' which is generated through the command line. 与一些教程紧密联系,但遇到了麻烦,因为您需要通过命令行生成的“ ./credentials/calendar-api-quickstart.json”。 The server I have to use is a shared one so there is no command line access (for obvious reasons :)). 我必须使用的服务器是共享服务器,因此没有命令行访问权限(出于显而易见的原因:))。 Used the https://developers.google.com/google-apps/calendar/quickstart/php information. 使用了https://developers.google.com/google-apps/calendar/quickstart/php信息。 That seems to be the the only piece i'm missing? 那似乎是我唯一想念的东西?

Activated the key too through the info on this page Google Calendar API v3 hardcoded credentials but getting an "invalid_grant" error. 也通过此页面上的信息激活了密钥,该日历是Google Calendar API v3硬编码的凭据,但出现“ invalid_grant”错误。 I've tried everything that came up in search but nothing seems to work. 我已经尝试了搜索中出现的所有内容,但似乎没有任何效果。 Is there an accurate up to date tutorial (2016-2017) that walks you though getting the Google Calendar data into your site? 是否有准确的最新教程(2016-2017年)指导您完成如何将Google日历数据导入您的网站? From setting up the project to oauth2 and token generation. 从设置项目到oauth2和令牌生成。 I've got to be messing something up because nothing seems to work. 我必须弄乱一些东西,因为似乎什么也没用。

The only other solution is to have them export the calendar as an .ics but they want live updates as they update the google calendar. 唯一的其他解决方案是让他们将日历导出为.ics,但他们希望在更新Google日历时进行实时更新。

Because i'm just looking to read the data I tried the suggestion here Get JSON from a public Google Calendar . 因为我只是想读取数据,所以我在这里尝试了建议从公共Google日历获取JSON But I get the Forbidden Error 403 error so it's depretiated? 但是我收到了禁止错误403错误,因此已弃用?

Still don't understand why they made it this complicated...I understand going through all the oauth stuff if you want to interact with the calendar on your site or app but just to read the data on a public calendar (which warns you making it public that everyone can see it)? 仍然不明白为什么他们要使它变得如此复杂...如果您想与网站或应用程序上的日历进行交互,而只是阅读公共日历上的数据(这会警告您,每个人都可以看到的公开)?

Thank you 谢谢

Finally found the solution. 终于找到了解决方案。 Posting here and might make a full tutorial later if i get any free time :). 在这里发布,如果我有空的话,以后可能会做一个完整的教程:)。 Hopefully it helps others. 希望它可以帮助其他人。

https://console.developers.google.com/iam-admin/projects create a project which should take you to the console after creating it...if not here is the console link https://console.developers.google.com/ https://console.developers.google.com/iam-admin/projects创建一个项目,该项目创建后应带您进入控制台...如果没有,则是控制台链接https://console.developers.google。 COM /

in the library section enable the "Google Calendar API". 在库部分中,启用“ Google Calendar API”。 Search for Google Calendar API, select it and use the enable button that appears at the top. 搜索Google Calendar API,选择它并使用顶部显示的启用按钮。

Go to the credentials section and add a Service account key. 转到凭据部分,然后添加服务帐户密钥。 New service account (any name you want). 新服务帐户(您想要的任何名称)。

Rolls: 劳斯莱斯:

Project > Viewer 项目>查看器

App Engine > App Engine Viewer App Engine> App Engine查看器

Storage > Storage Object Viewer 存储>存储对象查看器

You might not need all those but that seems to be the combonation that worked for me. 您可能不需要所有这些,但这似乎是对我有用的组合。 Make sure JSON is selected then on create it'll download a .json file. 确保选择了JSON,然后在创建时将下载.json文件。 It only downloads it once with no way to redownload so make sure you download and save it somewhere safe. 它只会下载一次,而无法重新下载,因此请确保将其下载并保存在安全的地方。

Download the latest API for PHP here https://developers.google.com/google-apps/calendar/downloads and upload it to wherever on your site. 在此处https://developers.google.com/google-apps/calendar/downloads下载最新的PHP API,并将其上传到您网站上的任何位置。 Fairly large with lots of support files so may take a while to upload. 很大,有很多支持文件,因此可能需要一段时间才能上传。

Then this is the PHP code that worked for me :) 然后这是对我有用的PHP代码:)

CALENDARID is the id of your calendar... from your google calendar, use the popup and choose "calendar settings". CALENDARID是您日历的ID ...从您的Google日历中,使用弹出菜单并选择“日历设置”。 Then towards the bottom in the "Calendar Address:" section make note of the Calendar ID. 然后,在“日历地址:”部分的底部,记下日历ID。 should be some kind of google .com email address or your email address if you set it up through the gSuite tools. 应该是某种google .com电子邮件地址,或者是您通过gSuite工具设置的电子邮件地址。

"YOURJSON.json" is the name of the json file you downloaded earlier. “ YOURJSON.json”是您先前下载的json文件的名称。 Suggestion to keep it safer is to upload it above the root directory on your site and link to it from there. 建议使其更安全,是将其上传到您网站的根目录上方,然后从那里链接到该目录。 Since it only has read roles it shouldn't cause any problems so more of a sanity just in case thing. 由于它只具有读取角色,因此不会造成任何问题,因此,以防万一。

require_once line is wherever you uploaded the google API to your server. require_once行是您将google API上传到服务器的位置。 Don't forget the /vendor/autoload.php part :). 不要忘记/vendor/autoload.php部分:)。

This is the solution that worked for me (2017-04-24). 这是为我工作的解决方案(2017-04-24)。 The last line of the PHP code is a general echo command so you can see the array that you get and all the data available. PHP代码的最后一行是常规的echo命令,因此您可以看到所获得的数组以及所有可用数据。 The only thing I don't see coming over is the custom color for an event. 我唯一看不到的是事件的自定义颜色。 There is a space for it but doesn't seem to come over. 有一个空间,但似乎没有消失。

<?php
$maxEvents = 100;
$minStartDate = date('c');
$calendarId = 'CALENDARID';

//path to the google API on your server
require_once 'inc/google-api-php-client-2.1.3_PHP54/vendor/autoload.php';
//set environment variable to use your downloaded Service account key
putenv("GOOGLE_APPLICATION_CREDENTIALS=YOURJSON.json");
$scope = 'https://www.googleapis.com/auth/calendar.readonly';

$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes($scope);
$service = new Google_Service_Calendar($client);

//options link
//  https://developers.google.com/google-apps/calendar/v3/reference/events/list
$options = array(
    'maxResults' => $maxEvents,
    'orderBy' => 'startTime',
    'singleEvents' => TRUE,
    //UNIX timestamp format
    'timeMin' => $minStartDate,
    //to use a calendar other than the default uncomment and enter the calendar's ID
    //not really needed here since you're using the $calendarId but does pull another calendar and more for completeness
    //'iCalUID' => 'CAL_ID_FROM_GOOGLE_CALENDAR'
);

$results = $service->events->listEvents($calendarId, $options);
//echo 'results<br><pre>';print_r($results); echo '</pre><br>';
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM