php - 谷歌表格 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.0
Composer。
$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。必须以斜杠结尾。我不知道,为什么谷歌这么严格,但这就是我的问题的解决方案。
错误的重定向网址:https://stage.domain.com
。
正确的重定向网址:https://stage.domain.com/
。
推荐阅读
- python - Python 3 中是否有任何“主题”等价物?
- flutter - 防止通过 Flutter 中的 video_player 使用的 ExoPlayer 向日志发送垃圾邮件
- google-sheets - 在 Google 表格中将 countif 函数向下扩展一列
- c++ - 如何显示向量的最大元素
? - python - 我们可以在 pyspark 或 python 中用任何给定的数字启动 dense_rank() 吗?
- python - 如何使用openpyxl保护excel中的列免受更改?
- flutter - 在 MaterialApp 内部。使用不包含 MediaQuery 的上下文调用 MediaQuery.of()
- python - 我看不到结果。结果上有一种地址
- python - 我收到一条错误消息“KeyError”,Python,Telegram api
- android - Android 应用程序尝试打开 pdf 文件但立即被发回