我目前正在通过OAuth 2.0和所谓的服务帐户实施对Google通讯录的访问.为普通用户生成服务帐户,例如"My.Name@gmail.com".
生成OAuth 2.0凭据的代码是:
public static GoogleCredential getCredentials() throws GeneralSecurityException, IOException {
GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SingleUserCredentials.SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes("https://www.google.com/m8/feeds")
.setServiceAccountPrivateKeyFromP12File(new File(SingleUserCredentials.SERVICE_ACCOUNT_PKCS12_FILE_PATH))
.build();
credential.refreshToken();
return credential;
}
Run Code Online (Sandbox Code Playgroud)
然后我试图通过以下方式检索联系人:
ContactsService myService = new ContactsService(
"myApp");
myService.setOAuth2Credentials(getCredentials());
URL feedUrl = new URL("https://www.google.com/m8/feeds/contacts/default/full");
Query myQuery = new Query(feedUrl);
ContactFeed resultFeed = myService.query(myQuery, ContactFeed.class);
// Print the results
for (ContactEntry entry : resultFeed.getEntries()) {
System.out.println(entry.getName().getFullName().getValue());
System.out.println("Updated on: " + entry.getUpdated().toStringRfc822());
}
Run Code Online (Sandbox Code Playgroud)
问题是我的帐户中没有任何一个联系人.Feed始终为空.没有错误.没有.
通过相同的方法访问Google Apps托管域时,它可以正常运行.
我想知道在使用p12密钥文件和服务帐户时,Contacts Api是否支持普通(也称为@ gmail.com)帐户的OAuth 2.0.
gdata-api google-api google-contacts-api oauth-2.0 service-accounts
我正在尝试通过访问 Google Admin SDK API 来提取我域中的用户。然而,我收到了 401 未经授权的例外。下面的代码是我的设置类,其中包含调用 API 的方法。
package com.brookfieldres.operations;
import java.io.File;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.ResourceBundle;
import org.apache.log4j.Logger;
import com.brookfieldres.common.Constants;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential.Builder;
import com.google.api.services.admin.directory.Directory;
import com.google.api.services.admin.directory.DirectoryScopes;
public class GCAuthentication {
private static final Logger _Log = Logger.getLogger(GCAuthentication.class.getName());
static ResourceBundle resources = ResourceBundle.getBundle("Resources");
// public static String serviceAcc = resources.getString("SERVICE_ACC_EMAIL");
// public static String privKeyPath = resources.getString("PRIVATE_KEY_PATH");
// public static String userEmail = resources.getString("ADMIN_ACC");
public static Directory getDirectoryService(String serviceAcc, String privKeyPath, String userEmail) …Run Code Online (Sandbox Code Playgroud)