Google People Api联系人已创建,但在任何地方都看不到

时间:2018-10-05 17:09:05

标签: c# google-api google-api-client google-api-dotnet-client google-people

我正在使用PeopleService来直接在我的G Suit帐户中创建联系人。我遵循了有关获取一种服务帐户密钥的安全步骤。我的应用程序将创建联系人,因此无需请求特定的用户权限。它具有自己的密钥和凭证。 我的代码似乎可以正常工作,因为CreateContactRequest为我提供了ResourceName值为“ people / c171255166120767303”。而且,每当我请求时,我都会得到一个不同的resourceName,例如“ people / c9013378989213012841”。 问题是,到底发生了什么?在我的G Suite帐户中,我无法在任何地方看到创建的联系人。 但是生成的Person对象似乎还可以。

如何检查是否可行?联系人在哪里创建?

代码如下:

    private static string _clientId = "1........1";
    private static string _clienteScret = "i******************_";
    private static string _serviceAccountId = "aaaa@bbbb.iam.gserviceaccount.com";

公共静态无效Cadastrar(Models.SignupRequest消息)         {

        var chave =
            @"D:\********.p12";

        var certificate = new X509Certificate2(chave, "notasecret", X509KeyStorageFlags.Exportable);

        var credential = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer(_serviceAccountId)
            {
                Scopes = new[] { 
                    PeopleService.Scope.Contacts, 
                    PeopleService.Scope.ContactsReadonly,
                    "https://www.google.com/m8/feeds"
                }
            }.FromCertificate(certificate));


        var service = new PeopleService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "Client for www",
        });

        var contactToCreate = new Person();
        var names = new List<Name> {new Name() {GivenName = message.name, FamilyName = "Doe"}};

        contactToCreate.Names = names;
        contactToCreate.EmailAddresses = new List<EmailAddress>
        {
            new EmailAddress()
            {
                DisplayName = message.name,
                Value = message.email
            }
        };
        contactToCreate.Organizations = new List<Organization>
        {
            new Organization()
            {
                Current = true,
                Name = message.nome_fantasia,
            }
        };
        contactToCreate.Biographies = new List<Biography>
        {
            new Biography()
            {
                Value = message.ToString()
            }
        };

        contactToCreate.PhoneNumbers = new List<PhoneNumber>
        {
            new PhoneNumber()
            {
                Type = "mobile",
                Value = message.celular
            },
            new PhoneNumber()
            {
                Type = "work",
                Value = message.telefone
            }
        };

        var request = new Google.Apis.PeopleService.v1.PeopleResource.CreateContactRequest(service, contactToCreate);

        var createdContact = request.Execute();
    }

1 个答案:

答案 0 :(得分:1)

如果您想通过服务帐户为使用“ email@yourdomain.com”的用户创建联系人,则需要:

  • 使用“ setServiceAccountUser(userEmail)”来模拟用户
  • 在您的G-Suite for People API服务上启用模拟。对于这一点: