Google 通讯录 API 会返回我发送到的电子邮件地址列表,但不会返回联系人列表中的电子邮件地址
本文关键字:列表 电子邮件地址 返回 联系人 API 通讯录 Google | 更新日期: 2023-09-27 18:32:32
Google 通讯录 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;
}
但由于某些原因,代码返回我过去发送到的电子邮件地址列表,但不返回联系人列表中的电子邮件。这是对的吗?有人知道为什么吗?
谢谢
** 感谢艾伦的回答,这是我的代码,效果很好!联系人组实际上是: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;
}
首先,我感受到你的痛苦。谷歌的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 并在联系人搜索中使用它。 我相信这就是您的代码所缺少的。