Home > Net >  create contact in google using c#
create contact in google using c#

Time:02-10

I need to create several contacts in google through c #, so far I have this code that already shows me the contacts I have:

// Genéricas
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Util.Store;

// People API
using Google.Apis.PeopleService.v1;
using Google.Apis.PeopleService.v1.Data;


// Docs API
using Google.Apis.Docs.v1;
using Google.Apis.Docs.v1.Data;

// Drive API
using Google.Apis.Drive.v3;
using Google.Apis.Drive.v3.Data; // Para el tipo File

using System;
using System.Collections.Generic;

//using System.IO;
using System.Threading;
using System.Text;
using System.Linq;
using System.Collections.Immutable;
using Person = Google.Apis.PeopleService.v1.OtherContactsResource;
using Google.Apis.Requests;

namespace insert_google_contacts
{
class Program
{
    // If modifying these scopes, delete your previously saved credentials
    // at ~/.credentials/docs.googleapis.com-dotnet-quickstart.json
    static string[] Scopes = { DocsService.Scope.Documents,
                               DocsService.Scope.DriveFile,
                               PeopleServiceService.Scope.Contacts };

    static string ApplicationName = "Tutorial Google APIs VB";

    // Los datos del proyecto creado para VB
    static ClientSecrets secrets = new ClientSecrets()
    {
        ClientId = ".....",
        ClientSecret = "......."
    };

    static DocsService docService;
    static DriveService driveService;
    static PeopleServiceService peopleService;

    static UserCredential Credential;

    static void Main(string[] args)
    {
        Console.WriteLine("Tutorial Google APIs con C#");

        string credPath = System.Environment.GetFolderPath(
            Environment.SpecialFolder.Personal);

        // Directorio donde se guardarán las credenciales
        credPath = System.IO.Path.Combine(credPath, ".credentials/Tutorial-Google-APIs-VB");

        Credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            secrets,
            Scopes,
            "user",
            CancellationToken.None,
            new FileDataStore(credPath, true)).Result;
        //Console.WriteLine("Credential file saved to: "   credPath);

        // Mostrar los contactos
        MostrarContactos();

        Console.WriteLine();
        Console.WriteLine("Pulsa una tecla.");
        Console.Read();
    }

    private static void MostrarContactos()
    {
        // Create Drive API service.
        peopleService = new PeopleServiceService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = Credential,
            ApplicationName = ApplicationName,
        });

        // Lista de los contactos (People)
        Console.WriteLine("Contactos:");

        // Este muestra todos los contactos
        GetPeople(peopleService, null);

        Console.WriteLine();
        Console.WriteLine($"Hay {total} contactos / People");
        Console.WriteLine();
    }

    private static int total = 0;

    static void GetPeople(PeopleServiceService service, string pageToken)
    {
        // Define parameters of request.
        PeopleResource.ConnectionsResource.ListRequest peopleRequest =
                service.People.Connections.List("people/me");

        //
        // Lista de campos a usar en RequestMaskIncludeField:
        // https://developers.google.com/people/api/rest/v1/people/get
        //

        peopleRequest.RequestMaskIncludeField = new List<string>()
                {"person.names", "person.phoneNumbers", "person.emailAddresses",
                  "person.birthdays", "person.Addresses"
                };


        if (pageToken != null)
        {
            peopleRequest.PageToken = pageToken;
        }

        ListConnectionsResponse people = peopleRequest.Execute();

        if (people != null && people.Connections != null && people.Connections.Count > 0)
        {
            total  = people.Connections.Count;
            foreach (var person in people.Connections)
            {
                Console.Write(person.Names != null ? ($"{person.Names.FirstOrDefault().DisplayName} - ") : "");
                Console.Write(person.PhoneNumbers != null ? ($"{person.PhoneNumbers.FirstOrDefault().Value} - ") : "");
                Console.Write(person.EmailAddresses != null ? ($"{person.EmailAddresses.FirstOrDefault().Value} - ") : "");
                Console.Write(person.Addresses != null ? ($"{person.Addresses.FirstOrDefault()?.City} - ") : "");
                if (person.Birthdays != null)
                {
                    var fecha = "";
                    var b = person.Birthdays.FirstOrDefault()?.Date;
                    if (b != null)
                        fecha = $"{b.Day}/{b.Month}/{b.Year}";
                    Console.Write($"{fecha}");
                }
                Console.WriteLine();
            }

            if (people.NextPageToken != null)
            {
                Console.WriteLine();
                Console.WriteLine($"{total} contactos mostrados hasta ahora. Pulsa una tecla para seguir mostrando contactos.");
                Console.WriteLine();
                Console.ReadKey();

                GetPeople(service, people.NextPageToken);
            }
        }
        else
        {
            Console.WriteLine("No se han encontrado contactos.");
            return;
        }
    }

    static void setPeople(PeopleServiceService service)
    {
        PeopleResource.ListDirectoryPeopleRequest peopleRequest =
             peopleService.People.ListDirectoryPeople();






    }
}
}

These are the installed nuGet

enter image description here

I have tried to follow these examples and have not been able to:https://developers.google.com/people/v1/contacts?hl=es_419#java_3

I have seen and tried several Examples: where it uses the People or Person class but from what I investigated these classes are already obsolete.

Thanks for the help

CodePudding user response:

  •  Tags:  
  • Related