Я пишу приложение, которое управляет контактами Google ...
теперь я могу создать контакт
читать контакты ...
но я нашел два разных типа кодов ...
Я получаю список Google.Apis.People.v1.Data.Person, когда получаю список контактов ...
и мне нужно использовать Google.Apis.PeopleService.v1.Data.Person, когда я обновляю / создаю контакт ....
Теперь я хотел бы узнать, как я могу преобразовать один тип данных в тип DataType или функцию, чтобы получить список контактов Google.Apis.PeopleService.v1.Data.Person ...
Я пытался получить запрос на получение от Google.Apis.PeopleService.v1
и обновить с Google.Apis.People.v1
но я не нашел методов для этого ...
private static UserCredential credential { get; set; }
static string[] Scopes = {
"https://www.googleapis.com/auth/contacts",
PeopleService.Scope.Contacts,
PeopleService.Scope.UserAddressesRead,
PeopleService.Scope.UserinfoProfile};
private static PeopleService service { get; set; }
static ClientSecrets secrets = new ClientSecrets()
{
ClientId = "KKKKKKK@gmail.com",
ClientSecret = "AAAAA"
};
private static void initialize()
{
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/gmail-dotnet-quickstart.json
string ApplicationName = "ASDGestSharp";
// UserCredential credential;
using (var stream =
new FileStream("client_id.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 = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/people-dotnet-quickstart");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
//secrets,
Scopes,
Environment.UserName,
//"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Gmail API service.
// Create Drive API service.
service = new PeopleService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
}
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/people-dotnet-quickstart.json
public List<Person> GetPeople(ContactTypeName kind, string DataName = null, string pageToken = null)
{
initialize();
PeopleResource.ConnectionsResource.ListRequest peopleRequest =
service.People.Connections.List("people/me");
peopleRequest.RequestMaskIncludeField = "person.id,person.names,person.emailAddresses,person.PhoneNumbers";
ListConnectionsResponse connectionsResponse = peopleRequest.Execute();
IList<Person> connections = connectionsResponse.Connections;
switch (kind)
{
case ContactTypeName.All:
return (from a in connections select a).ToList();
break;
case ContactTypeName.Address:
var people =
(from a in connections
from c in a.Addresses
where (c.StreetAddress.ToString()+c.ExtendedAddress.ToString()+c.City.ToString()+c.Region.ToString()+c.Country.ToString())
.ToUpper().Contains(DataName.ToUpper())
select a).ToList();
return people;
case ContactTypeName.Name:
people =
(from a in connections
from c in a.Names
where c.DisplayName.ToString().ToUpper().Contains(DataName.ToUpper())
select a).ToList();
return people;
case ContactTypeName.phone:
people =
(from a in connections
from c in a.PhoneNumbers
where c.Value.ToString().ToUpper().Contains(DataName.ToUpper())
select a).ToList();
return people;
case ContactTypeName.email:
people =
(from a in connections
from c in a.Names
where c.DisplayName.ToString().ToUpper().Contains(DataName.ToUpper())
select a).ToList();
return people;
}
return null;
}
public void CreateContact()
{
initialize();
Google.Apis.PeopleService.v1.Data.Person
contactToCreate = new Google.Apis.PeopleService.v1.Data.Person();
List<Google.Apis.PeopleService.v1.Data.Name> names = new List<Google.Apis.PeopleService.v1.Data.Name>();
List<Google.Apis.PeopleService.v1.Data.PhoneNumber> phoneNumbers = new List<Google.Apis.PeopleService.v1.Data.PhoneNumber>();
names.Add(new Google.Apis.PeopleService.v1.Data.Name() {
DisplayName = "ZZZZZZZZZZZ",
FamilyName = "ZZZZZZZZZZZ"
});
phoneNumbers.Add(new Google.Apis.PeopleService.v1.Data.PhoneNumber()
{
Type= "main",
Value="1111100000"
});
contactToCreate.Names = names;
contactToCreate.PhoneNumbers = phoneNumbers;
Google.Apis.PeopleService.v1.PeopleResource.CreateContactRequest request =
new Google.Apis.PeopleService.v1.PeopleResource.CreateContactRequest(service, contactToCreate);
Google.Apis.PeopleService.v1.Data.Person createdContact = request.Execute();
Console.WriteLine(createdContact.Names[0].DisplayName);
}