c#使用Google Contacts API添加联系人

时间:2019-01-26 19:03:51

标签: c# .net visual-studio

我正在尝试使用Microsoft Visual Studios .net应用程序中使用c#的Google Contacts API将联系人添加到Google Contacts。我的应用程序是台式机,没有在网站应用程序上使用。我已经注册了该应用程序,并拥有我的客户ID和密码。我有以下代码可添加联系人,但不知道如何发送发帖请求和所需令牌的代码。如果我需要添加任何其他using语句,请告诉我。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Mail;
using Word = Microsoft.Office.Interop.Word;

using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
using System.Data.OleDb;
using Google.Contacts;
using Google.GData.Contacts;
using Google.GData.Client;
using Google.GData.Extensions;

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"));
        Contact createdEntry = cr.Insert(feedUri, newEntry);
        Console.WriteLine("Contact's ID: " + createdEntry.Id);
        return createdEntry;
    }

// create a new google contact
    private void btnGoogleContact_Click(object sender, EventArgs e)
    {

        // ...
        RequestSettings settings = new RequestSettings("TBF Database");
        // Add authorization token.
        // ...
        ContactsRequest cr = new ContactsRequest(settings);
        // ...

        MessageBox.Show("Contact updated to Google Contacts", "Google 
        Contacts", MessageBoxButtons.OK, MessageBoxIcon.Information);

    }

0 个答案:

没有答案