向Google API添加联系人时出错

时间:2017-12-04 11:26:56

标签: c# asp.net-mvc

public static Contact CreateContact(ContactsRequest cr)
{
    Contact newEntry = new Contact();
    // Set the contact's name.
    newEntry.Name = new Name()
    {
        FullName = "Elizabeth Bennet",
        GivenName = "Elizabeth",
        FamilyName = "Bennet",
    };
    newEntry.Content = "Notes";
    // Set the contact's e-mail addresses.
    newEntry.Emails.Add(new EMail()
    {
        Primary = true,
        Rel = ContactsRelationships.IsHome,
        Address = "liz@gmail.com"
    });
    newEntry.Emails.Add(new EMail()
    {
        Rel = ContactsRelationships.IsWork,
        Address = "liz@example.com"
    });
    // Set the contact's phone numbers.
    newEntry.Phonenumbers.Add(new PhoneNumber()
    {
        Primary = true,
        Rel = ContactsRelationships.IsWork,
        Value = "(206)555-1212",
    });
    newEntry.Phonenumbers.Add(new PhoneNumber()
    {
        Rel = ContactsRelationships.IsHome,
        Value = "(206)555-1213",
    });
    // Set the contact's IM information.
    newEntry.IMs.Add(new IMAddress()
    {
       Primary = true,
       Rel = ContactsRelationships.IsHome,
       Protocol = ContactsProtocols.IsGoogleTalk,
    });
    // Set the contact's postal address.
    newEntry.PostalAddresses.Add(new StructuredPostalAddress()
    {
       Rel = ContactsRelationships.IsWork,
       Primary = true,
       Street = "1600 Amphitheatre Pkwy",
       City = "Mountain View",
       Region = "CA",
       Postcode = "94043",
       Country = "United States",
       FormattedAddress = "1600 Amphitheatre Pkwy Mountain View",
    });
    // Insert the contact.
    Uri feedUri = new Uri(ContactsQuery.CreateContactsUri("default"));
    ContactsService GContactService = null;
    GContactService=  new ContactsService("My Project 55318");
    GContactService.setUserCredentials("binal.p1391987@gmail.com", "abc@223133");

    ContactsFeed contactFeed = new ContactsFeed(feedUri, GContactService);
    // contactFeed.Insert(newEntry);
    Contact createdEntry = cr.Insert(feedUri, newEntry);
    Console.WriteLine("Contact's ID: " + createdEntry.Id);
    return createdEntry;
}

此行抛出异常"远程服务器返回错误:(401)未经授权。"

Contact createdEntry = cr.Insert(feedUri, newEntry);

任何人都有完整的代码示例,可以在asp.net中的gmail中添加联系人详细信息吗?

我需要在gmail中添加名称和电话号码吗?

0 个答案:

没有答案