locked
How to import Gmail Contacts using oAuth API2.0 RRS feed

  • Question

  • User945338570 posted

    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:

    <%@ Page Title="" Language="VB" MasterPageFile="~/BackendMain.master" AutoEventWireup="false" CodeFile="testfetchgmail_oauth.aspx.vb" Inherits="Gogotech.ProductCatalog_testfetchgmail_oauth" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="Child_Content" Runat="Server">
        <table width ="100%">
            <tr>
                <td>
                    <asp:Button ID="btn_submit" runat="server" Text ="Get Contacts" CssClass ="Button" /><br />
                    <asp:Label ID="lbl" runat ="server" ></asp:Label>
                </td>
            </tr>
            <tr>
                           <td>
                                <asp:GridView ID="DG_answers" runat="server" AutoGenerateColumns="False"   CssClass="Control_table" AlternatingRowStyle-BackColor ="#f0f8ff"
                                    Font-Size="11px" Width="100%">
                                     <AlternatingRowStyle CssClass="Grid_Alternate_Item" HorizontalAlign="left" VerticalAlign="Top"></AlternatingRowStyle>
                                <RowStyle CssClass="Grid_Item" VerticalAlign="Top" HorizontalAlign="left"></RowStyle>
                                <HeaderStyle CssClass="Control_Header" Wrap="False" HorizontalAlign="left"></HeaderStyle>
                                  <Columns >
                                        <asp:TemplateField HeaderText ="Email" >
                                            <ItemTemplate>
                                                <asp:Label ID="link_id" runat="server" Text='<%#Container.DataItem("EmailID")%>'></asp:Label>
                                            </ItemTemplate>
                                            <ItemStyle VerticalAlign="Top" />
                                        </asp:TemplateField>
                                  </Columns>
                                    </asp:GridView>
                                         </td>
                        </tr>
        
            </table>
    </asp:Content>
    
    

    Code Behind:

    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
    
    

     

    Please help me !

    Friday, December 12, 2014 5:00 AM

Answers

  • User945338570 posted

    I have successfully solved this issue:

    ON ASPX PAGE

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <link href="/assets/css/reset.css" rel="stylesheet">
        <link href="/assets/css/main.css" rel="stylesheet">
        <link href="/assets/css/fonts.css" rel="stylesheet">
        <link href="/assets/css/fonts.css" rel="stylesheet">
        <link href="/assets/css/jquery.fancybox-account.css" rel="stylesheet">
            <link rel="stylesheet" type="text/css" href="/assets/css/premain.css" />
        <link rel="stylesheet" type="text/css" href="/assets/css/responsive.css">
        <!--[if IE]><script src="assets/scripts/html5.js"></script><![endif]-->
        <script src="/assets/scripts/jquery-1.11.1.min.js"></script>
        <script src="/assets/scripts/jquery.fancybox.js"></script>
        <script src="/assets/scripts/function.js"></script>
        <style type="text/css">
            .bluebtn {
            border-radius: 30px;
            padding: 5px 20px;
            background: #61cae7;
            color: #FFFFFF;
            text-decoration: none;
            font-size: 19px;
            font-family: "exo_2regular";
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <section class="main full">
                    <div class="inner">
                        <div class="mainHead full">
                            <h2 style="float: none; color: #61cae7;">Your Google Contacts </h2>
                            <small style="font-size: 18px;color: #878b97;margin: 0 0 12px 0;float: left;">Select your friends to invite.</small>
                        </div>
                        <div class="yourInvites full" style="margin:0px 0 0;">
                            <table>
                                <tr>
                                    <td style="vertical-align:top">
                                        <section class="inviteCol">
                                             <span><strong><asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true"/></strong>Select All</span>   
                                         </section>
                                    </td>
                                    <td style="vertical-align:top">
                                        <asp:Button ID="btnAdd" runat="server" Text="Add Email" CssClass="bluebtn" Visible="false" />
                                    </td>
                                </tr>
                                <tr><td colspan="2"><asp:Label ID="lbl" runat="server"></asp:Label><br /></td></tr>
                                <tr>
                                    <td colspan="2">
                                        <%--<asp:GridView ID="DG_answers" runat="server" AutoGenerateColumns="False" CssClass="Control_table" AlternatingRowStyle-BackColor="#f0f8ff"
                                            Font-Size="11px" Width="100%">
                                            <AlternatingRowStyle CssClass="Grid_Alternate_Item" HorizontalAlign="left" VerticalAlign="Top"></AlternatingRowStyle>
                                            <RowStyle CssClass="Grid_Item" VerticalAlign="Top" HorizontalAlign="left"></RowStyle>
                                            <HeaderStyle CssClass="Control_Header" Wrap="False" HorizontalAlign="left"></HeaderStyle>
                                            <Columns>
    
                                                <asp:TemplateField HeaderText="Email">
                                                    <ItemTemplate>
                                                        <asp:Label ID="link_id" runat="server" Text='<%#Container.DataItem("EmailID")%>'></asp:Label>
                                                    </ItemTemplate>
                                                    <ItemStyle VerticalAlign="Top" />
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Select">
                                                    <ItemTemplate>
                                                        <asp:CheckBox ID="CHK_is_active" runat="server"></asp:CheckBox>
                                                    </ItemTemplate>
                                                    <ItemStyle VerticalAlign="Top" />
                                                </asp:TemplateField>
                                            </Columns>
                                        </asp:GridView>--%>
    
                                        <asp:Repeater runat="server" ID="repGoogleEmail">
                                            <ItemTemplate>
                                                <section class="inviteCol">
                                                    <span><strong><asp:CheckBox ID="chkEmailId" runat="server" /></strong><asp:Literal ID="lblEmail" runat="server" Text='<%#Eval("EmailID")%>'></asp:Literal></span>
                                                </section>
                                            </ItemTemplate>
                                        </asp:Repeater>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                    <!--/main -->
                </section>
            </div>
        </form>
    </body>
    </html>
    

    Code Behind:

    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
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 5, 2015 3:50 AM