Google OAuth2 PHP 客户端 redirect_uri_mismatch 当每个可能的 URI 都在控制台中时
Google OAuth2 PHP client redirect_uri_mismatch when every possible URI is in console
我已经将调用来自 PHP 库或库重定向到的任何页面的任何 OAUTH 2.0 代码的所有可能的 URI 放入开发控制台凭据中。有正斜杠,没有。
出现此错误的页面的确切 URI 在控制台中。它不是在谈论我当前所在的 URI,它是原始 URI 吗?这仍然没有意义,因为它也在控制台中。
我得到这个错误的 URI 和控制台中的唯一区别是它在末尾有一个 # url 片段。但是当我尝试在控制台中添加它时,出现错误——我假设是因为不允许使用片段。
我不知道这个片段是从哪里来的,但我在获得授权码后通过更改 PHP header 位置进入页面,如下所示:
return header('Location: https://website.com/m/?mail=tokened');
错误所在页面的 URI 以及上述代码行转到的 URI 是:https://website.com/m/?mail=tokened#
控制台中的 URI 是 https://website.com/m/?mail=tokened
和 https://website.com/m?mail=tokened
.
更新:
这是我得到的确切错误:
array(2) { ["error"]=> string(21) "redirect_uri_mismatch" ["error_description"]=> string(11) "Bad Request" }
在此页面:https://keyreader.xyz/m?mail=tokened
这是开发控制台凭据的屏幕截图:
这是我使用的代码:
<?php
session_start();
if(isset($_GET['mail']) && $_GET['mail']==='approved'){
setcookie('authCode', $_GET['code'], time()+(60*60), '/');
return header('Location: https://keyreader.xyz/m/?mail=tokened');
}
if(isset($_GET['mail']) && $_GET['mail']==='tokened'){
require_once 'php/vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfig('php/client_secret.json');
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->addScope(array('email', 'profile'));
var_dump($client->fetchAccessTokenWithAuthCode(urldecode($_COOKIE['authCode'])));
} ?>
所以我正在获取授权代码并将其存储在 cookie 中,因为我认为 url 中的代码参数具有不同代码的值这一事实导致了重定向错误,因为url 每次都不一样,我不能把它放在控制台里。然后我重定向到 url 中没有代码参数的同一页面,并获取访问令牌。
不知道为什么,但是从开发控制台删除所有 URL 然后 re-inputting 它们然后下载一个新的 client_secrets 文件似乎有效!
更改重定向 url 后,您必须更新 client_id.json 文件,因为它包含重定向 urls。
{"web":
{
"client_id":"xxxx.apps.googleusercontent.com",
"project_id":"some-xxx",
"auth_uri":"https://accounts.google.com/o/oauth2/auth",
"token_uri":"https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
"client_secret":"xxxxxxxxxx",
"redirect_uris":[
"http://website.com/getGoogleUrl",
"http://website.dev/getGoogleUrl"
]
}}
并且在 redirect_uris 中列出您当前的重定向 url 应该是第一个。
我已经将调用来自 PHP 库或库重定向到的任何页面的任何 OAUTH 2.0 代码的所有可能的 URI 放入开发控制台凭据中。有正斜杠,没有。
出现此错误的页面的确切 URI 在控制台中。它不是在谈论我当前所在的 URI,它是原始 URI 吗?这仍然没有意义,因为它也在控制台中。
我得到这个错误的 URI 和控制台中的唯一区别是它在末尾有一个 # url 片段。但是当我尝试在控制台中添加它时,出现错误——我假设是因为不允许使用片段。
我不知道这个片段是从哪里来的,但我在获得授权码后通过更改 PHP header 位置进入页面,如下所示:
return header('Location: https://website.com/m/?mail=tokened');
错误所在页面的 URI 以及上述代码行转到的 URI 是:https://website.com/m/?mail=tokened#
控制台中的 URI 是 https://website.com/m/?mail=tokened
和 https://website.com/m?mail=tokened
.
更新:
这是我得到的确切错误:
array(2) { ["error"]=> string(21) "redirect_uri_mismatch" ["error_description"]=> string(11) "Bad Request" }
在此页面:https://keyreader.xyz/m?mail=tokened
这是开发控制台凭据的屏幕截图:
这是我使用的代码:
<?php
session_start();
if(isset($_GET['mail']) && $_GET['mail']==='approved'){
setcookie('authCode', $_GET['code'], time()+(60*60), '/');
return header('Location: https://keyreader.xyz/m/?mail=tokened');
}
if(isset($_GET['mail']) && $_GET['mail']==='tokened'){
require_once 'php/vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfig('php/client_secret.json');
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->addScope(array('email', 'profile'));
var_dump($client->fetchAccessTokenWithAuthCode(urldecode($_COOKIE['authCode'])));
} ?>
所以我正在获取授权代码并将其存储在 cookie 中,因为我认为 url 中的代码参数具有不同代码的值这一事实导致了重定向错误,因为url 每次都不一样,我不能把它放在控制台里。然后我重定向到 url 中没有代码参数的同一页面,并获取访问令牌。
不知道为什么,但是从开发控制台删除所有 URL 然后 re-inputting 它们然后下载一个新的 client_secrets 文件似乎有效!
更改重定向 url 后,您必须更新 client_id.json 文件,因为它包含重定向 urls。
{"web":
{
"client_id":"xxxx.apps.googleusercontent.com",
"project_id":"some-xxx",
"auth_uri":"https://accounts.google.com/o/oauth2/auth",
"token_uri":"https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
"client_secret":"xxxxxxxxxx",
"redirect_uris":[
"http://website.com/getGoogleUrl",
"http://website.dev/getGoogleUrl"
]
}}
并且在 redirect_uris 中列出您当前的重定向 url 应该是第一个。