0

我的问题是我无法在谷歌中添加联系人。我正在使用asp.net 2008。当我使用谷歌日历时,它可以毫无问题地保存。我不确定这是一个象征性的问题还是其他什么,所以我决定在这里问。下面是我添加联系人的代码

 protected void Create_Click()
    {

    GAuthSubRequestFactory authFactory_con = new GAuthSubRequestFactory("cp", "ContactApp");
    authFactory_con.Token = (String)Session["token"];
    ContactsService ser = new ContactsService(authFactory_con.ApplicationName);
    ser.RequestFactory = authFactory_con;               

    string str = "";



        ContactDetail contact = new ContactDetail
        {
            Name = NameTextBox.Text + " " + LastTextBox.Text,
            EmailAddress1 = primaryEmailTextBox.Text,
            EmailAddress2 = secondryEmailTextBox.Text,
            Phone = phoneTextBox.Text,
            Mobile = MobileTextBox.Text,
            Street = StreetTextBox.Text,
            City = CityTextBox.Text,
            Region = RegionTextBox.Text,
            PostCode = PostCodeTextBox.Text,
            Country = CountryTextBox.Text,
            Details = detailsTextBox.Text

        };

            GoogleContactService.AddContact(contact,ser);
            str = "<script>alert('Contact Added Sucessfully')</script>";

        Response.Write(str);   


}

上面的函数调用了 GoogleContactService 的 AddContact 函数。下面是添加联系人功能的代码

   public void AddContact(ContactDetail contact, ContactsService GContactService)
     {
    ContactEntry newEntry = new ContactEntry();

    newEntry.Title.Text = contact.Name;
    //newEntry.Name.FullName = contact.Name;  

    newEntry.Name = new Name();     
    newEntry.Name.FullName = contact.Name;

    EMail primaryEmail = new EMail(contact.EmailAddress1);
    primaryEmail.Primary = true;
    primaryEmail.Rel = ContactsRelationships.IsWork;
    newEntry.Emails.Add(primaryEmail);

    EMail secondaryEmail = new EMail(contact.EmailAddress2);
    secondaryEmail.Rel = ContactsRelationships.IsHome;
    newEntry.Emails.Add(secondaryEmail);

    PhoneNumber phoneNumber = new PhoneNumber(contact.Phone);
    phoneNumber.Rel = ContactsRelationships.IsHome ;
    newEntry.Phonenumbers.Add(phoneNumber);

    PhoneNumber phoneNumber_ = new PhoneNumber(contact.Mobile );
    phoneNumber_.Primary = true;
    phoneNumber_.Rel = ContactsRelationships.IsMobile ;
    newEntry.Phonenumbers.Add(phoneNumber_);

    newEntry.PostalAddresses.Add(new StructuredPostalAddress()
    {
        Rel = ContactsRelationships.IsWork,
        Primary = true,
        Street = contact.Street  ,
        City = contact.City  ,
        Region = contact.Region  ,
        Postcode = contact.PostCode  ,
        Country = contact.Country  ,
        FormattedAddress = contact.Street + " , " + contact.City + " , " + contact.Region + " , " + contact.PostCode + " , " + contact.Country,
    });      
    newEntry.Content.Content = contact.Details;
    Uri feedUri = new Uri(ContactsQuery.CreateContactsUri("default"));

   // Uri feedUri = new Uri("http://www.google.com/m8/feeds/contacts/default/full");
    System.Net.ServicePointManager.Expect100Continue = false;
    ContactEntry createdEntry = (ContactEntry)GContactService.Insert(feedUri, newEntry);

}

我在页面加载时获得令牌

以下是我尝试添加联系人时出现的错误。

用户代码未处理 GDataRequestException 请求执行失败:https//www.google.com/m8/feeds/contacts/default/full

4

1 回答 1

0

好吧,我得到了解决问题的方法。这是一个非常愚蠢的错误。

我在做什么,我正在使用多范围令牌(用于日历和联系人)为了获取多范围令牌,我使用下面的代码

 string nextUrl = Request.Url.ToString();
 const string scope = "http://www.google.com/calendar/feeds/%20http://www.google.com/m8/feeds/";
 const bool secure = false;
 const bool session = true;
 string authSubUrl = AuthSubUtil.getRequestUrl(nextUrl, scope, secure, session);
 Response.Redirect(authSubUrl);

从上面的“const string scope”变量中,您必须删除 %20 并留出空间。在 google Api 文档中,它被指定为 %20,但它不起作用。您必须删除 %20 并在它们之间添加空格。如下面的字符串。

 const string scope = "http://www.google.com/calendar/feeds/ http://www.google.com/m8/feeds/";

通过在字符串中使用 %20,您只能访问第一个服务而不是第二个。这就是为什么会出现所述错误。

于 2013-01-01T09:30:31.563 回答