简体   繁体   English

使用服务帐户凭据通过 Google People API 访问 Gmail 联系人始终返回 null

[英]Accessing Gmail contacts via the Google People API using service account credentials always returning null

I cannot access Gmail contacts with the following code.我无法使用以下代码访问 Gmail 联系人。 It always returns null, and all API permissions are granted with contacts inside the account.它始终返回 null,并且所有 API 权限都授予帐户内的联系人。

string jsonText = @"{""type"": ""service_account"",
    ""project_id"": """",
    ""private_key_id"": """",
    ""private_key"": """",
    ""client_email"": """",
    ""client_id"": """",
    ""auth_uri"": """",
    ""token_uri"": """",
    ""auth_provider_x509_cert_url"": """",
    ""client_x509_cert_url"": """"
}";

var credentialParameters = NewtonsoftJsonSerializer.Instance.Deserialize<JsonCredentialParameters>(jsonText);

// Credentials
var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(credentialParameters.ClientEmail)
{
    User = credentialParameters.ClientEmail,
    Scopes = new[] { "https://www.googleapis.com/auth/contacts.readonly",
                     "https://www.googleapis.com/auth/contacts",
                     "https://www.googleapis.com/auth/contacts.other.readonly " }
                   }.FromPrivateKey(credentialParameters.PrivateKey));

// accessToken
var accessToken = await credential.GetAccessTokenForRequestAsync();

// Create the service.
var service = new PeopleServiceService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
});

GoogleCredential googleCredentials = GoogleCredential.FromJson(jsonText);

var ser = new PeopleServiceService(new BaseClientService.Initializer()
{
    HttpClientInitializer = googleCredentials,
});

// Get list of contacts
ConnectionsResource.ListRequest peopleRequest = ser.People.Connections.List("people/me");

peopleRequest.PersonFields = "names,emailAddresses";
ListConnectionsResponse response = peopleRequest.Execute();
IList<Person> people = response.Connections;

Side note:边注:

Cannot access Gmail contacts无法访问 Gmail 联系人

It is unclear if you are using a standard Gmail account or working with Google Workspace.目前尚不清楚您是使用标准 Gmail 帐户还是使用 Google Workspace。 You should be aware that Service accounts are not going to work with a standard Gmail user's contacts.您应该知道服务帐户不会与标准 Gmail 用户的联系人一起使用。 There is no way to delegate the permission without workspace.没有工作空间就无法委派权限。


Remember a service account is not you.请记住,服务帐户不是您。 A service account is a dummy user.服务帐户是一个虚拟用户。 Its returning null or empty because your service account does not currently have any contacts.它返回 null 或空,因为您的服务帐户当前没有任何联系人。 You would need to add some.你需要添加一些。

However if you are trying to access contacts for a user then.但是,如果您尝试访问用户的联系人,那么。

In order for a service account to access a users data you need to have delegated to that user.为了让服务帐户访问用户数据,您需要委派给该用户。 Trying to access Google contacts you will need to go though your workspace account.尝试访问 Google 联系人时,您需要使用您的工作区帐户。 The workspace admin can set up domain wide delegation to the user on your domain whose contacts you would like to access.工作区管理员可以为您想要访问其联系人的域上的用户设置域范围的委派

Make sure that you have included the Domain scope as shown in the documentation.确保您已包含文档中显示的域范围

Then remember to add the user when you initialize the service account this must be a user on your domain for which you have set up delegation for.然后记得在初始化服务帐户时添加用户,这必须是您已为其设置委派的域中的用户。

var serviceAccountCredentialInitializer = new  ServiceAccountCredential.Initializer(serviceAccount)
{
    User = gsuiteUser,
    Scopes = new[] { GmailService.Scope.GmailSend, GmailService.Scope.GmailLabels }

}.FromCertificate(certificate);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM