locked
Importing Google Contacts using oAuth API vewrsion 3 RRS feed

Answers

  • User-955115217 posted

    Okay..I've implemented it..

    On button's click event I've added following code:

    if (Session["Access_Token"] != null)
    {
    //if access token available in session
    RequestSettings rs = new RequestSettings("MyApplicationName", Session["Access_Token"].ToString());
    rs.AutoPaging = true;
    ContactsRequest cr = new ContactsRequest(rs);

    //Function that retrieves all my google contacts and binds to the grid.
    BindGoogleGrid(cr);
    }

    else //no auth data, print link
    {

     OAuth2Parameters parameters = new OAuth2Parameters();

    parameters.ClientId = "myClientID";
    parameters.ClientSecret = "myClientSecret";
    parameters.RedirectUri = "myURI.aspx";
    parameters.Scope = "https://www.google.com/m8/feeds";

    parameters.ResponseType = "code";

    Response.Redirect(OAuthUtil.CreateOAuth2AuthorizationUrl(parameters));

    }

    public void BindGoogleGrid(ContactsRequest cr)
    {

    string Fullname = "";
    string EmailID = "";
    string FirstName = "";
    string LastName = "";

    try
    {
    Feed<Contact> f = cr.GetContacts();

    DataSet dtImportedContacts = new DataSet();

    DataTable dtName = new DataTable();


    //dtName.Columns.Add("Name");
    dtName.Columns.Add("FirstName");
    dtName.Columns.Add("LastName");
    dtName.Columns.Add("EmailID");

    foreach (Contact entry in f.Entries)
    {

    //EMail email = entry.Emails[0];
    if (entry.Emails.Count > 0)
    {
    //for fetching EmailID
    foreach (EMail email in entry.Emails)
    {
    if (email != null)
    {
    EmailID = entry.Emails[0].Address;
    if (EmailID == string.Empty)
    EmailID = "";
    }
    else
    {
    EmailID = "";
    }
    }

    //For fetching Contact Name
    if (entry.Name != null)
    {
    Name name = entry.Name;

    if (!string.IsNullOrEmpty(name.FullName))
    {
    Fullname = name.FullName;
    var Name = name.FullName.Split(' ');

    if (Name.Length >= 2)
    {
    FirstName = Name[0];
    LastName = Name[1];
    }

    else
    {
    FirstName = Name[0];
    LastName = "";
    }

    }

    else
    {
    FirstName = "";
    LastName = "";
    }

    }

    else
    {
    FirstName = "";
    LastName = "";
    }

    dtName.Rows.Add(FirstName, LastName, EmailID);
    }
    }


    dtImportedContacts.Tables.Add(dtName);

    if (dtImportedContacts.Tables[0].Rows.Count < 0)
    {

    lbl.Text = "No Contacts Found";

    }

    else
    {
    ImgBtnGImportAdd.Visible = true;

    GrdG.DataSource = dtImportedContacts;
    GrdG.DataBind();


    }

    }

    catch (Exception ex)
    {
    lbl.Text = ex.Message;
    }

    }

    And Then in Same Page's page_load(because the same page on which I've button and grid.. I've set as Redirect URI) event, I've added following code:

    if (Session["Access_Token"] != null)
    {
    //For Google Contacts in case of page reload or 'Import from Gmail button' reclicked

    //Same button's click event has been fired using Javascript
    ScriptManager.RegisterStartupScript(this, this.GetType(), "googleContactclick", "<script>GoogleContacts();;</Script>", false);

    //In GoogleContacts() javascript I've fired button's click event.
    }

    else if (Request.QueryString["code"] != null)
    {
    //For Google Contacts while returning from Google SignIN page
    OAuth2Parameters parameters = new OAuth2Parameters();


    parameters.ClientId = "myClientID";
    parameters.ClientSecret = "myClientSecret";
    parameters.RedirectUri = "myURI.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("~/YourCurrentPage.aspx");
    }

    And when you add contacts or in my case when I add fetched contacts .. I've cleared Access-Token which I've set it in session(follow page_load code).

    That's it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 21, 2014 5:20 AM

All replies

  • User-955115217 posted

    Anybody is there..

    Thursday, January 16, 2014 12:41 AM
  • User-46109667 posted

    The problem? once u have the username and pswd of the user then can easily get the contacts of that person from gmail. as with using the google credential only u can get the contacts of a person using google api. with the help of dowloading GData library u can do...

    Thursday, January 16, 2014 1:14 AM
  • User-955115217 posted

    Dear Ragavan,

    It will be very helpfull if you give me proper example

    because..previously I've implemented in the same way as mentioned in examples at above mentioned sites.. I even got the contacts

    but after 2-3 attempts.. google locked my Google A/c saying Suspicious login detected and now if I try enter username, password and try to fetch contacts.. It gives error 'Invalid

    Credentials' I guess that is not the proper way.

    Thursday, January 16, 2014 1:39 AM
  • User-46109667 posted

    As i am using same way, i have no problem of fetching contacts. have u traced the code where error occurs.

    as it throws Invalid Credentials, check your username & password and try..

    Friday, January 17, 2014 1:42 AM
  • User-955115217 posted

    Okay..I've implemented it..

    On button's click event I've added following code:

    if (Session["Access_Token"] != null)
    {
    //if access token available in session
    RequestSettings rs = new RequestSettings("MyApplicationName", Session["Access_Token"].ToString());
    rs.AutoPaging = true;
    ContactsRequest cr = new ContactsRequest(rs);

    //Function that retrieves all my google contacts and binds to the grid.
    BindGoogleGrid(cr);
    }

    else //no auth data, print link
    {

     OAuth2Parameters parameters = new OAuth2Parameters();

    parameters.ClientId = "myClientID";
    parameters.ClientSecret = "myClientSecret";
    parameters.RedirectUri = "myURI.aspx";
    parameters.Scope = "https://www.google.com/m8/feeds";

    parameters.ResponseType = "code";

    Response.Redirect(OAuthUtil.CreateOAuth2AuthorizationUrl(parameters));

    }

    public void BindGoogleGrid(ContactsRequest cr)
    {

    string Fullname = "";
    string EmailID = "";
    string FirstName = "";
    string LastName = "";

    try
    {
    Feed<Contact> f = cr.GetContacts();

    DataSet dtImportedContacts = new DataSet();

    DataTable dtName = new DataTable();


    //dtName.Columns.Add("Name");
    dtName.Columns.Add("FirstName");
    dtName.Columns.Add("LastName");
    dtName.Columns.Add("EmailID");

    foreach (Contact entry in f.Entries)
    {

    //EMail email = entry.Emails[0];
    if (entry.Emails.Count > 0)
    {
    //for fetching EmailID
    foreach (EMail email in entry.Emails)
    {
    if (email != null)
    {
    EmailID = entry.Emails[0].Address;
    if (EmailID == string.Empty)
    EmailID = "";
    }
    else
    {
    EmailID = "";
    }
    }

    //For fetching Contact Name
    if (entry.Name != null)
    {
    Name name = entry.Name;

    if (!string.IsNullOrEmpty(name.FullName))
    {
    Fullname = name.FullName;
    var Name = name.FullName.Split(' ');

    if (Name.Length >= 2)
    {
    FirstName = Name[0];
    LastName = Name[1];
    }

    else
    {
    FirstName = Name[0];
    LastName = "";
    }

    }

    else
    {
    FirstName = "";
    LastName = "";
    }

    }

    else
    {
    FirstName = "";
    LastName = "";
    }

    dtName.Rows.Add(FirstName, LastName, EmailID);
    }
    }


    dtImportedContacts.Tables.Add(dtName);

    if (dtImportedContacts.Tables[0].Rows.Count < 0)
    {

    lbl.Text = "No Contacts Found";

    }

    else
    {
    ImgBtnGImportAdd.Visible = true;

    GrdG.DataSource = dtImportedContacts;
    GrdG.DataBind();


    }

    }

    catch (Exception ex)
    {
    lbl.Text = ex.Message;
    }

    }

    And Then in Same Page's page_load(because the same page on which I've button and grid.. I've set as Redirect URI) event, I've added following code:

    if (Session["Access_Token"] != null)
    {
    //For Google Contacts in case of page reload or 'Import from Gmail button' reclicked

    //Same button's click event has been fired using Javascript
    ScriptManager.RegisterStartupScript(this, this.GetType(), "googleContactclick", "<script>GoogleContacts();;</Script>", false);

    //In GoogleContacts() javascript I've fired button's click event.
    }

    else if (Request.QueryString["code"] != null)
    {
    //For Google Contacts while returning from Google SignIN page
    OAuth2Parameters parameters = new OAuth2Parameters();


    parameters.ClientId = "myClientID";
    parameters.ClientSecret = "myClientSecret";
    parameters.RedirectUri = "myURI.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("~/YourCurrentPage.aspx");
    }

    And when you add contacts or in my case when I add fetched contacts .. I've cleared Access-Token which I've set it in session(follow page_load code).

    That's it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 21, 2014 5:20 AM