public OAuthUserInfo getUserInfo()

in src/main/java/com/googlesource/gerrit/plugins/oauth/BitbucketOAuthService.java [71:111]


  public OAuthUserInfo getUserInfo(OAuthToken token) throws IOException {
    OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
    OAuth2AccessToken t = new OAuth2AccessToken(token.getToken(), token.getRaw());
    service.signRequest(t, request);

    JsonElement userJson = null;
    try (Response response = service.execute(request)) {

      if (response.getCode() != SC_OK) {
        throw new IOException(
            String.format(
                "Status %s (%s) for request %s",
                response.getCode(), response.getBody(), request.getUrl()));
      }
      userJson = JSON.newGson().fromJson(response.getBody(), JsonElement.class);
      if (log.isDebugEnabled()) {
        log.debug("User info response: {}", response.getBody());
      }
      if (userJson.isJsonObject()) {
        JsonObject jsonObject = userJson.getAsJsonObject();
        JsonObject userObject = jsonObject.getAsJsonObject("user");
        if (userObject == null || userObject.isJsonNull()) {
          throw new IOException("Response doesn't contain 'user' field");
        }
        JsonElement usernameElement = userObject.get("username");
        String username = usernameElement.getAsString();

        JsonElement displayName = jsonObject.get("display_name");
        return new OAuthUserInfo(
            BITBUCKET_PROVIDER_PREFIX + username,
            username,
            null,
            displayName == null || displayName.isJsonNull() ? null : displayName.getAsString(),
            fixLegacyUserId ? username : null);
      }
    } catch (ExecutionException | InterruptedException e) {
      throw new RuntimeException("Cannot retrieve user info resource", e);
    }

    throw new IOException(String.format("Invalid JSON '%s': not a JSON Object", userJson));
  }