Google通讯录-使用服务帐户获取其他用户的通讯录 [英] Google Contacts - Get contacts of another user using service account

查看:157
本文介绍了Google通讯录-使用服务帐户获取其他用户的通讯录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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):

  1. Google Developers Console 中创建服务帐户.
  2. 使用域的管理控制台将域范围的权限委派给服务帐户.
  3. (访问联系人所必需:) 通过GoogleCredential工厂的setServiceAccountUser方法指定用户帐户的电子邮件地址来模拟用户.
  1. Create a service account in the Google Developers Console.
  2. Delegate domain-wide authority to the service account using the domain’s Admin console.
  3. (Necessary to access contacts:) Impersonate the user by specifying the email address of the user account with the setServiceAccountUser method of the GoogleCredential 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆