Google API 使用 PeopleService 添加联系人

Google APIs Adding a contact using PeopleService

我有一个关于如何使用 Google API 将联系人正确添加到 Google 联系人的问题。 对于授权,我使用外部 Json 文件生成。 当我执行它时,它没有给出任何错误,但没有将联系人添加到 Google 联系人。 代码有什么问题? 请在下面找到代码

谢谢

        private async Task Run()
    {

        GoogleCredential credential;

        using (Stream stream = new FileStream(@"D:\project1.json", FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            credential = GoogleCredential.FromStream(stream);
        }

        string[] scopes = new string[] {
        PeopleServiceService.Scope.Contacts,
        PeopleServiceService.Scope.ContactsReadonly,
        PeopleServiceService.Scope.ContactsOtherReadonly,
        };

        credential = credential.CreateScoped(scopes);

        BaseClientService.Initializer initializer = new BaseClientService.Initializer()
        {
            HttpClientInitializer = (IConfigurableHttpClientInitializer)credential,
            ApplicationName = "Project1",
            GZipEnabled = true,
        };

        PeopleServiceService service = new PeopleServiceService(initializer);

        Person contactToCreate = new Person();

        List<Name> names = new List<Name>();
        names.Add(new Name() { GivenName = "Alex", FamilyName = "Breen", DisplayName = "Alex Breen" });
        contactToCreate.Names = names;

        List<PhoneNumber> phoneNumbers = new List<PhoneNumber>();
        phoneNumbers.Add(new PhoneNumber() { Value = "11-22-33" });
        contactToCreate.PhoneNumbers = phoneNumbers;

        List<EmailAddress> emailAddresses = new List<EmailAddress>();
        emailAddresses.Add(new EmailAddress() { Value = "AlexBreen@mail.com" });
        contactToCreate.EmailAddresses = emailAddresses;

        PeopleResource.CreateContactRequest request = new PeopleResource.CreateContactRequest(service, contactToCreate);

        Person createdContact = request.Execute();

        Console.WriteLine(request);

    }

Results

Metrics

您需要遍历您的服务对象。

    var request = service.People.CreateContact(new Person()
    {
    Names = new List<Name>() { new Name() { DisplayName = "test"}}

    // Fill in the rest of the person object here.

    });

    var response = request.Execute

确保您检查的 google 联系人来自您验证应用程序的同一用户。

响应应该返回新用户。

用户的所有联系人

你也可以边做边测试。这将为您提供为您授权的用户插入的用户列表。

var results = service.People.Connections.List("people/me").Execute();

当前用户是谁

var results = service.People.Get("people/me").Execute();

var 结果 = service.People.Connections.List("people/me").Execute();

服务帐户

服务帐户不是您。将服务帐户更多地视为虚拟用户,它拥有自己的 Google 联系人帐户。当您插入其中时,您将插入到服务帐户拥有的帐户中。

如果您有 google 工作区,您可以设置服务帐户的域范围委托,然后委托给域中的用户,并将联系人添加到他们在域中的 google 联系人。

您不能使用服务帐户写入标准 google gmail 用户的 google 联系人。为此,您需要使用 Oauth2 并授权用户访问他们的 google 联系人。

根据上一个回答的评论:

  • 您正在向服务帐户插入联系人

要使用您自己的帐户,您必须创建一个 OAuth client ID,然后使用 credentials.json 对代码进行授权。

People API samples but you can check on how to use this credentials based on the .NET quickstart 上没有适用于 Drive API 但未使用 Drive API 范围和代码的 C# 示例。

基本上使用这部分代码:

UserCredential credential;

using(var stream =
  new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
  // The file token.json stores the user's access and refresh tokens, and is created
  // automatically when the authorization flow completes for the first time.
  string credPath = "token.json";
  credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
    GoogleClientSecrets.Load(stream).Secrets,
    Scopes,
    "user",
    CancellationToken.None,
    new FileDataStore(credPath, true)).Result;
  Console.WriteLine("Credential file saved to: " + credPath);
}