【问题标题】:Redirect_uri when trying to exchange an authcode from backend server when the authcode was received in an ios app当在 ios 应用程序中收到身份验证码时尝试从后端服务器交换身份验证码时的 Redirect_uri
【发布时间】:2016-06-23 09:49:59
【问题描述】:

我有一个 ios 应用程序和一个 Web 应用程序,它们从用户那里获得授权并生成一个 authcode 并将其发送到后端 java servlet,后者尝试将 authcode 交换为访问和刷新令牌。从 web 应用程序交换身份验证码是有效的,但对于从 ios 应用程序生成的身份验证码,我在交换过程中收到以下错误。

com.google.api.client.auth.oauth2.TokenResponseException: 400 错误请求 { “错误”:“无效请求”, “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。

【问题讨论】:

    标签: java ios oauth-2.0 google-oauth google-oauth-java-client


    【解决方案1】:

    urn:ietf:wg:oauth:2.0:oob

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

    【讨论】:

      猜你喜欢
      • 2018-10-09
      • 2012-04-02
      • 1970-01-01
      • 1970-01-01
      • 2020-11-24
      • 2011-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多