首页 > 解决方案 > 谷歌表格 PHP API redirect_uri_mismatch 错误

问题描述

我正在尝试进行身份验证以使用 Google Sheets API。我开始对此失去理智,因为我已经被这个问题困扰了两天。

在我的 Google 控制台中,我已将 Credentials -> OAuth 2.0 Client IDs -> Authorized redirect URIs 设置为https://stage.domain.com/https://stage.domain.com

当我访问 时https://stage.domain.com,我被重定向到 accounts.google.com 以授权我的应用程序。我选择我的帐户并单击“允许”按钮。我被重定向到https://stage.domain.com/?code=4/...&scope=https://www.googleapis.com/auth/spreadsheets.readonly并获取{"error":"redirect_uri_mismatch","error_description":"Bad Request"}from$client->fetchAccessTokenWithAuthCode函数。

我究竟做错了什么?我正在运行 PHP,并且正在使用google/apiclient^2.0Composer。

 $configPath = __DIR__ . "/";

 // This get's generated by the script, so don't create it
 $credentialsPath = $configPath . 'credentials.json';

 $client = new Google_Client();

 // Matches the "Application Name" you enter during the "Step 1" wizard
 $client->setApplicationName( 'App name matching Google Console name' );
 $client->setScopes( Google_Service_Sheets::SPREADSHEETS_READONLY );

 // You need to go through "Step 1" steps to generate this file: https://developers.google.com/sheets/api/quickstart/php
 $client->setAuthConfig( $configPath . 'secret.json' );
 $client->setAccessType( 'offline' );

 $actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

 // This must match the "callback URL" that you enter under "OAuth 2.0 client ID" in the Google APIs console at https://console.developers.google.com/apis/credentials
 $client->setRedirectUri( $actual_link );

 // We have a stored credentials file, try using the data from there first
 if ( file_exists( $credentialsPath ) ) {
    $accessToken = json_decode( file_get_contents( $credentialsPath ), true );
 }

 // No stored credentials found, we'll need to request them with OAuth
 else {

    // Request authorization from the user
    $authUrl = $client->createAuthUrl();
    if ( ! isset( $_GET['code'] ) ) {
       header( "Location: $authUrl", true, 302 );
       exit;
    }

    // The authorization code is sent to the callback URL as a GET parameter.
    // We use this "authorization code" to generate an "access token". The
    // "access token" is what's effectively used as a private API key.
    $authCode = $_GET['code'];
    $accessToken = $client->fetchAccessTokenWithAuthCode( $authCode );

    // Create credentials.json if it doesn't already exist (first run)
    if ( ! file_exists( dirname( $credentialsPath ) ) ) {
       mkdir( dirname( $credentialsPath ), 0700, true );
    }

    // Save the $accessToken object to the credentials.json file for re-use
    file_put_contents( $credentialsPath, json_encode( $accessToken ) );
 }
 //var_dump( $accessToken ); die();

 // Provide client with API access token
 $client->setAccessToken( $accessToken );

 // If the $accessToken is expired then we'll need to refresh it
 if ( $client->isAccessTokenExpired() ) {
    $client->fetchAccessTokenWithRefreshToken( $client->getRefreshToken() );
    file_put_contents( $credentialsPath, json_encode( $client->getAccessToken() ) );
 }

我正在从公开可用的实际服务器运行此代码,这不是本地计算机。

标签: phpgoogle-sheetsgoogle-apigoogle-sheets-api

解决方案


神奇之处在于重定向 URL。必须以斜杠结尾。我不知道,为什么谷歌这么严格,但这就是我的问题的解决方案。


错误的重定向网址:https://stage.domain.com

正确的重定向网址:https://stage.domain.com/


推荐阅读