Google Contacts API会返回我发送给的电子邮件地址列表,但不会返回我的联系人列表中的电子邮件地址列表吗?
我正在使用谷歌联系人api并尝试获取我的Gmail联系人中的电子邮件列表。谷歌联系人是https://contacts.google.com/u/0/preview/all。
下面是我使用的代码:
public List<GMail> GetContacts(GooglePlusAccessToken serStatus)
{
List<GMail> ret = new List<GMail>();
string google_client_id = _ClientId;
string google_client_sceret = _ClientSeceret;
string refreshToken = serStatus.refresh_token;
string accessToken = serStatus.access_token;
string scopes = "https://www.google.com/m8/feeds/contacts/default/full/";
OAuth2Parameters oAuthparameters = new OAuth2Parameters()
{
ClientId = google_client_id,
ClientSecret = google_client_sceret,
RedirectUri = GetRedirectUrl(),
Scope = scopes,
AccessToken = accessToken,
RefreshToken = refreshToken
};
RequestSettings settings = new RequestSettings("<var>Invite GMail Friends</var>", oAuthparameters);
ContactsRequest cr = new ContactsRequest(settings);
ContactsQuery query = new ContactsQuery(ContactsQuery.CreateContactsUri("default"));
query.NumberToRetrieve = _NumberToRetrieve;
Feed<Contact> feed = cr.Get<Contact>(query);
foreach (Contact entry in feed.Entries)
{
foreach (EMail email in entry.Emails)
{
GMail g = new GMail(email.Address);
ret.Add(g);
}
}
return ret;
}
但由于某些原因,代码会返回我过去发送给的电子邮件地址列表,但不会返回我的联系人列表中的电子邮件地址。它是否正确?谁知道为什么?
由于
**感谢Allen的回答,这是我的代码,而且效果很好!联系人组实际上是:SystemGroup:MyContacts@gmail.com **
public List<GMail> GetContacts_2(GooglePlusAccessToken serStatus)
{
List<GMail> ret = new List<GMail>();
string google_client_id = _ClientId;
string google_client_sceret = _ClientSeceret;
/*Get Google Contacts From Access Token and Refresh Token*/
string refreshToken = serStatus.refresh_token;
string accessToken = serStatus.access_token;
string scopes = "https://www.google.com/m8/feeds/groups/default/full/?v=3.0";
OAuth2Parameters oAuthparameters = new OAuth2Parameters()
{
ClientId = google_client_id,
ClientSecret = google_client_sceret,
RedirectUri = GetRedirectUrl(),
Scope = scopes,
AccessToken = accessToken,
RefreshToken = refreshToken
};
RequestSettings settings = new RequestSettings("<var>Invite GMail Friends</var>", oAuthparameters);
ContactsRequest cr = new ContactsRequest(settings);
GroupsQuery query = new GroupsQuery(GroupsQuery.CreateGroupsUri("default"));
query.NumberToRetrieve = 1000; // _NumberToRetrieve; // 100;// 5000;
//Feed feed = cr.Get(query);
Feed<Group> feed = cr.Get<Group>(query);
string gpId = string.Empty;
foreach (Group gp in feed.Entries)
{
if (gp.Title.Contains("Contacts"))
{
gpId = gp.Id;
//break;
}
//// for testing
//GMail g = new GMail(gp.Title.Replace(" ", "") + "@gmail.com");
//ret.Add(g);
}
if (gpId.Length > 0)
{
scopes = "https://www.google.com/m8/feeds/contacts/default/full/?v=3.0";
oAuthparameters = new OAuth2Parameters()
{
ClientId = google_client_id,
ClientSecret = google_client_sceret,
RedirectUri = GetRedirectUrl(),
Scope = scopes,
AccessToken = accessToken,
RefreshToken = refreshToken
};
settings = new RequestSettings("<var>Invite GMail Friends</var>", oAuthparameters);
//ContactsRequest cr = new ContactsRequest(settings);
cr = new ContactsRequest(settings);
ContactsQuery query2 = new ContactsQuery(ContactsQuery.CreateContactsUri("default"));
query2.NumberToRetrieve = _NumberToRetrieve; // 100;// 5000;
query2.OrderBy = ContactsQuery.OrderByLastModified;
query2.SortOrder = ContactsQuery.SortOrderDescending;
query2.Group = gpId;
Feed<Contact> feed2 = cr.Get<Contact>(query2);
foreach (Contact entry in feed2.Entries)
{
foreach (EMail email in entry.Emails)
{
GMail g = new GMail(email.Address);
ret.Add(g);
}
}
//if (ret.Count <= 0)
//{
// GMail g = new GMail("NoContact@gmail.com");
// ret.Add(g);
//}
}
//else
//{
// // for testing
// GMail g = new GMail("NoGroup@gmail.com");
// ret.Add(g);
//}
return ret;
}
答案 0 :(得分:1)
首先,我感到痛苦。谷歌的API是最糟糕的。周期。
我绕过它的方式(不是一种有效的做事方式,但这是我能找到的最好的方式。否则Google会返回所有那些与帐户持有人通信的虚假联系电子邮件,可能是成千上万的此类条目一个典型的GMail帐户))就是这个(它的VB.net代码,但你可以得到一个要点并轻松转换)
首先检索联系人群并查看标题“联系人”
Dim settings As New RequestSettings("MyApp", RefreshOAuthToken())
Dim cr As New ContactsRequest(settings)
'retrieve Contacts group (this is to retrieve only real contacts)
Dim groupquery As New GroupsQuery(GroupsQuery.CreateGroupsUri("default"))
Dim fgrp As Feed(Of Group) = cr.Get(Of Group)(groupquery)
Dim GroupAtomId As String = ""
For Each gr In fgrp.Entries
If gr.Title.Contains("Contacts") Then
GroupAtomId = gr.Id
Exit For
End If
Next
然后使用联系小组的ID来联系
Dim query As New ContactsQuery(ContactsQuery.CreateContactsUri("default"))
query.NumberToRetrieve = 2000
query.OrderBy = ContactsQuery.OrderByLastModified
query.SortOrder = ContactsQuery.SortOrderDescending
query.Group = GroupAtomId
Dim f As Feed(Of Contact) = cr.Get(Of Contact)(query)
For Each entry As Contact In f.Entries
'Do something with the data, these are real contacts in GMail
Next
所以这里的要点是首先获取组ID并在联系人搜索中使用它。我相信这就是你的代码所缺少的。