Google通讯录-使用服务帐户获取其他用户的通讯录 [英] Google Contacts - Get contacts of another user using service account
问题描述
我正在尝试使用Google服务帐户与其他用户联系.我什至将用户凭据设置为我要为其获取联系人的帐户的用户名和密码.但是,我不断收到错误-线程"main" com.google.gdata.util.ServiceForbiddenException中的异常:无法请求属于另一个用户的联系人
I am trying to get contacts of another user using Google Service Account. I even set the user credentials to the username and password of the account for which I want to fetch the contacts. However, I keep getting an error - Exception in thread "main" com.google.gdata.util.ServiceForbiddenException: Cannot request contacts belonging to another user
最初,我是使用admin帐户来获取另一个用户的联系人,而我一直在该用户中遇到禁止访问"错误.有人告诉我在这种情况下可以使用服务帐户.
Initially I was using admin account to get the contacts of another user for which I kept getting a Forbidden error. I was told that service accounts will work in this case.
有人可以帮忙吗?真的有可能通过服务帐户或任何其他方法来获取另一个帐户的联系人吗?如果可以,为什么我必须收到此错误?
Can anybody please help? Is it really possible to get the contacts of another account with service accounts or any other method? If yes, any idea why I must be getting this error?
推荐答案
If you want to access a user's contacts using a service account, you need to do three things (see https://developers.google.com/accounts/docs/OAuth2ServiceAccount):
- 在 Google Developers Console 中创建服务帐户.
- 使用域的管理控制台将域范围的权限委派给服务帐户.
- (访问联系人所必需:) 通过
GoogleCredential
工厂的setServiceAccountUser
方法指定用户帐户的电子邮件地址来模拟用户.
- Create a service account in the Google Developers Console.
- Delegate domain-wide authority to the service account using the domain’s Admin console.
- (Necessary to access contacts:) Impersonate the user by specifying the email address of the user account with the
setServiceAccountUser
method of theGoogleCredential
factory.
例如:
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(emailAddress)
.setServiceAccountPrivateKeyFromP12File(new File("MyProject.p12"))
.setServiceAccountScopes(Collections.singleton(SQLAdminScopes.SQLSERVICE_ADMIN))
.setServiceAccountUser("user@example.com")
.build();
错误消息Cannot request contacts belonging to another user
表示您忘记了最后一步.
The error message Cannot request contacts belonging to another user
suggests that you forgot the last step.
这篇关于Google通讯录-使用服务帐户获取其他用户的通讯录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!