带有服务帐户的Google通讯录API

时间:2015-07-23 06:54:31

标签: c# google-api google-contacts google-data-api

我无法使用google api v3在谷歌中创建联系人。 我可以成功致电cr.GetContacts()以获取所有联系人(针对我指定的用户),但cr.Insert正在给我错误:

  

执行请求失败:https://www.google.com/m8/feeds/contacts/someuser@mydomain.com/full

我错过了什么吗?

    string serviceAccountEmail = "111111111111-aaaa1a1aa11a1aaaaaaa11aaaa1aaa11@developer.gserviceaccount.com";
    X509Certificate2 certificate = new X509Certificate2(@"C:\All\ContactsManager-1aa1bbb1ab11.p12", "notasecret", X509KeyStorageFlags.Exportable);

    ServiceAccountCredential credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
    {
        Scopes = new[]
            {
                "https://www.google.com/m8/feeds"
            },
        User = "someuser@mydomain.com"
    }.FromCertificate(certificate));

    credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Wait();

    RequestSettings rs = new RequestSettings("ContactsManager", credential.Token.AccessToken);
    //rs.AutoPaging = true;

    ContactsRequest cr = new ContactsRequest(rs);
    //var contacts = cr.GetContacts();  //////THIS LINE WORKS AND GETS ALL THE CONTACTS

    Uri feedUri = new Uri(ContactsQuery.CreateContactsUri("someuser@mydomain.com"));
    //feedUri will now be    https://www.google.com/m8/feeds/contacts/someuser%40mydomain.com/full

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

    Console.WriteLine("Contact's ID: " + createdEntry.Id);

1 个答案:

答案 0 :(得分:1)

编辑:我想出了问题,只想更新其他有相同问题的人。 如果您按照v3 documentation创建联系人:他们在该联系人实体上有一个部分,如下所示:

newEntry.IMs.Add(new IMAddress()
  {
    Primary = true,
    Rel = ContactsRelationships.IsHome,
    Protocol = ContactsProtocols.IsGoogleTalk,
  });

如果删除此IMAddress,则联系人创建正常。