如何在Laravel中将Google日历与API集成?

时间:2013-08-22 20:03:11

标签: php google-api oauth-2.0 laravel

我想在我的网络应用中构建Google日历界面,并将日历用作事件的存储空间,因为这也会提供其他服务。我知道我需要获取授权令牌,然后获取应用程序的刷新令牌以使用API​​。我在谷歌上设置了应用程序并获取了PHP代码以帮助获取令牌。

然而,我真的很难理解我正在做什么以及代码正在做什么,并且没有找到关于这个主题的白痴指导作为一步一步...我可能犯了基本错误但不是确定它们是什么。

我发现这篇文章:Google Calendar API v3 hardcoded credentials我试图效仿。

我运行了第一个脚本:

<?php
$scope         =   'https://www.google.com/calendar/feeds/';
$client_id      =   'my id';
$redirect_uri   =   'my redirect url';

$params = array(
                'response_type' =>   'code',
                'client_id'     =>   $client_id,
                'redirect_uri'  =>   $redirect_uri,
                'scope'         =>   $scope
                );
$url = 'https://accounts.google.com/o/oauth2/auth?' . http_build_query($params);        
echo $url."\n";

问题:我从Google API复制了重定向URI。这包括一些代码以及另一行http://localhost。我是复制两行还是只复制一行。无论如何,这应该是我的应用程序定制。这个参数有什么作用? -

我现在在本地计算机上运行,​​服务器名称为downsadmin.loc。我的目的是在构建后最终在实时服务器上运行。

我使用Google API提供的uri运行脚本,但它出现了错误:

  

redirect_uri的参数值无效:缺少权限:urn:ietf:wg:oauth:2.0:oob http://localhost

所以我认为我的重定向URI是错误的?

我刚刚下载了Google PHP库。我在这里运行了这个例子:

$client->setClientId('my id');
$client->setClientSecret('my key_');
$client->setRedirectUri('uri
');
$client->setDeveloperKey('my developer key');
$plus = new Google_PlusService($client);

if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['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']);
}

if ($client->getAccessToken()) {
$activities = $plus->activities->listActivities('me', 'public');
print 'Your Activities: <pre>' . print_r($activities, true) . '</pre>';

// We're not done yet. Remember to update the cached access token.
// Remember to replace $_SESSION with a real database or memcached.
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
print "<a href='$authUrl'>Connect Me!</a>";
}

再次出现错误:

  

redirect_uri的参数值无效:缺少权限:urn:ietf:wg:oauth:2.0:oob http://downsadmin.loc/google/test_googleplus.php

任何帮助和指示都表示赞赏 - 尤其是一步一步的基本链接,了解如何做以及为什么 - 然后接下来的步骤如何开始使用API​​。

1 个答案:

答案 0 :(得分:1)

确定

更多关于绝望的研究,我在oauth 2上找到了这个教程。我在第2步中已经按照了这个,并设法得到了我的刷新令牌!

http://cornempire.net/2012/01/08/part-2-oauth2-and-configuring-your-application-with-google/

我最初确实返回了一个空白令牌,需要根据帖子中用户“Bac”的评论将以下内容添加到脚本中:

要解决空的$ cRefreshToken问题,您需要通过添加

来启用SSL
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_exec($ch);之前

现在开始使用API​​

相关问题