【问题标题】:Google Contacts API with Service Account带有服务帐户的 Google 通讯录 API
【发布时间】:2015-07-23 06:54:30
【问题描述】:

我无法使用 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:\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);

【问题讨论】:

  • 并且您授予了服务帐户写入权限?

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


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2017-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 2021-03-18
    • 2015-08-26
    • 2013-12-27
    相关资源
    最近更新 更多