我正在尝试获取此处描述的“Google 域共享联系人 API”: https ://developers.google.com/admin-sdk/domain-shared-contacts/
使用此处描述的“用于服务器到服务器应用程序的 OAuth 2.0”工作: https ://developers.google.com/accounts/docs/OAuth2ServiceAccount
OAuth 页面的建议是使用提供的 Google 客户端库...我正在使用 Java 库。但是 Shared-Contacts API 没有使用此库的示例,我无法弄清楚如何将库与 Shared-Contacts API 一起使用。
我可以让 OAuth 为我工作的例子......它使用谷歌云存储。这是代码片段:
String STORAGE_SCOPE = "https://www.googleapis.com/auth/devstorage.read_write";
try {
try {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
String p12Content = Files.readFirstLine(new File(keyFile), Charset.defaultCharset());
// Build service account credential.
GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(Collections.singleton(STORAGE_SCOPE))
.setServiceAccountPrivateKeyFromP12File(new File(keyFile))
.build();
// Set up and execute Google Cloud Storage request.
String URI;
URI = "https://storage.googleapis.com/" + BUCKET_NAME;
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
GenericUrl url = new GenericUrl(URI);
HttpRequest request = requestFactory.buildGetRequest(url);
HttpResponse response = request.execute();
content = response.parseAsString();
} catch (IOException e) {
System.err.println(e.getMessage());
}
} catch (Throwable t) {
t.printStackTrace();
}
return content;
这是获取 GCS 上某个存储桶中内容列表的请求。它使用 Credentials 对象调用特定的 URL,其中 Credentials 对象使用我下载的密钥文件执行 OAuth 的工作。让它工作还涉及其他步骤(设置服务帐户电子邮件等),我这样做了。它返回一个包含存储桶内内容的 xml 字符串,它对我来说很好用。
然后我尝试将 URI 更改为此字符串: URI = " https://www.google.com/m8/feeds/contacts/myGoogleAppsDomain.com/full "; 我将 STORAGE_SCOPE 变量更改为以下字符串: STORAGE_SCOPE = " http://www.google.com/m8/feeds/ ";
希望它会返回共享联系人的 xml 字符串。但相反,它返回此错误:403 无法请求属于另一个域的联系人
我相信我收到了这个错误,因为我在执行身份验证请求时没有指定“hd”参数......但是,我不确定如何使用 GoogleCredential 对象(或其他参数,“范围”除外)...有人可以帮我吗?