使用PHP连接到Google Calendar v3 API
我完全迷路了。。。我所要做的就是摆脱这个错误消息并登录到GoogleAPI。我已经创建了所有键并将它们添加到/src/Google/Config.php使用PHP连接到Google Calendar v3 API,php,google-calendar-api,google-api-php-client,Php,Google Calendar Api,Google Api Php Client,我完全迷路了。。。我所要做的就是摆脱这个错误消息并登录到GoogleAPI。我已经创建了所有键并将它们添加到/src/Google/Config.php PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com/calendar/v3/users/me/calendarList/0qfrti6gst4
PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling GET
https://www.googleapis.com/calendar/v3/users/me/calendarList/0qfrti6gst4q8hpl89utm8elno%40grou p.calendar.google.com?key=AIzaSyAiR_4OsoheCPbd7tU2u3QrqbEW_a2uVCc: (401) Login Required'
in C:\\Bitnami\\wampstack\\apache2\\htdocs\\yac\\google-api-php-
client\\src\\Google\\Http\\REST.php:79\nStack ...
以下是我(很可能)的错误脚本:
session_start();
set_include_path( get_include_path() . PATH_SEPARATOR . 'google-api-php-client/src' );
include_once('google-api-php-client/src');
require_once('google-api-php-client/src/Google/Client.php');
require_once ('google-api-php-client/src/Google/Service/Calendar.php');
$client = new Google_Client();
$client->setApplicationName("YAC_Calendar");
$client->setDeveloperKey(<MY KEY>);
$service = new Google_Service_Calendar($client);
$calendarListEntry = $service->calendarList->get('0qfrti6gst4q8hpl89utm8elno@group.calendar.google.com');
echo $calendarListEntry->getSummary();
session_start();
设置_include_path(get_include_path().path_分隔符'google api php client/src');
包括_once('google-api-php-client/src');
require_once('google-api-php-client/src/google/client.php');
require_once('google-api-php-client/src/google/Service/Calendar.php');
$client=新的Google_客户端();
$client->setApplicationName(“YAC_日历”);
$client->setDeveloperKey();
$service=新谷歌服务日历($client);
$calendarListEntry=$service->calendarList->get('0qfrti6gst4q8hpl89utm8elno@group.calendar.google.com');
echo$calendarListEntry->getSummary();
在访问日历之前,您需要使用Oauth登录。我现在手头上唯一的例子是一个使用Google Analytics API和PHP的例子,如果你有任何切换到Calendar的方法,请告诉我,我会看看是否可以得到一个有效的例子
<?php
require_once 'Google/Client.php';
require_once 'Google/Service/Analytics.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setDeveloperKey("{devkey}");
$client->setClientId('{clientid}.apps.googleusercontent.com');
$client->setClientSecret('{clientsecret}');
$client->setRedirectUri('http://www.daimto.com/Tutorials/PHP/Oauth2.php');
$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
//For loging out.
if ($_GET['logout'] == "1") {
unset($_SESSION['token']);
}
// Step 2: The user accepted your access now you need to exchange it.
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
// Step 1: The user has not authenticated we give them a link to login
if (!$client->getAccessToken() && !isset($_SESSION['token'])) {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
// Step 3: We have access we can now create our service
if (isset($_SESSION['token'])) {
print "<a class='logout' href='".$_SERVER['PHP_SELF']."?logout=1'>LogOut</a><br>";
$client->setAccessToken($_SESSION['token']);
$service = new Google_Service_Analytics($client);
// request user accounts
$accounts = $service->management_accountSummaries->listManagementAccountSummaries();
foreach ($accounts->getItems() as $item) {
echo "Account: ",$item['name'], " " , $item['id'], "<br /> \n";
foreach($item->getWebProperties() as $wp) {
echo ' WebProperty: ' ,$wp['name'], " " , $wp['id'], "<br /> \n";
$views = $wp->getProfiles();
if (!is_null($views)) {
foreach($wp->getProfiles() as $view) {
// echo ' View: ' ,$view['name'], " " , $view['id'], "<br /> \n";
}
}
}
} // closes account summaries
}
print "<br><br><br>";
print "Access from google: " . $_SESSION['token'];
?>
我的教程中的代码我很确定我在以前的搜索中看到了这一点,但我并不知道我在寻找什么。谢谢你把我带到正确的地方!即使应用程序只与我的帐户一起使用,我也需要创建OAuth吗?这不是简单的API案例吗?我的应用程序是服务器端的,我不需要询问用户登录的用户详细信息。。它只影响我的google+帐户。。那么,设置应用程序名和开发者密钥就足够了吗?提前谢谢