gpt4 book ai didi

java - 使用 java 配置 Api

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:19:23 25 4
gpt4 key购买 nike

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

    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:未知授权 header

最佳答案

    // 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 的双足 OAuth 的特殊之处在于它不需要用户。你不需要 xoauth_id=123@xxx.com .管理员可以从 https://www.google.com/a/cpanel/<your domain>/ManageOauthClients 授权客户端

关于java - 使用 java 配置 Api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2880207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com