C# Google使用服务帐户联系API
我在使用google api v3在google中创建联系人时遇到问题。 我可以成功调用C# Google使用服务帐户联系API,c#,google-api,google-contacts-api,google-data-api,C#,Google Api,Google Contacts Api,Google Data Api,我在使用google api v3在google中创建联系人时遇到问题。 我可以成功调用cr.GetContacts()获取所有联系人(针对我指定的用户),但是cr.Insert给了我一个错误: 执行请求失败: 我错过什么了吗 string serviceAccountEmail = "111111111111-aaaa1a1aa11a1aaaaaaa11aaaa1aaa11@developer.gserviceaccount.com"; X509Certificate2 cer
cr.GetContacts()
获取所有联系人(针对我指定的用户),但是cr.Insert
给了我一个错误:
执行请求失败:
我错过什么了吗
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);
编辑:我解决了问题,只想为其他有同样问题的人更新。 如果您遵循创建联系人的步骤:他们在该联系人实体上有如下部分:
newEntry.IMs.Add(new IMAddress()
{
Primary = true,
Rel = ContactsRelationships.IsHome,
Protocol = ContactsProtocols.IsGoogleTalk,
});
如果您删除此IMAddress,则联系人将正常创建。您是否授予服务帐户写访问权限?