Google Sheets PHP API重定向_uri_mismatch错误



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

在我的谷歌控制台中,我已经设置了凭据->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() ) );
}

我在一个公开的实际服务器上运行这段代码,这不是一台本地机器。

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


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

更正重定向url:https://stage.domain.com/

最新更新