1

我想在我的网络应用程序中构建一个谷歌日历界面,并将日历用作事件的存储,因为这也提供其他服务。我知道我需要获取授权令牌,然后是应用程序使用 API 的刷新令牌。我已经在 Google 上设置了应用程序并获取了 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:oobhttp://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。

4

1 回答 1

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

于 2013-08-22T23:48:43.633 回答