【发布时间】:2013-04-16 06:51:05
【问题描述】:
我正在尝试使用服务帐户从我的 Google 日历中获取活动。我收到了访问令牌:
{"access_token":"ya29.AHES6ZR9o2-cut-Gg","expires_in":3600,"created":1366631471}
现在此令牌已过期,当我尝试获取事件时,出现错误:
OAuth 2.0 访问令牌已过期,刷新令牌不可用。自动批准的响应不会返回刷新令牌。
我试图在 API 文档中找到获取新访问令牌的方法,但没有找到合适的方法。现在我有一个问题:我必须如何刷新我的访问令牌?
我用来访问日历的代码:
session_start();
require_once '../../src/Google_Client.php';
require_once '../../src/contrib/Google_CalendarService.php';
define('SERVICE_ACCOUNT_NAME', 'numbers-and-letters@developer.gserviceaccount.com');
define('CLIENT_ID', 'numbers-and-letters.apps.googleusercontent.com');
define('KEY_FILE', '../../key.p12');
$client = new Google_Client();
$client->setApplicationName("app name");
$client->setUseObjects(true);
$client->setClientID(CLIENT_ID);
$key = file_get_contents(KEY_FILE);
if (isset($_SESSION['token']))
{
$client->setAccessToken($_SESSION['token']);
$client->setaccessType('offline');
}
else
{
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/calendar.readonly'),
$key
));
}
try
{
$cal = new Google_CalendarService($client);
$events = $cal->events->listEvents('numbers-and-letters@group.calendar.google.com');
print_r($events);
} catch (Exception $e) echo $e->getMessage();
if ($client->getAccessToken()) {
$_SESSION['token'] = $client->getAccessToken();
}
【问题讨论】:
-
嗨,斯坦。欢迎来到 SO!与其将问题的答案添加为问题的附录,不如提交自己问题的答案并接受该答案。回答你自己的问题完全没问题。此外,该问题将不再出现在“没有答案的问题”部分,这有助于过滤此类问题。当然,这样一来,对于与您有相同问题的未来用户来说,答案会更加明显。
-
好的!感谢您的澄清
标签: google-calendar-api google-oauth google-api-php-client