我试图在datagridview中获取所有谷歌联系人数据,谷歌开发者文档不是很有用。我使用此代码但只检索:姓名,电子邮件,电话和生日。我怎样才能检索所有?
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports Google.GData.Client
Imports Google.GData.Extensions
Imports Google.GData.Contacts
Imports Google.Contacts
Imports System.Collections
Imports System.Data
Imports System.Xml
Partial Public Class _Default
Dim Secrets = New ClientSecrets()
Dim scope = New List(Of String)
Dim initializer = New BaseClientService.Initializer
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Dim rs As New RequestSettings("APP NAME", "EMAIL@gmail.com", "PASSWORD")
'Application Name,Username,password
Dim cr As New ContactsRequest(rs)
'Request all contacts
rs.AutoPaging = True
'Allow autopaging
Dim f As Feed(Of Contact) = cr.GetContacts()
'Get all contacts
Dim dt As New DataTable()
Dim dr As DataRow
dt.Columns.Add("Name")
dt.Columns.Add("Phone")
dt.Columns.Add("EmailHome")
dt.Columns.Add("EmailWork")
dt.Columns.Add("EmailOther")
dt.Columns.Add("Birthday")
dt.Columns.Add("Address")
For Each exi As Contact In f.Entries
dr = dt.NewRow()
Dim n As Name = exi.Name
dr(0) = n.FullName
Dim homeemail As String = ""
Dim workemail As String = ""
Dim otheremail As String = ""
Dim homephone As String = ""
Dim workphone As String = ""
Dim otherphone As String = ""
Dim birth As String = ""
Dim address As String = ""
For Each ph As PhoneNumber In exi.Phonenumbers
If ph.Other = True Then
If otherphone.Equals("") Then
otherphone += ph.Value
Else
otherphone += ","
otherphone += ph.Value
End If
ElseIf ph.Home = True Then
If homephone.Equals("") Then
homephone += ph.Value
Else
homephone += ","
homephone += ph.Value
End If
Else
If workphone.Equals("") Then
workphone += ph.Value
Else
workphone += ","
workphone += ph.Value
End If
End If
dr(1) = workphone
Next
For Each email As EMail In exi.Emails
If email.Home = True Then
If homeemail.Equals("") Then
homeemail += email.Address
Else
homeemail += ","
homeemail += email.Address
End If
End If
If email.Work = True Then
If workemail.Equals("") Then
workemail += email.Address
Else
workemail += ","
workemail += email.Address
End If
Else
If otheremail.Equals("") Then
otheremail += email.Address
Else
otheremail += ","
otheremail += email.Address
End If
End If
dr(2) = homeemail
dr(3) = workemail
dr(4) = otheremail
Next
Try
birth = exi.ContactEntry.Birthday.ToString
Catch ex As Exception
birth = ""
End Try
dr(5) = birth
For Each wb As Website In exi.ContactEntry.Websites
address = wb.ToString
dr(6) = address
Next
dt.Rows.Add(dr)
Next
DataGridView1.DataSource = dt
End Sub
答案 0 :(得分:0)
这是一个简单的例子,它工作正常(asp.net)。添加所需的字段。
首先,添加两个文本框(txtgmailusername,txtpassword),一个按钮(Button1)和一个gridView(gridemails)并粘贴此代码:
public DataSet GetGmailContacts(string p_name, string e_id, string psw)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn();
dc1.DataType = Type.GetType("System.String");
dc1.ColumnName = "emailid";
DataColumn dc2 = new DataColumn();
dc2.DataType = Type.GetType("System.String");
dc2.ColumnName = "name";
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
RequestSettings rs = new RequestSettings(p_name, e_id, psw);
rs.AutoPaging = true;
ContactsRequest cr = new ContactsRequest(rs);
Feed<Contact> f = cr.GetContacts();
int counter = 0;
foreach (Contact t in f.Entries)
{
foreach (EMail email in t.Emails)
{
//DataRow dr1 = dt.NewRow();
//dr1["emailid"] = email.Address.ToString();
//dr1["name"] = email.XmlName.ToString();
//dt.Rows.Add(dr1);
//counter++;
}
Name nameContact = t.Name;
DataRow drx = dt.NewRow();
drx["emailid"] = t.PrimaryEmail.Address.ToString();
drx["name"] = t.Title.ToString();
dt.Rows.Add(drx);
counter++;
}
ds.Tables.Add(dt);
Response.Write("Total:" + counter.ToString());
return ds;
}
public void getcontacts()
{
DataSet ds = GetGmailContacts("MyNetwork Web Application!", txtgmailusername.Text, txtpassword.Text);
gridemails.DataSource = ds;
gridemails.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
getcontacts();
//create1contact("MyUsers", this.txtgmailusername.Text, this.txtpassword.Text);
}