谷歌联系人API与服务帐户



我在使用google api v3创建google联系人时遇到了麻烦。我可以成功地调用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:AllContactsManager-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);

编辑:我找到了问题所在,只是想为其他有同样问题的人更新。如果您按照v3文档创建联系人:他们有一个关于联系人实体的章节,如下所示:

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

如果你删除这个IMAddress,然后联系人创建良好

相关内容

  • 没有找到相关文章

最新更新