[英]Authorising native app user with Google ID token and Laravel Socialite gives 401
我在通過 Google 進行社交名流身份驗證時遇到問題。 我有兩個獨立的應用程序:Laravel 和 React Native。 對於 react native 應用程序,我使用 @react-native-community/google-signin 並在客戶端上獲得令牌后,我將其發送到 Laravel 應用程序,我將這個令牌傳遞給 Socialite function: Socialite::driver('google ')->userFromToken($token); . 我收到此錯誤:
Client error: GET https://www.googleapis.com/oauth2/v3/userinfo?prettyPrint=false resulted in a 401 Unauthorized response:
{
"error": "invalid_request",
"error_description": "Invalid Credentials"
}
我已經重新檢查了 4 次憑據,我確信它們是正確的。 我使用與 react native 應用程序相同的客戶端 ID。 我究竟做錯了什么?
注意:我使用 ID 令牌而不是身份驗證令牌進行授權。
根據我對類似問題的研究 - 看起來谷歌一鍵的工作方式與 oAuth 的工作方式不同。 因此,您必須使用google/apiclient
package 手動從$token
獲取用戶。
谷歌文檔中有一個例子(它在android
,但這些特定的片段是指后端部分,所以它仍然可以解決問題)
composer require google/apiclient
// Get $id_token via HTTPS POST.
$client = new Google_Client(['client_id' => $CLIENT_ID]); // Specify the CLIENT_ID of the app that accesses the backend
$payload = $client->verifyIdToken($id_token);
if ($payload) {
$userid = $payload['sub'];
// If request specified a G Suite domain:
//$domain = $payload['hd'];
} else {
// Invalid ID token
}
您可以在此Google 文檔頁面上找到更多信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.