gpt4 book ai didi

java - 在 ios 应用程序中收到授权码时尝试从后端服务器交换授权码时的 Redirect_uri

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:33:49 27 4
gpt4 key购买 nike

我有一个 ios 应用程序和一个 web 应用程序,它们从用户那里获得授权并生成一个授权码并将其发送到后端 java servlet,后者试图交换授权码以获取访问和刷新 token 。从 web 应用程序交换授权码是有效的,但对于从 ios 应用程序生成的授权码,我在交换期间遇到以下错误。

com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request { "error" : "invalid_request", "error_description" : "Missing parameter: redirect_uri" }

这是进行交换的代码

      public OAuthCodeExchangeResponse exchangeAuthCode(String authCode, boolean isIosApp) throws JSONException, IOException {
OAuthCodeExchangeResponse response = new OAuthCodeExchangeResponse();
GoogleClientSecrets clientSecrets = getClientSecrets(isIosApp);
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setAccessType("offline")
.build();
GoogleTokenResponse tokenResponse = null;
if(isIosApp == false) {
tokenResponse = flow.newTokenRequest(authCode)
.setRedirectUri("postmessage")
.execute();
} else {
tokenResponse = flow.newTokenRequest(authCode).execute();
}
GoogleIdToken idToken = tokenResponse.parseIdToken();
GoogleIdToken.Payload payload = idToken.getPayload();
response.setAccessToken(tokenResponse.getAccessToken());
response.setEmail(payload.getEmail());
response.setIdToken(tokenResponse.getIdToken());
response.setRefreshToken(tokenResponse.getRefreshToken());
return response;
}

public GoogleClientSecrets getClientSecrets(boolean isIosApp) throws JSONException, IOException {
GoogleClientSecrets.Details d = new GoogleClientSecrets.Details();
if(isIosApp == false) {
d.setClientId(WebClientId);
d.setClientSecret(WebClientSecret);
} else {
d.setClientId(PhoneClientId);
}
GoogleClientSecrets clientSecrets = new GoogleClientSecrets();
clientSecrets.setInstalled(d);
return clientSecrets;
}

当交换从 ios 应用程序生成的授权码时,我必须设置什么 redirect_uri?在谷歌开发者控制台中为 ios 应用程序创建的凭证没有设置重定向 uri。

最佳答案

urn:ietf:wg:oauth:2.0:oob

您需要安装应用程序的流程,请参阅 https://developers.google.com/identity/protocols/OAuth2InstalledApp#formingtheurl

关于java - 在 ios 应用程序中收到授权码时尝试从后端服务器交换授权码时的 Redirect_uri,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35884508/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com