使用Google登录登录Web服务器

时间:2018-06-10 21:42:35

标签: download httpclient google-signin

我从HTTP客户端(Java或Dart - Android应用程序)连接到第三方Web服务器,以下载属于该服务器上当前用户的一些资源(XML或IMG文件)。此网站需要使用Google Sing-In登录。我已经在我的Android应用中设置了所有用Google登录用户的内容,我获得了他们的授权idToken。但是如何在HTTP GET或POST方法中实际使用它来下载受保护的资源?

使用BASIC身份验证很简单 - 只需设置HTTP'授权'标题正确("基本" +用户:密码编码为base64),调用GET,然后下载所需的资源。但我无法找到有关如何使用Google Sing-In执行此操作的任何信息。我是否在某些标题中发送了我从Google收到的idToken?还需要什么其他魔力?

1 个答案:

答案 0 :(得分:3)

添加Java代码段,希望对您有所帮助:

// (Receive authCode via HTTPS POST)


if (request.getHeader('X-Requested-With') == null) {
  // Without the `X-Requested-With` header, this request could be forged. Aborts.
}

// Set path to the Web application client_secret_*.json file you downloaded from the
// Google API Console: https://console.developers.google.com/apis/credentials
// You can also find your Web application client ID and client secret from the
// console and specify them directly when you create the GoogleAuthorizationCodeTokenRequest
// object.
String CLIENT_SECRET_FILE = "/path/to/client_secret.json";

// Exchange auth code for access token
GoogleClientSecrets clientSecrets =
    GoogleClientSecrets.load(
        JacksonFactory.getDefaultInstance(), new FileReader(CLIENT_SECRET_FILE));
GoogleTokenResponse tokenResponse =
          new GoogleAuthorizationCodeTokenRequest(
              new NetHttpTransport(),
              JacksonFactory.getDefaultInstance(),
              "https://www.googleapis.com/oauth2/v4/token",
              clientSecrets.getDetails().getClientId(),
              clientSecrets.getDetails().getClientSecret(),
              authCode,
              REDIRECT_URI)  // Specify the same redirect URI that you use with your web
                             // app. If you don't have a web version of your app, you can
                             // specify an empty string.
              .execute();

String accessToken = tokenResponse.getAccessToken();

// Use access token to call API
GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken);
Drive drive =
    new Drive.Builder(new NetHttpTransport(), JacksonFactory.getDefaultInstance(), credential)
        .setApplicationName("Auth Code Exchange Demo")
        .build();
File file = drive.files().get("appfolder").execute();

// Get profile info from ID token
GoogleIdToken idToken = tokenResponse.parseIdToken();
GoogleIdToken.Payload payload = idToken.getPayload();
String userId = payload.getSubject();  // Use this value as a key to identify a user.
String email = payload.getEmail();
boolean emailVerified = Boolean.valueOf(payload.getEmailVerified());
String name = (String) payload.get("name");
String pictureUrl = (String) payload.get("picture");
String locale = (String) payload.get("locale");
String familyName = (String) payload.get("family_name");
String givenName = (String) payload.get("given_name");

有关详细信息,请在以下位置找到所有必需的步骤和参考:https://developers.google.com/identity/sign-in/web/server-side-flow#step_1_create_a_client_id_and_client_secret