我正在實現Google OAuth2服務器一次性代碼流,如下所述:https://developers.google.com/identity/sign-in/web/server-side-flow客戶端從谷歌獲取代碼(在用戶完成oauth流后),并將其POST到服務器。服務器嘗試使用以下調用將代碼交換為刷新令牌(使用 Java SDK):val authorizationScopes = Seq(GmailScopes.GMAIL_READONLY, GmailScopes.GMAIL_SEND, "email").asJavaCollectionval googleAuthorizationCodeFlow = new GoogleAuthorizationCodeFlow.Builder( GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance, googleAppInfo.googleClientId, googleAppInfo.googleClientSecret, authorizationScopes) .setTokenServerUrl(new GenericUrl(googleAppInfo.tokenServerUrl)) .setAccessType("offline") .build()val googleTokenResponse: GoogleTokenResponse = googleAuthorizationCodeFlow .newTokenRequest(code) .setRedirectUri(redirectUri) .execute()我得到:com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request { “error” : “redirect_uri_mismatch”, “error_description” : “Bad Request”}重定向Uri與Google云控制臺中的>憑據>客戶端ID(Web應用程序)>授權重定向URI完全相同。此外,當我們使用OAuth重定向流時,它已經起作用了,但是當我們切換到POST流時,它停止使用此消息。我嘗試發送一個空的redirectUri(正如我在有關此事的一些答案中看到的那樣),但是我得到的:com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request { “error” : “Missing required parameter: redirect_uri”, “error_description” : “Bad Request”}我甚至嘗試發送實際的“引用”網址。我在這里做錯了什么?
2 回答

千萬里不及你
TA貢獻1784條經驗 獲得超9個贊
它沒有記錄在案,但是當您使用Google客戶端SDK時,您不會重定向。
SDK 將打開一個新窗口,并使用發布消息與其通信。
如果您設置重定向uri,因為它應該適合您。"postmessage"

Helenr
TA貢獻1780條經驗 獲得超4個贊
如果您在使用彈出模式時收到該錯誤,則解決方案在前端和后端都具有相同的redirect_uri。
即前部-
const client = google.accounts.oauth2.initCodeClient({
redirect_uri: 'postmessage',
some_other_params,
})
client.requestCode()
返回-
export const oauth2Client = new google.auth.OAuth2(
clientId,
clientSecret,
"postmessage"
)
let { tokens } = await oauth2Client.getToken(code)
添加回答
舉報
0/150
提交
取消