如何解决Google表格PHP API redirect_uri_mismatch错误
我正在尝试通过身份验证以使用Google Sheets API。我开始对此失去理智,因为我已经在这个问题上停留了两天。
在我的Google控制台中,我已将凭据-> OAuth 2.0客户端ID->授权重定向URI设置为https://stage.domain.com/
和https://stage.domain.com
。
当我访问https://stage.domain.com
时,我被重定向到account.google.com以授权我的应用程序。我选择我的帐户,然后单击“允许”按钮。我被重定向到https://stage.domain.com/?code=4/...&scope=https://www.googleapis.com/auth/spreadsheets.readonly
,并从{"error":"redirect_uri_mismatch","error_description":"Bad Request"}
函数中获得了$client->fetchAccesstokenWithAuthCode
。
我在做什么错?我正在运行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() ) );
}
我正在从公开可用的实际服务器(不是本地计算机)上运行此代码。
解决方法
神奇之处在于重定向URL。 必须以斜杠结尾。我不知道,为什么Google如此严格,但这就是解决我的问题的方法。
错误的重定向网址:https://stage.domain.com
。
正确的重定向网址:https://stage.domain.com/
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。