Google 日历 API 错误 redirect_uri_mismatch

Google Calendar API error redirect_uri_mismatch

我正在尝试使用 google API 来管理用户的日历,但我发现了一个问题。我在 Google Developers Console 上创建并配置了一个 google 项目。其中一项设置是允许重定向 uris...我认为没问题,因为在 google 抛出相同错误 (redirect_uri_mismatch) 的一些测试之后,我得到 google 问我的权限...我认为问题是这一行: $this->client->authenticate($_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_Client对象,并检查是否存在用户的权限文件,get 上没有 "code" 索引。
如果不是,它会调用重定向到 google.
的 getCredentials 函数 授予权限后,用户将重定向到 http://domain.com/calendar/responseCredentials(它是在 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

我做错了什么??
谢谢。

编辑

我刚刚意识到在 return uri 上的代码变量的末尾总是有一个垫...像这样的东西:
http://pedro.eatec.es/prototipo/calendar_test_stack/responseCredentials?code=4/PL7nK1s9m5vpBow7HScaPmkpWpoW3J4uzUxlD7NE49g#
这里的示例:https://developers.google.com/identity/protocols/OAuth2WebServer#handlingresponse 不显示此垫...我尝试这样做:

$this->client->authenticate($_GET['code']."#");

但是...当然,行不通。
PS:我尝试这样做是因为有了 echo $_GET['code'];没有显示垫。

你好,谢谢@thepieterdc 最后你是理由...

我在 console.developers 上正确设置了项目...但我的错误是我需要进行一些尝试以获得正确的配置,当我必须使错误 400 消失时(损坏的robot) 并在我进行 $this->g_client->authenticate($_GET['code']); 时就重定向功能询问我的许可该代码尝试向 https://accounts.google.com/o/oauth2/token 发出其他请求(在 OAuth2.php 上),它使用了您需要刷新的 client_id.json...但我没有。 我正在使用 CLIENT_ID.JSON 的第一版,如果您在控制台上更改了某些内容,我需要重新下载(或重新编写)。

谢谢。