我開始在我的新應用程序上配置谷歌日曆。我差點讓在谷歌開發者(https://developers.google.com/google-apps/calendar/instantiate)中顯示的驗證碼的精確副本,但我不斷收到以下錯誤:谷歌驗證:OAuth2不斷返回'invalid_grant'
錯誤獲取的OAuth2訪問令牌,消息:「invalid_grant」
我目前使用Fork-CMS(http://www.fork-cms.com),一個年輕的CMS。我正確地配置了google-api-php-client的config.php文件。 (client-id,client-secret,redirect-uri,development key,...),並且在google api的控制檯上正確設置了redirect uri。我的代碼如下:
<?php
/**
* This is a widget with a calendar implementation.
*
* @package frontend
* @subpackage events
*
* @author Michiel Vlaminck <[email protected]>
*/
class FrontendEventsWidgetCalendar extends FrontendBaseWidget
{
private $events = array();
private $authUrl = array();
/**
* Execute the extra
*
* @return void
*/
public function execute()
{
// call parent
parent::execute();
// load template
$this->loadTemplate();
// get data
$this->getData();
// parse
$this->parse();
}
/**
* Get the data from Google Calendar
* This method is only executed if the template isn't cached
*
* @return void
*/
private function getData()
{
require_once PATH_LIBRARY . '/external/google-api-php-client/src/apiClient.php';
require_once PATH_LIBRARY . '/external/google-api-php-client/src/contrib/apiCalendarService.php';
$client = new apiClient();
$service = new apiCalendarService($client);
if (isset($_SESSION['oauth_access_token'])) {
$client->setAccessToken($_SESSION['oauth_access_token']);
} else {
$token = $client->authenticate();
$_SESSION['oauth_access_token'] = $token;
}
if ($client->getAccessToken()) {
$calId = FrontendEventsModel::getCalendarId((int) $this->data['id']);
$calId = $calId[0]['calendar_id'];
$events = $service->events->listEvents($calId);
$this->events = $events['items'];
$_SESSION['oauth_access_token'] = $client->getAccessToken();
} else {
$this->authUrl = $client->createAuthUrl();
}
}
/**
* Parse
*
* @return void
*/
private function parse()
{
$this->tpl->assign('events', $this->events);
$this->tpl->assign('authUrl', $this->authUrl);
}
}
?>
當我打開這個小部件頁的第一次,我被定向到谷歌認證的應用程序。當我同意,我重定向到我的應用程序,這就是我得到了這一點:
apiAuthException » Main
Message Error fetching OAuth2 access token, message: 'invalid_grant'
File C:\wamp\www\Officevibes\library/external\google-api-php-client\src\auth\apiOAuth2.php
Line 105
Date Thu, 05 Apr 2012 08:34:47 +0000
URL http://localhost/calendar?code=4/YPUpFklKvhEeTcMm4moRth3x49oe
Referring URL (Unknown)
Request Method GET
User-agent Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.142 Safari/535.19
它現在稱爲「重置您的客戶端密鑰」而不是撤銷。 – jenlampton 2013-03-30 01:40:36
在我的情況下,我曾使用以前從控制檯p12密鑰文件刪除... – yergo 2015-06-09 08:56:14