3

嗨,我正在使用 java 并试图检索域中的所有用户,因为我使用了 Provisionin api............它工作得很好但我的想法是使用 2-legged OAuth 来检索域中的用户是否可能?我不知道如何指定网址请帮帮我我尝试了以下程序

    final String CONSUMER_KEY = "example.com";
    final String CONSUMER_SECRET = "12345678122154154df9";
    final String DOMAIN = "example.com";
    GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
    oauthParameters.setOAuthConsumerKey(CONSUMER_KEY);
    oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET);
    oauthParameters.setOAuthType(OAuthType.TWO_LEGGED_OAUTH);
    OAuthHmacSha1Signer signer = new OAuthHmacSha1Signer();
    URL feedUrl = new URL("https://apps-apis.google.com/a/feeds/" + DOMAIN + 
      "/user/2.0/?xoauth_id=123@example.com");
userService = new UserService("Myapplication");
    userService.setOAuthCredentials(oauthParameters, signer);
    userService.useSsl();
    UserFeed allUsers = new UserFeed();
       UserFeed allpage;
      Link nextLink;


do {
  allpage = userService.getFeed(feedUrl, UserFeed.class);

  allUsers.getEntries().addAll(allpage.getEntries());

  nextLink = allpage.getLink(Link.Rel.NEXT, Link.Type.ATOM);
  if (nextLink != null) {
    feedUrl = new URL(nextLink.getHref());
   }

}while (nextLink != null);
return allUsers;
 }

将错误返回为 com.google.gdata.util.AuthenticationException: Unknown authentication header

4

1 回答 1

1
    // use real values.
    final String CONSUMER_KEY = "example.com";
    final String CONSUMER_SECRET = "secret-here";
    final String DOMAIN = "domain.com";

    GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
    oauthParameters.setOAuthConsumerKey(CONSUMER_KEY);
    oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET);
    oauthParameters.setOAuthType(OAuthType.TWO_LEGGED_OAUTH);
    OAuthHmacSha1Signer signer = new OAuthHmacSha1Signer();

    URL feedUrl = new URL("https://apps-apis.google.com/a/feeds/" + DOMAIN + "/user/2.0");

    UserService service = new UserService("ProvisiongApiClient");
    service.setOAuthCredentials(oauthParameters, signer);
    service.useSsl();
    UserFeed resultFeed = service.getFeed(feedUrl, UserFeed.class);

    for (UserEntry entry : resultFeed.getEntries()) {
      System.out.println(entry.getTitle().getPlainText());
    }

Google Apps API 的 2-Legged OAuth 的特殊之处在于它是无用户的。你不需要xoauth_id=123@xxx.com。管理员可以授权客户端 https://www.google.com/a/cpanel/<your domain>/ManageOauthClients

于 2011-04-26T00:15:37.953 回答