在谷歌中插入联系人,以便使用c#在WhatsApp中使用



我希望你能帮助我:

我有一个数据库,里面有几个电话号码和名字

我需要在C#中创建一个自动运行并插入这些值的进程​​进入链接到WhatsApp的谷歌帐户。

结果是,当打开WhatsApp时,联系人显示的名称为

你能告诉我该找什么或从哪里开始吗,因为我还没有找到任何有用的

提前非常感谢

如果您能够要求用户登录他们的google帐户,则可以使用people.createContactapi端点在他们的google联系人帐户中创建联系人。

虽然在联系人更新这些不同的系统之前会有延迟,但这可能是你能做的最好的事情。HttpClient应该能够进行所需的API调用:(

来源:

  1. https://developers.google.com/people/api/rest/v1/people/createContact
  2. https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=net-6.0

我给您留下答案,其中包含:从谷歌获取联系人并能够插入。如果有人有更新和删除的功能,那就太好了,这就是的一切

// Genéricas
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Util.Store;
// People API
//using Google.Apis.People.v1;
//using Google.Apis.People.v1.Data;
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;
using insert_contacts_google.Mediador;
using System.Net;
using System.Net.Security;
namespace insert_contacts_google
{
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 = "importGoogle";
static PeopleServiceService peopleService;
static UserCredential Credential;
private static int total = 0;
public static List<String> lNumG = new List<String>();
public static Dictionary<string, string> dDatWs = new Dictionary<string, string>();
public static Tools_CPE tools = new Tools_CPE() { Credentials = glbCredenciales, Url = glbWebService };

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

static void Main(string[] args)
{
Console.WriteLine("Main");
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
getContactGoole();
getContactWs();
setPeople(peopleService);
}
private static void getContactGoole()
{
// 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();
}
static bool GetPeople(PeopleServiceService service, string pageToken)
{
bool control = true;
// 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.PhoneNumbers != null ? ($"{person.PhoneNumbers.FirstOrDefault().Value} - ") : "");
Console.Write(person.EmailAddresses != null ? ($"{person.EmailAddresses.FirstOrDefault().Value} - ") : "");
Console.Write(person.Addresses != null ? ($"{person.Addresses.FirstOrDefault()?.City} - ") : "");
Console.Write(person.Names != null ? ($"{person.Names.FirstOrDefault().DisplayName} - ") : "");
*/

if (person.PhoneNumbers != null)
{
String num = person.PhoneNumbers.FirstOrDefault().Value;
lNumG.Add(num);
}
Console.WriteLine();
}
if (people.NextPageToken != null)
{
Console.WriteLine();
Console.WriteLine($"{total} contactos mostrados hasta ahora. Pulsa una tecla para seguir mostrando contactos.");
Console.WriteLine();
GetPeople(service, people.NextPageToken);
}
}
else
{
control = false;
Console.WriteLine("No se han encontrado contactos.");
}
return control;
}
static void setPeople(PeopleServiceService service)
{
Google.Apis.PeopleService.v1.Data.Person person = new Google.Apis.PeopleService.v1.Data.Person();
Name chef = new Name();
chef.GivenName = "FirstName2";
chef.FamilyName = "FamilyName3";
person.Names = new List<Name>();
person.Names.Add(chef);
PhoneNumber phone = new PhoneNumber();
phone.Value = "04040404";
phone.Type = "mobile";
person.PhoneNumbers = new List<PhoneNumber>();
person.PhoneNumbers.Add(phone);
EmailAddress email = new EmailAddress();
email.Type = "work";
email.Value = "firstname2.familyname2@something.com";
person.EmailAddresses = new List<EmailAddress>();
person.EmailAddresses.Add(email);

ContactToCreate contactToCreate = new ContactToCreate();
contactToCreate.ContactPerson = person;
BatchCreateContactsRequest request = new BatchCreateContactsRequest();
request.Contacts = new List<ContactToCreate>();
request.Contacts.Add(contactToCreate);
request.ReadMask = "names";
BatchCreateContactsResponse reply = service.People.BatchCreateContacts(request).Execute();
}
}
}

最新更新