我正在实现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
提交
取消