简体   繁体   English

google 上的身份验证:OAuth2 不断返回“invalid_grant”

[英]Authentication on google: OAuth2 keeps returning 'invalid_grant'

I started to configure google calendar on my new application.我开始在我的新应用程序上配置谷歌日历。 I almost made an exact copy of the authentication code displayed at google developers ( https://developers.google.com/google-apps/calendar/instantiate ), but i keep getting the following error:我几乎完全复制了谷歌开发人员 ( https://developers.google.com/google-apps/calendar/instantiate ) 上显示的身份验证代码,但我不断收到以下错误:

Error fetching OAuth2 access token, message: 'invalid_grant'获取 OAuth2 访问令牌时出错,消息:“invalid_grant”

I'm currently using Fork-CMS ( http://www.fork-cms.com ), a young lightweigth CMS.我目前正在使用 Fork-CMS ( http://www.fork-cms.com ),一个年轻的轻量级 CMS。 I correctly configured the config.php file of the google-api-php-client.我正确配置了 google-api-php-client 的 config.php 文件。 (client-id, client-secret, redirect-uri, development key,...) and the redirect uri is correctly set on the google api's console. (client-id, client-secret, redirect-uri, development key,...) 并且在 google api 的控制台上正确设置了重定向 uri。 My code looks as follows:我的代码如下所示:

<?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);
    }
}

?>

When I open this widget-page for the first time, I get directed to google to authenticate the application.当我第一次打开这个小部件页面时,我被引导到谷歌来验证应用程序。 When I agree, I get redirected to my application and that's the point where I'm getting:当我同意时,我会被重定向到我的应用程序,这就是我得到的地方:

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

You should reuse the access token you get after the first successful authentication.您应该重用第一次成功身份验证后获得的访问令牌。 You will get an invalid_grant error if your previous token has not expired yet.如果您之前的令牌尚未过期,您将收到invalid_grant错误。 Cache it somewhere so you can reuse it.将它缓存在某个地方,以便您可以重用它。

I was having a similar problem caused by the time on my server being incorrect.由于服务器上的时间不正确,我遇到了类似的问题。 Make sure your system clock is synchronised.确保您的系统时钟已同步。

Go to your Google API Console ( https://code.google.com/apis/console/ ) and revoke your Client Secret under Client ID for installed applications . Go 到您的 Google API 控制台 ( https://code.google.com/apis/console/ ),并在已安装应用程序的客户端 ID下撤销您的客户端密码。

Be sure to also update your code with the new Client Secret确保还使用新的客户端密钥更新您的代码

  1. Go to security.google.com Go 到 security.google.com
  2. Revoke access撤销访问
  3. visit the authentication url again再次访问认证url
  4. you will now get a new refreshtoken您现在将获得一个新的刷新令牌

You'll also receive this error if you mistakenly try to authenticate your ID Token , rather than your Access Token .如果您错误地尝试验证您的ID Token而不是您的Access Token ,您也会收到此错误。

So don't be like me - Make sure you pass the correct token into your code!所以不要像我一样 - 确保将正确的令牌传递到代码中!

I had a similar issue.我有一个类似的问题。 The problem with "invalid_grant" is that it's basically a placeholder for any error that occurs in relation to the token. “invalid_grant”的问题在于它基本上是与令牌相关的任何错误的占位符。 I found this article to be really helpful.我发现这篇文章真的很有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OAuth2 Google服务帐户invalid_grant - OAuth2 Google Service Account invalid_grant Google Oauth2:刷新OAuth2令牌时出错,消息:&#39;{“错误”:“ invalid_grant”}&#39; - Google Oauth2: Error refreshing the OAuth2 token, message: '{ “error” : “invalid_grant” }' Google服务帐户示例返回“刷新OAuth2令牌时出错{”错误“:”invalid_grant“}” - google service account example returns “Error refreshing the OAuth2 token { “error” : “invalid_grant” }” Google_Auth_Exception&#39;,消息为&#39;获取 OAuth2 访问令牌时出错,消息:&#39;invalid_grant&#39; - Google_Auth_Exception' with message 'Error fetching OAuth2 access token, message: 'invalid_grant' PHP Google API OAuth2 JWT 对新访问令牌的请求显示“invalid_grant” - PHP Google API OAuth2 JWT request for new acces token says “invalid_grant” Google_Auth_Exception:刷新OAuth2令牌时出错,消息:&#39;{“错误”:“ invalid_grant”}&#39; - Google_Auth_Exception: Error refreshing the OAuth2 token, message: '{ “error” : “invalid_grant” }' 请求 oauth2 令牌时获取 invalid_grant - Getting invalid_grant when requesting oauth2 token Google SSO aouth 2.0 提供错误获取 OAuth2 访问令牌,消息:“invalid_grant:代码已被赎回。” - Google SSO aouth 2.0 gives Error fetching OAuth2 access token, message: 'invalid_grant: Code was already redeemed.' Google API OAuth2.0错误请求invalid_grant - Google API OAuth2.0 bad request invalid_grant invalid_grant:代码已被兑换为Google Oauth - invalid_grant: Code was already redeemed Google Oauth
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM