Google api 使用Google API修改Google联系人
我想开始开发带有.NET客户端的GoogleAPI。第一步,我尝试获取所有谷歌联系人,现在我想插入联系人。 我已经读了很多关于谷歌API的CRUD(创建、读取、更新和删除)联系人。有联系人API、人员API和其他(?)。什么是清除接触物的最佳方法Google api 使用Google API修改Google联系人,google-api,google-contacts-api,Google Api,Google Contacts Api,我想开始开发带有.NET客户端的GoogleAPI。第一步,我尝试获取所有谷歌联系人,现在我想插入联系人。 我已经读了很多关于谷歌API的CRUD(创建、读取、更新和删除)联系人。有联系人API、人员API和其他(?)。什么是清除接触物的最佳方法 UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync( new ClientSecrets { ClientId = "xyz.apps.goog
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = "xyz.apps.googleusercontent.com",
ClientSecret = " xyz"
},
new[] { "profile", "https://www.google.com/m8/feeds/contacts/xy%40gmail.com/full" },
"me",
CancellationToken.None).Result;
// Create the service.
var peopleService = new PeopleService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "WindowsClient-Google-Sync",
});
ListRequest listRequest = peopleService.People.Connections.List("people/me");
listRequest.SyncToken = null;
ListConnectionsResponse result = listRequest.Execute();
foreach (Person person in result.Connections)
{
foreach (Name name in person.Names)
{
Console.WriteLine("Name: " + name.DisplayName);
}
}
如何扩展此示例以创建或更新联系人
谢谢
Andreas如果您要检查:
GoogleContactsAPI允许客户端应用程序查看和更新用户的联系人。联系人存储在用户的Google帐户中;大多数谷歌服务都可以访问联系人列表
您的客户端应用程序可以使用Google Contacts API创建新联系人、编辑或删除现有联系人以及查询符合特定条件的联系人
创建联系人
要创建新联系人,请向用户的联系人提要URL发送授权的POST
请求,并在正文中包含联系人数据
URL的格式如下:
https://www.google.com/m8/feeds/contacts/{userEmail}/full
https://www.google.com/m8/feeds/contacts/userEmail/full/{contactId}
https://www.google.com/m8/feeds/contacts/{userEmail}/full/{contactId}
成功后,服务器用HTTP 201
创建的状态代码和创建的联系人条目响应,其中包含服务器设置的一些附加元素和属性(以粗体显示),例如id、各种链接元素和属性
更新联系人
要更新联系人,首先检索联系人条目,修改数据,并向联系人的编辑URL发送一个授权的PUT
请求,请求中包含修改后的联系人条目
URL的格式如下:
https://www.google.com/m8/feeds/contacts/{userEmail}/full
https://www.google.com/m8/feeds/contacts/userEmail/full/{contactId}
https://www.google.com/m8/feeds/contacts/{userEmail}/full/{contactId}
为确保发送到API的数据不会覆盖其他客户端的更改,应在请求标头中提供联系人条目的Etag
If-Match: Etag
If-Match: Etag
成功后,服务器将使用HTTP 200 OK
状态代码和更新的联系人条目进行响应
public static ContactEntry updateContactName(
ContactsService myService, URL contactURL)
throws ServiceException, IOException {
// First retrieve the contact to updated.
ContactEntry entryToUpdate = myService.getEntry(contactURL, ContactEntry.class);
entryToUpdate.getName().getFullName().setValue("New Name");
entryToUpdate.getName().getGivenName().setValue("New");
entryToUpdate.getName().getFamilyName().setValue("Name");
URL editUrl = new URL(entryToUpdate.getEditLink().getHref());
try {
ContactEntry contactEntry = myService.update(editUrl, entryToUpdate);
System.out.println("Updated: " + contactEntry.getUpdated().toString());
return contactEntry;
} catch (PreconditionFailedException e) {
// Etags mismatch: handle the exception.
}
return null;
}
删除联系人
要删除联系人,请向联系人的编辑URL发送授权的delete
请求
URL的格式如下:
https://www.google.com/m8/feeds/contacts/{userEmail}/full
https://www.google.com/m8/feeds/contacts/userEmail/full/{contactId}
https://www.google.com/m8/feeds/contacts/{userEmail}/full/{contactId}
为确保发送到API的数据不会覆盖其他客户端的更改,应在请求标头中提供联系人条目的Etag
If-Match: Etag
If-Match: Etag
成功后,服务器以http200ok
状态代码响应
public static void deleteContact(ContactsService myService, URL contactURL)
throws ServiceException, IOException {
// Retrieving the contact is required in order to get the Etag.
ContactEntry contact = myService.getEntry(contactURL, ContactEntry.class);
try {
contact.delete();
} catch (PreconditionFailedException e) {
// Etags mismatch: handle the exception.
}
}
而:
People API允许您列出经过身份验证的用户的联系人,并检索经过身份验证的用户及其联系人的配置文件信息
例如,假设经过身份验证的用户Jen的私人联系人中有Fabian和Ranjith。当你的应用程序调用people.connections.list来检索她的连接列表时,Jen会看到一个同意屏幕,要求该应用程序访问该列表。如果Jen同意,应用程序将检索包含Fabian和Ranjith的列表(每个人都有一个资源名)。然后,应用程序可以调用people.get,传入资源名称,以获取每个人的私人联系人和公共配置文件数据
谢谢你的回复。浏览器打开谷歌网站,授予访问权限。在创建联系人行时,“ContactEntry createdContact=contactsService.Insert(url,contact);”我得到了以下错误:请求执行失败:System.Net.WebException:HTTP错误401未在Google.GData.Client.GDataRequest.Execute()的System.Net.HttpWebRequest.GetResponse()上授权,因为我认为应该为服务提供用户名和密码,但如何?