【问题标题】:Google Sheets PHP API redirect_uri_mismatch error谷歌表格 PHP API redirect_uri_mismatch 错误
【发布时间】:2020-08-24 15:05:59
【问题描述】:

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

在我的 Google 控制台中,我已将凭据 -> OAuth 2.0 客户端 ID -> 授权重定向 URI 设置为 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 并从$client->fetchAccessTokenWithAuthCode 函数中获取{"error":"redirect_uri_mismatch","error_description":"Bad Request"}

我做错了什么?我正在运行 PHP,我正在使用 Composer 的 google/apiclient^2.0

 $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() ) );
 }

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

【问题讨论】:

    标签: php google-sheets google-api google-sheets-api


    【解决方案1】:

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


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

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

    【讨论】:

      猜你喜欢
      • 2014-06-20
      • 2013-07-24
      • 2016-11-04
      • 2016-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-29
      • 1970-01-01
      相关资源
      最近更新 更多