我需要制作一个 PHP 脚本来在 Google 日历上创建单个事件。
我在设置客户端 ID、客户端密码、开发密钥和创建新事件时没有遇到任何问题。
我唯一的问题是 OAuth2,特别是我需要建立永久连接,并且我不想每次运行脚本时都进行身份验证。
实际上,通过这个脚本,我可以获得一个令牌和一个刷新令牌,但是我的令牌每小时都会过期,我不知道如何刷新它。我如何编辑这段代码来做到这一点?
我可以将令牌和刷新令牌保存在某处并始终使用相同的数据吗?
我收到未捕获的异常“Google_AuthException”,消息为“刷新 OAuth2 令牌时出错,消息:”{“error”:“invalid_grant”
我已经在 stackoverflow 中阅读了有关此主题的其他一些帖子,但我仍然没有找到解决方案...:\
<?php
require_once 'src/Google_Client.php';
require_once 'src/contrib/Google_CalendarService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Calendar App");
$client->setClientId('xxxx');
$client->setClientSecret('yyy');
$client->setRedirectUri('http://www.zzzzzz');
$client->setDeveloperKey('kkk');
$client->setAccessType('offline');
$cal = new Google_CalendarService($client);
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['token'])) {
//echo $_SESSION['token'];
//$client->setAccessToken($_SESSION['token']);
$authObj = json_decode($_SESSION['token']);
$accessToken = $authObj->access_token;
$refreshToken = $authObj->refresh_token;
$tokenType = $authObj->token_type;
$expiresIn = $authObj->expires_in;
echo 'access_token = ' . $accessToken;
echo '<br />';
echo 'refresh_token = ' . $refreshToken;
echo '<br />';
echo 'token_type = ' . $tokenType;
echo '<br />';
echo 'expires_in = ' . $expiresIn;
}
if(!empty($cookie)){
$client->refreshToken($this->Cookie->read('token'));
}
if ($client->getAccessToken()) {
$calList = $cal->calendarList->listCalendarList();
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
// Creation of a single event
$event = new Google_Event();
$event->setSummary($event_name);
$event->setLocation('');
....
?>
多谢您的支持!
这一页https://developers.google.com/accounts/docs/OAuth2WebServer#offline解释刷新令牌的工作原理,以及如何使用它通过原始 http 获取新的访问令牌。
从这个问题如何使用 Google API 客户端刷新令牌?这是 php 的等价物
here is the snippet to set token, before that make sure the access type should be set to offline
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['access_token'] = $client->getAccessToken();
}
To refresh token
$google_token= json_decode($_SESSION['access_token']);
$client->refreshToken($google_token->refresh_token);
this will refresh your token, you have to update it in session for that you can do
$_SESSION['access_token']= $client->getAccessToken()
调试按照以下三个步骤调试任何 oauth 应用程序
确保您已阅读https://developers.google.com/accounts/docs/OAuth2以及适当的子页面(webapp、javascript 等),具体取决于您使用的 oauth 风格。如果您不了解应用程序在做什么,那么您将无法深入调试应用程序。
使用 Oauth Playground,位于https://developers.google.com/oauthplayground/完成 Oauth 步骤并最终进行 Google API 调用。这将确认您的理解并向您展示 http 调用和响应的样子
使用跟踪/日志记录/代理等来跟踪应用程序的实际 http 流量。将其与您在步骤 2 中观察到的情况进行比较。这应该会告诉您问题所在。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)