Google Calendar API v3 PHP和JSON [英] Google Calendar API v3 PHP and JSON

查看:56
本文介绍了Google Calendar API v3 PHP和JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

拥有一个公开的Google日历,我要做的就是将事件从今天"获取到最多n(或最多)个事件.我一直在兜圈子,试图找出答案,这要么是已弃用的信息,要么是似乎无法使用.一个好的解决方案是使用PHP生成具有CSS优势和javascript数组的事件的初始列表,以便当用户单击事件时可以更新详细信息DIV.

Have a public Google Calendar where all I want to do is get the events from "today" up to n (or max) events. 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并将最新的API上传到服务器,拥有一个带有只读帐户,OAuth 2.0客户端ID和API密钥的日历项目.与一些教程紧密联系,但遇到了麻烦,因为您需要通过命令行生成的"./credentials/calendar-api-quickstart.json".我必须使用的服务器是共享服务器,因此没有命令行访问权限(出于显而易见的原因:)).使用了 https://developers.google.com/google-apps/calendar/quickstart/php 信息.那似乎是我唯一想念的东西?

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. 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. 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. That seems to be the the only piece i'm missing?

也通过此页面上的信息激活了密钥 Google Calendar API v3硬编码凭据,但出现"invalid_grant"错误.我已经尝试了搜索中出现的所有内容,但似乎没有任何效果.是否有准确的最新教程(2016-2017年)指导您完成如何将Google日历数据导入您的网站?从设置项目到oauth2和令牌生成.我必须弄乱一些东西,因为似乎什么也没用.

Activated the key too through the info on this page Google Calendar API v3 hardcoded credentials but getting an "invalid_grant" error. 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? From setting up the project to oauth2 and token generation. I've got to be messing something up because nothing seems to work.

唯一的其他解决方案是让他们将日历导出为.ics,但他们希望在更新Google日历时进行实时更新.

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.

因为我只是想读取数据,所以我在这里尝试了建议从公共Google日历获取JSON .但是我收到了Forbidden Error 403错误,因此已弃用?

Because i'm just looking to read the data I tried the suggestion here Get JSON from a public Google Calendar. But I get the Forbidden Error 403 error so it's depretiated?

仍然不理解为什么它们变得如此复杂...如果您想与您的网站或应用上的日历进行交互,而只是读取公共日历上的数据(我想了解所有这些内容,那么我会理解所有的oauth内容)警告您将其公开,以便所有人都可以看到)?

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)?

谢谢

推荐答案

最终找到了解决方案.在这里发布,如果我有空的话,以后可能会做一个完整的教程:).希望它可以帮助其他人.

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 创建一个在创建项目后应带您进入控制台的项目...如果没有,则是控制台链接 https: //console.developers.google.com/

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/

启用"Google Calendar API".搜索Google Calendar API,选择它并使用顶部显示的启用按钮.

in the library section enable the "Google Calendar API". Search for Google Calendar API, select it and use the enable button that appears at the top.

转到凭据部分,然后添加服务帐户密钥.新服务帐户(您想要的任何名称).

Go to the credentials section and add a Service account key. New service account (any name you want).

劳斯:

项目>查看器

App Engine> App Engine查看器

App Engine > App Engine Viewer

存储>存储对象查看器

您可能不需要所有这些,但这似乎是对我有用的组合.确保选择了JSON,然后在创建时将下载.json文件.它只能下载一次,而无法重新下载,因此请确保将其下载并保存在安全的地方.

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. It only downloads it once with no way to redownload so make sure you download and save it somewhere safe.

https://developers.google.com/上下载最新的PHP API. google-apps/calendar/downloads 并将其上传到您网站上的任何位置.很大,有很多支持文件,因此可能需要一段时间才能上传.

Download the latest API for PHP here https://developers.google.com/google-apps/calendar/downloads and upload it to wherever on your site. Fairly large with lots of support files so may take a while to upload.

然后这是为我工作的PHP代码:)

Then this is the PHP code that worked for me :)

CALENDARID 是您日历的ID ... 从您的Google日历中,使用弹出菜单,然后选择日历设置".然后,在日历地址:"部分的底部,记下日历ID.应该是某种google .com电子邮件地址,或者是您通过gSuite工具设置的电子邮件地址.

CALENDARID is the id of your calendar... from your google calendar, use the popup and choose "calendar settings". Then towards the bottom in the "Calendar Address:" section make note of the Calendar ID. should be some kind of google .com email address or your email address if you set it up through the gSuite tools.

"YOURJSON.json" 是您先前下载的json文件的名称.建议使其更安全,是将其上传到您网站的根目录上方,然后从那里链接到该目录.由于它只具有读取角色,因此不会造成任何问题,所以更多的情况是万一的以防万一.

"YOURJSON.json" is the name of the json file you downloaded earlier. 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 行是您将google API上传到服务器的位置.不要忘记/vendor/autoload.php部分:).

require_once line is wherever you uploaded the google API to your server. Don't forget the /vendor/autoload.php part :).

这是为我工作的解决方案(2017-04-24). PHP代码的最后一行是常规的echo命令,因此您可以看到所获得的数组以及所有可用数据.我唯一看不到的是事件的自定义颜色.有一个空间,但似乎没有消失.

This is the solution that worked for me (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. 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>';
?>

这篇关于Google Calendar API v3 PHP和JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆