Php Google日历API错误重定向\u uri\u不匹配

Php Google日历API错误重定向\u uri\u不匹配,php,google-api,google-calendar-api,google-api-php-client,Php,Google Api,Google Calendar Api,Google Api Php Client,我正在尝试使用GoogleAPI来管理用户的日历,我发现了一个问题。我在google开发者控制台上创建并配置了一个google项目。其中一个设置是允许重定向URI。。。我认为这没关系,因为在谷歌抛出相同错误(重定向uri不匹配)的一些测试之后,我得到谷歌向我请求权限。。。我认为问题在于这行:$this->client->authenticate($\u GET['code']) 我将展示代码并解释它的功能 function __construct() { parent::__const

我正在尝试使用GoogleAPI来管理用户的日历,我发现了一个问题。我在google开发者控制台上创建并配置了一个google项目。其中一个设置是允许重定向URI。。。我认为这没关系,因为在谷歌抛出相同错误(重定向uri不匹配)的一些测试之后,我得到谷歌向我请求权限。。。我认为问题在于这行:$this->client->authenticate($\u GET['code'])
我将展示代码并解释它的功能

function __construct() 
{
    parent::__construct();
    require __DIR__ . '/vendor/autoload.php';

    define('APPLICATION_NAME', 'Google Calendar API PHP Quickstart');
    define('CLIENT_SECRET_PATH', __DIR__ . '/credentials/client_secret.json');
    define('CREDENTIALS_PATH', __DIR__ .'/credentials/');
    define('SCOPES', implode(' ', array(Google_Service_Calendar::CALENDAR_READONLY)));
    // If modifying these scopes, delete your previously saved credentials
    // at ~/.credentials/calendar-php-quickstart.json

    $this->client = new Google_Client();
    $this->client->setApplicationName(APPLICATION_NAME);
    $this->client->setScopes(SCOPES);
    $this->client->setAuthConfigFile(CLIENT_SECRET_PATH);

    if (!file_exists(CLIENT_SECRET_PATH.$this->session->userdata("identity").".json") && !$this->input->get("code"))
        $this->getCredentials();
}

public function responseCredentials()
{
    $authCode           = $this->input->get("code");
    $this->client->authenticate($_GET['code']);
    $accessToken        = $this->client->client->getAccessToken();
    $credentialsPath    = CLIENT_SECRET_PATH.$this->session->userdata("identity").".json";
    mkdir(dirname($credentialsPath), 0700, true);

    file_put_contents($credentialsPath, $accessToken);
    redirect(base_url("dashboard"));
}
private function getCredentials()
{
    $this->client->setRedirectUri(base_url('calendar/responseCredentials'));
    $authUrl = $this->client->createAuthUrl();
    header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));
}
好的。。。第一。。。它加载google api自动加载程序的构造函数和常量创建一个新的google_客户端对象,并检查用户是否存在权限文件,以及get上是否没有“代码”索引。
如果没有,则调用重定向到google的getCredentials函数。
授予权限后,用户将重定向到(在console.developers.google上配置)

引发的错误如下:

Fatal error: Uncaught exception 'Google_Auth_Exception' with message 
'Error fetching OAuth2 access token, message: 'redirect_uri_mismatch'' in 
/var/www/html/prototipo/application/controllers/vendor/google/apiclient/src/Google/Auth/OAuth2.php:126 
Stack trace: 
#0 /var/www/html/prototipo/application/controllers/vendor/google/apiclient/src/Google/Client.php(128): Google_Auth_OAuth2->authenticate('4/rkAKNAmiVgs1Z...', false) 
#1 /var/www/html/prototipo/application/controllers/calendar.php(52): Google_Client->authenticate('4/rkAKNAmiVgs1Z...') 
#2 [internal function]: Calendar->responseCredentials() 
#3 /var/www/html/prototipo/system/core/CodeIgniter.php(360): call_user_func_array(Array, Array) 
#4 /var/www/html/prototipo/index.php(202): require_once('/var/www/html/p...') 
#5 {main} thrown in/var/www/html/prototipo/application/controllers/vendor/google/apiclient/src/Google/Auth/OAuth2.php on line 126
我做错了什么??
多谢各位

编辑 我刚刚意识到,在返回uri上的代码变量的末尾总是有一个pad。。。类似这样的内容:

这里的示例:不显示此焊盘。。。我试着这样做:

$this->client->authenticate($_GET['code']."#");
但是。。。当然,它不起作用。

PS:我试着这么做是因为使用echo$_GET['code'];没有显示便笺簿。

您好,谢谢@thepieterdc最后您是

我在控制台上正确设置了项目。开发人员。。。但我的错误是,我需要做一些尝试,以获得正确的配置,当我要使错误消失400(与损坏的机器人),它要求我的许可,在重定向功能上,当我做$this->g_client->authenticate($_get['code']);代码尝试发出其他请求(在OAuth2.php上),并使用需要刷新的客户机_id.json。。。而我没有我使用的是第一版的CLIENT_ID.JSON如果您在控制台上更改了某些内容,我需要重新下载(或重新写入)


谢谢。

它可以是url末尾的小斜杠(/);验证是否100%正确。感谢@thepieterdc I查看代码和配置。。。但我没有发现任何问题。我发布了一个“答案”来提供更多的信息(太大以至于无法编辑)。