Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP Gmail API无效授权:代码已赎回(Codeigniter)_Php_Codeigniter_Gmail_Gmail Api - Fatal编程技术网

PHP Gmail API无效授权:代码已赎回(Codeigniter)

PHP Gmail API无效授权:代码已赎回(Codeigniter),php,codeigniter,gmail,gmail-api,Php,Codeigniter,Gmail,Gmail Api,首先,我想说的是,我读了所有其他关于这个问题的帖子,但我找不到解决办法 这就是错误: 异常“Google_Auth_exception”,消息为“获取OAuth2访问令牌时出错,消息为:“无效授权:代码已被兑换。” 在localhost中,它工作得非常好。我可以调试它,我可以毫无问题地获得Google plus和电子邮件信息 我为生产Web应用程序创建了一个新凭据,并在:Authorized redirect uri中设置了我的oauth返回uri 它可以工作,因为我可以接受并在回调中获取代码

首先,我想说的是,我读了所有其他关于这个问题的帖子,但我找不到解决办法

这就是错误:

异常“Google_Auth_exception”,消息为“获取OAuth2访问令牌时出错,消息为:“无效授权:代码已被兑换。”

在localhost中,它工作得非常好。我可以调试它,我可以毫无问题地获得Google plus和电子邮件信息

我为生产Web应用程序创建了一个新凭据,并在:Authorized redirect uri中设置了我的oauth返回uri

它可以工作,因为我可以接受并在回调中获取代码,但当我尝试验证时失败

我还试图撤销Token,但我的会话为空

我怎样才能解决这个问题

非常感谢

这是我的代码:

function __construct()
{
    define('CLIENT_SECRET_PATH', 'client_secret_online.json');
    $this->redirect_url=base_url()."plus_gmail/oauth2callback";
    $this->client = new Google_Client();
    $this->client->setIncludeGrantedScopes(true);
    $this->client->setAuthConfigFile(CLIENT_SECRET_PATH); 
    $this->client->addScope(Google_Service_Plus::PLUS_LOGIN);
    $this->client->addScope(Google_Service_Plus::PLUS_ME);
    $this->client->addScope(Google_Service_Plus::USERINFO_EMAIL);
    $this->client->addScope(Google_Service_Plus::USERINFO_PROFILE);
    $this->client->addScope(Google_Service_Gmail::MAIL_GOOGLE_COM);
    $this->client->setRedirectUri($this->redirect_url);
    $this->client->setAccessType('offline');
    $this->client->setIncludeGrantedScopes(true);
}

function index()
{
    $auth_url = $this->client->createAuthUrl();
    header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
}

function oauth2callback()
{
    if (! isset($_GET['code'])) 
    {
        echo "Error getting code"
    } 
    else
    {
        $token=$_GET['code'];
        $data["token"]=$token;

        try
        {
            $this->client->authenticate($token);
            $access_token = $this->client->getAccessToken();

            $plus = new Google_Service_Plus($this->client);
            $me=$plus->people->get('me');

            $this->daouser->newUserPlus($me,$access_token);
            var_dump($me);
            $this->readEmailsId($this->client);
        }
        catch (Exception $e)
        {
            var_dump($e)
        }             
    }
}
我还需要知道为什么它会显示var_dump$e,但我的数据库中有一个新的行,其中有e用户google plus info


我在我的gmail帐户中删除了对我的应用程序的访问权限,并再次授予访问权限,因为相同的错误代码已被兑换。我使用了一个新的gmail帐户,我也收到了同样的错误…

代码已经被兑换了。尝试使用已使用的授权代码时引发错误。给定的授权码只能使用一次。但是为什么我的数据库中有所有的access\u令牌,包括refresh\u令牌?这意味着它是有效的!但它显示了一个例外。我在想,可能是两次调用我的回调,我不知道为什么…这意味着您正在使用身份验证代码而不是刷新令牌来获取新的访问令牌。身份验证代码仅用于获取刷新令牌一次。然后它将不再工作。不,我用新用户测试它,我的数据库中有一个包含他的信息的新行,但它会打印异常。我不知道为什么,但它似乎叫了两次。第一次很好,但是第二次它使用的是相同的代码,所以它表明代码已经被兑换了…正是如此,请修复您的代码,这样您就可以停止兑换两次,并且您已经准备好了。有可能是$token=$\u GET['code'];?