Imports Google.GData.Contacts
Imports Google.GData.Client
Imports Google.GData.Extensions
Imports Google.Contacts
Namespace Gogotech
Partial Class ProductCatalog_testfetchgmail_oauth
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
If Session("Access_Token") IsNot Nothing Then
Dim parameters As New OAuth2Parameters()
parameters.ClientId = "myclientid"
parameters.ClientSecret = "secretcode"
parameters.RedirectUri = "http://localhost:162/ProductCatalog/testfetchgmail_oauth.aspx"
parameters.Scope = "https://www.google.com/m8/feeds"
parameters.AccessCode = Request.QueryString("code")
Google.GData.Client.OAuthUtil.GetAccessToken(parameters)
'Dim requestFactory As New GOAuth2RequestFactory(Nothing, "MyApplicationName", parameters)
Dim rs As New RequestSettings("MyApplicationName", parameters)
rs.AutoPaging = True
Dim cr As New ContactsRequest(rs)
BindGoogleGrid(cr)
End If
End If
End Sub
Protected Sub btn_submit_Click(sender As Object, e As EventArgs) Handles btn_submit.Click
If Session("Access_Token") IsNot Nothing Then
'if access token available in session
Dim rs As New RequestSettings("MyApplicationName", Session("Access_Token").ToString())
rs.AutoPaging = True
Dim cr As New ContactsRequest(rs)
'Function that retrieves all my google contacts and binds to the grid.
BindGoogleGrid(cr)
Else
'no auth data, print link
Dim parameters As New OAuth2Parameters()
parameters.ClientId = "myclientid"
parameters.ClientSecret = "secretcode"
parameters.RedirectUri = "http://localhost:162/ProductCatalog/testfetchgmail_oauth.aspx"
parameters.Scope = "https://www.google.com/m8/feeds"
parameters.ResponseType = "code"
Response.Redirect(OAuthUtil.CreateOAuth2AuthorizationUrl(parameters))
End If
End Sub
Public Sub BindGoogleGrid(cr As ContactsRequest)
Dim Fullname As String = ""
Dim EmailID As String = ""
Dim FirstName As String = ""
Dim LastName As String = ""
' Try
Dim f As Feed(Of Contact) = cr.GetContacts()
Dim dtImportedContacts As New DataSet()
Dim dtName As New DataTable()
'dtName.Columns.Add("Name");
dtName.Columns.Add("FirstName")
dtName.Columns.Add("LastName")
dtName.Columns.Add("EmailID")
For Each entry As Contact In f.Entries
'EMail email = entry.Emails[0];
If entry.Emails.Count > 0 Then
'for fetching EmailID
For Each email As EMail In entry.Emails
If email IsNot Nothing Then
EmailID = entry.Emails(0).Address
If EmailID = String.Empty Then
EmailID = ""
End If
Else
EmailID = ""
End If
Next
If entry.Name IsNot Nothing Then
Dim name__1 As Name = entry.Name
If Not String.IsNullOrEmpty(name__1.FullName) Then
Fullname = name__1.FullName
Dim Name__2 = name__1.FullName.Split(" "c)
If Name__2.Length >= 2 Then
FirstName = Name__2(0)
LastName = Name__2(1)
Else
FirstName = Name__2(0)
LastName = ""
End If
Else
FirstName = ""
LastName = ""
End If
Else
FirstName = ""
LastName = ""
End If
dtName.Rows.Add(FirstName, LastName, EmailID)
End If
Next
dtImportedContacts.Tables.Add(dtName)
If dtImportedContacts.Tables(0).Rows.Count < 0 Then
lbl.Text = "No Contacts Found"
Else
btn_submit.Visible = True
DG_answers.DataSource = dtImportedContacts
DG_answers.DataBind()
End If
'Catch ex As Exception
'lbl.Text = ex.Message
' End Try
End Sub
End Class
End Namespace
Imports Google.GData.Contacts
Imports Google.GData.Client
Imports Google.GData.Extensions
Imports Google.Contacts
Partial Class Default2
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
If Session("Access_Token") IsNot Nothing Then
Dim rs As New RequestSettings("MyApplicationName", Session("Access_Token").ToString())
rs.AutoPaging = True
Dim cr As New ContactsRequest(rs)
'Function that retrieves all my google contacts and binds to the grid.
BindGoogleGrid(cr)
ElseIf Request.QueryString("code") IsNot Nothing Then
Dim parameters As New OAuth2Parameters()
parameters.ClientId = "ClientID"
parameters.ClientSecret = "ClientSecret"
parameters.RedirectUri = "http://test.com/Google_Contacts/default2.aspx"
parameters.Scope = "https://www.google.com/m8/feeds"
parameters.ResponseType = "code"
parameters.AccessCode = Request.QueryString("code")
OAuthUtil.GetAccessToken(parameters)
Session("Access_Token") = parameters.AccessToken
Response.Redirect("~/default2.aspx")
Else
Dim parameters As New OAuth2Parameters()
parameters.ClientId = "ClientID"
parameters.ClientSecret = "ClientSecret"
parameters.RedirectUri = "http://test.com/Google_Contacts/default2.aspx"
parameters.Scope = "https://www.google.com/m8/feeds"
parameters.ResponseType = "code"
Response.Redirect(OAuthUtil.CreateOAuth2AuthorizationUrl(parameters))
End If
End If
End Sub
Public Sub BindGoogleGrid(cr As ContactsRequest)
Dim Fullname As String = ""
Dim EmailID As String = ""
Dim FirstName As String = ""
Dim LastName As String = ""
' Try
Dim f As Feed(Of Contact) = cr.GetContacts()
Dim dtImportedContacts As New DataSet()
Dim dtName As New DataTable()
'dtName.Columns.Add("Name");
dtName.Columns.Add("FirstName")
dtName.Columns.Add("LastName")
dtName.Columns.Add("EmailID")
For Each entry As Contact In f.Entries
'EMail email = entry.Emails[0];
If entry.Emails.Count > 0 Then
'for fetching EmailID
For Each email As EMail In entry.Emails
If email IsNot Nothing Then
EmailID = entry.Emails(0).Address
If EmailID = String.Empty Then
EmailID = ""
End If
Else
EmailID = ""
End If
Next
'For fetching Contact Name
If entry.Name IsNot Nothing Then
Dim name__1 As Name = entry.Name
If Not String.IsNullOrEmpty(name__1.FullName) Then
Fullname = name__1.FullName
Dim Name__2 = name__1.FullName.Split(" "c)
If Name__2.Length >= 2 Then
FirstName = Name__2(0)
LastName = Name__2(1)
Else
FirstName = Name__2(0)
LastName = ""
End If
Else
FirstName = ""
LastName = ""
End If
Else
FirstName = ""
LastName = ""
End If
dtName.Rows.Add(FirstName, LastName, EmailID)
End If
Next
dtImportedContacts.Tables.Add(dtName)
If dtImportedContacts.Tables(0).Rows.Count < 0 Then
lbl.Text = "No Contacts Found"
Else
btnAdd.Visible = True
repGoogleEmail.DataSource = dtImportedContacts
repGoogleEmail.DataBind()
dtImportedContacts.Dispose()
End If
'Catch ex As Exception
'lbl.Text = ex.Message
' End Try
End Sub
Protected Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim sEmailIds As String = ""
For Each dr As RepeaterItem In repGoogleEmail.Items
If CType(dr.FindControl("chkEmailId"), CheckBox).Checked Then
sEmailIds &= CType(dr.FindControl("lblEmail"), Literal).Text & ","
End If
Next
sEmailIds = sEmailIds.Remove(sEmailIds.Length - 1, 1)
'lbl.Text = sEmailIds
CommonCode.Create_Cookie("GoogleEmailId", sEmailIds)
CommonCode.Create_Cookie("PageName", "SendInvitation")
Response.Write("<script type='text/javascript'>window.close();</script>")
Response.Redirect("http://localhost:164/share.aspx")
End Sub
Protected Sub chkAll_CheckedChanged(sender As Object, e As EventArgs) Handles chkAll.CheckedChanged
SelectAll()
End Sub
Protected Sub SelectAll()
Dim str As String = ""
For Each dr As RepeaterItem In repGoogleEmail.Items
If chkAll.Checked Then
CType(dr.FindControl("chkEmailId"), CheckBox).Checked = True
Else
CType(dr.FindControl("chkEmailId"), CheckBox).Checked = False
End If
Next
End Sub
End Class
Member
31 Points
188 Posts
How to import Gmail Contacts using oAuth API2.0
Dec 12, 2014 05:00 AM|shashikant2011|LINK
hi
I want to import all gmail contacts using oAuth2.0. I am following this link http://forums.asp.net/t/1961519.aspx?Importing+Google+Contacts+using+oAuth+API+vewrsion+3
But the code is not working. Actually in Session("Access_Token") there is no value stored.
Here is my code:
ASPX PAGE:
Code Behind:
Please help me !
email gmail vb.net OAuth asp.net
Member
31 Points
188 Posts
Re: How to import Gmail Contacts using oAuth API2.0
Jan 05, 2015 03:50 AM|shashikant2011|LINK
I have successfully solved this issue:
ON ASPX PAGE
Code Behind: