谷歌API的PHP日历 [英] google api calendar php

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

问题描述

我知道很多类似的问题已经被问过,但我努力理解。我已经成功地使用了谷歌的PHP库V3与日历功能接口。在code我是:

I am aware many similar questions have been asked, but I am struggling to understand. I have successfully used the php google libraries for v3 to interface with calendar functions. The code I have is:

<?php 

require_once "google-api-php-client/autoload.php";
session_start();

$client = new Google_Client();
$client->setApplicationName("My app");
$client->setClientId("CI.apps.googleusercontent.com");
$client->SetClientSecret("SECRET");
$client->setRedirectUri("redirect"); 
$client->setDeveloperKey("key");
$client->setScopes(array("https://www.googleapis.com/auth/calendar"));

if (isset($_REQUEST['logout'])) {
  unset($_SESSION['access_token']);
}


if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}


if (isset($_SESSION['token'])) {
   $client->setAccessToken($_SESSION['token']);//update token
  }

$service=new Google_Service_Calendar($client);  
...
?>

这工程确定,但我想修改日历始终是应用程序在同一帐户作为日历注册same-。是否有办法一轮的oauth2认证,这样我就可以调整我自己的日历中的条目,而无需执行与重定向一个额外的验证步骤?我以前用的Zend做到这一点,它工作得很好,直到最近,但更新到API的V3和使用的oauth2这样似乎有点矫枉过正。我可能误解了,当然 - 建议的任何帮助将我最有帮助。

This works ok, but the calendar I want to modify is always the same- the app is registered at the same account as the calendar. Is there a way round the oauth2 authentication so that I can just adjust the entries in the calendar which I own without having to perform an extra authentication step with redirect? I used to use Zend to do this and it worked fine until recently, but updating to v3 of the API and using oauth2 like this seems a bit overkill. I may have misunderstood of course - any help of advice would me most helpful.

推荐答案

确定 - 在回答这个情况下,任何人的搜索,我想我会发布我的解决方案。我是有点笨。如果你想验证的应用程序以这种方式来修改一个日历,你需要在谷歌控制台创建一个服务帐户 - 而不是一个Web应用程序。然后,你需要使用的服务帐户名(给服务帐户的客户ID电子邮件地址),以允许脚本修改日历。通过将要修改的日历设置手动完成。那么这个code将正常工作。

Ok - in case anyone else searches for the answer to this I thought I'd post my solution. I was a bit stupid. If you want to authenticate an application to modify a calendar in this way you need to create a service account in the google console - not a web application. Then you need to use the service account name (e-mail address given to the Client ID for the service account) to allow the script to modify the calendar. Do this manually by going to the setting of the calendar you want to modify. This code will then work fine.

<?php 
session_start();
require_once "google-api-php-client/autoload.php";

$client_id = ''; //Client ID
$service_account_name = ''; //Email Address
$key_file_location = ''; //key.p12
$client = new Google_Client();
$client->setApplicationName("my app");
$service = new Google_Service_Calendar($client);

if (isset($_SESSION['service_token'])) {
  $client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
                         $service_account_name,
                         array('https://www.googleapis.com/auth/calendar'),
    $key
                         );
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();?>

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

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