无法在Asp.net中使用AuthSuB在Google通讯录中添加联系人

时间:2012-12-28 04:38:47

标签: c# asp.net contacts

我的问题是我无法在谷歌中添加联系人。我正在使用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

1 个答案:

答案 0 :(得分:0)

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

我在做什么,我使用的是multiscope令牌(用于日历和联系人) 为获得multiscope令牌,我使用下面的代码

 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字符串范围”变量,您必须删除%20并给出空格。在谷歌Api文档中,它以%20给出,但它不起作用。您必须删除%20并在它们之间添加空格。如下面的字符串。

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

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