gpt4 book ai didi

php - 谷歌认证 : OAuth2 keeps returning 'invalid_grant'

转载 作者:IT王子 更新时间:2023-10-29 00:00:57 27 4
gpt4 key购买 nike

我开始在我的新应用程序上配置谷歌日历。我几乎完全复制了谷歌开发人员 ( https://developers.google.com/google-apps/calendar/instantiate ) 上显示的身份验证代码,但我不断收到以下错误:

获取 OAuth2 访问 token 时出错,消息:“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 的控制台上正确设置了重定向 uri。我的代码如下所示:

<?php

/**
* This is a widget with a calendar implementation.
*
* @package frontend
* @subpackage events
*
* @author Michiel Vlaminck <michielvlaminck@gmail.com>
*/
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

最佳答案

您应该重用您在第一次成功验证后获得的访问 token 。如果您之前的 token 尚未过期,您将收到 invalid_grant 错误。将其缓存在某个地方,以便您可以重复使用它。

关于php - 谷歌认证 : OAuth2 keeps returning 'invalid_grant' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10025698/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com