Пример #1
0
  /**
   * Send a request to the UserInfo API to retrieve user e-mail address associated with the given
   * credential.
   *
   * @param credential Credential to authorize the request.
   * @return User's e-mail address.
   * @throws NoUserIdException An error occurred, and the retrieved email address was null.
   */
  private Userinfo getUserInfo(Credential credential) throws NoUserIdException {
    Userinfo userInfo = null;

    // Create a user info service, and make a request to get the user's info.
    Oauth2 userInfoService = new Oauth2.Builder(TRANSPORT, JSON_FACTORY, credential).build();
    try {
      userInfo = userInfoService.userinfo().get().execute();
      if (userInfo == null) {
        throw new NoUserIdException();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    return userInfo;
  }
  protected Userinfo getUserInfo(Credential credentials) throws NoSuchUserIdException {

    Oauth2.Builder builder =
        new Oauth2.Builder(new NetHttpTransport(), new JacksonFactory(), credentials);

    Oauth2 oauth2 = builder.build();

    Userinfo userInfo = null;

    try {
      userInfo = oauth2.userinfo().get().execute();
    } catch (IOException e) {
      System.err.println("An error occurred: " + e);
    }

    if ((userInfo != null) && (userInfo.getId() != null)) {
      return userInfo;
    } else {
      throw new NoSuchUserIdException();
    }
  }