如何更改Google GMail联系中的生日?

时间:2014-11-24 19:38:21

标签: asp.net vb.net google-contacts

我更改为从ASP.NET中的网络表单更改Gmail联系人的属性? 我可以改变大多数属性,但我不能改变生日。这是我的代码:

    Dim contact As Contact = cr.Retrieve(Of Contact)(contactURL)
    contact.Name.FullName = m.Prenom & " " & m.Nom
    contact.Name.FamilyName = m.Nom
    contact.Name.GivenName = m.Prenom
    contact.PrimaryEmail.Address = m.AdresseMail

    Dim adr As StructuredPostalAddress = contact.PostalAddresses(0)
    adr.Primary = True
    adr.Street = m.Adresse
    adr.Postcode = m.CodePostal
    adr.City = m.Ville
    adr.Rel = ContactsRelationships.IsHome

    Dim d As New Date
    d = CDate(m.DateNaissance)
    contact.ContactEntry.Birthday = d.Year & "-" & d.Month & "-" & d.Day

    Try
    Dim updatedContact As Contact = cr.Update(contact)

    Catch e As GDataVersionConflictException
       ' Do Something
    Finally
       'do Something
    End Try

我收到了一个错误:

    [WebException: Le serveur distant a retourné une erreur : (400) Demande incorrecte.]
    System.Net.HttpWebRequest.GetResponse() +6542104
    Google.GData.Client.GDataRequest.Execute() +136

    [GDataRequestException: Execution of request failed: https://www.google.com/m8/feeds/contacts/xxxxxxx/full/xxxxxxx]
       Google.GData.Client.GDataRequest.Execute() +291
       Google.GData.Client.GDataGAuthRequest.Execute(Int32 retryCounter) +653
       Google.GData.Client.GDataGAuthRequest.Execute() +34
       Google.GData.Client.Service.EntrySend(Uri feedUri, AtomBase baseEntry, GDataRequestType type, AsyncSendData data) +430
    Google.GData.Client.FeedRequest`1.Update(Y entry) +417
       FlbMembres.CtlMajGmail.MajGmail() in C:\Mes Sites Web\FLBMembres\FlbWeb\ControlesUser\CtlMajGmail.ascx.vb:187
       FlbMembres.CtlMajGmail.btnMaj_Click(Object sender, EventArgs e) in C:\Mes Sites Web\FLBMembres\FlbWeb\ControlesUser\CtlMajGmail.ascx.vb:200
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9628114
       System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
                                                    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

1 个答案:

答案 0 :(得分:0)

问题是你正在写这样的日期:

Dim d As New Date
d = CDate(m.DateNaissance)
contact.ContactEntry.Birthday = d.Year & "-" & d.Month & "-" & d.Day

日期类型的DayMonth属性是数字。这意味着,如果任何一个小于10,您将获得一个月或一个数字日。例如,2014年1月1日将成为2014-1-1

但Google希望将日期格式设置为yyyy-MM-dd

只需检查日期和月份的长度,并确保它们在需要时具有前导零。

由于您使用的是.NET,DateTime结构体采用ToString方法,该方法采用格式字符串。您可以用以下代码替换您的代码:

Dim d As New Date
d = CDate(m.DateNaissance)
contact.ContactEntry.Birthday = d.ToString("yyyy-MM-dd")

...你会得到一个你需要的格式的字符串,连接更少。 Bonne chance!