我有一个ios应用程序和一个Web应用程序,该应用程序从用户那里获得授权并生成一个authcode,并将其发送到后端java servlet,后者试图将authcode交换为访问和刷新 token 。从网络应用程序交换身份验证代码是可行的,但是对于从ios应用程序生成的身份验证代码,我在交换过程中遇到以下错误。
com.google.api.client.auth.oauth2.TokenResponseException:400错误的请求
{
“error”:“invalid_request”,
“error_description”:“缺少参数: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?在google开发人员控制台中为ios应用创建的凭据没有设置重定向uri btw。
最佳答案
ur:ietf:wg:oauth:2.0:oob
您需要安装的应用程序的流程,请参阅https://developers.google.com/identity/protocols/OAuth2InstalledApp#formingtheurl