0

Hi i am going to work on Google apps for education to integrate with my .net web application.For that iam doing some R&D work on Google contacts API.I am using below code to get contacts using Contacts API.

using System;
using Google.Contacts;
using Google.GData.Contacts;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Gmail.v1;
using Google.Apis.Gmail.v1.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System.Threading;
using System.IO;
using GoogleClassRoomApi;
using System.Data;


public partial class _Default : System.Web.UI.Page
{
    RequestSettings settings = new RequestSettings("YOUR_APPLICATION_NAME");
    static string[] Scopes = { GmailService.Scope.GmailReadonly };
    static string ApplicationName = "Gmail API .NET Quickstart";
    UserCredential credential;
    protected void Page_Load(object sender, EventArgs e)
    {
        using (var stream = new FileStream(Server.MapPath("client_secret.json"), FileMode.Open, FileAccess.Read))
        {
            string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            credPath = Path.Combine(@"C:\Users\pradeep.s\Documents\googleKeys\ChoiceDemo\.credentials");
            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, Scopes, "user", CancellationToken.None, new FileDataStore(credPath, true)).Result;
        }
        var service = new GmailService(new BaseClientService.Initializer()
       {
           HttpClientInitializer = credential,
           ApplicationName = ApplicationName,
       });
        ContactsRequest cr = new ContactsRequest(settings);
    }

    public DataSet GetGmailContacts(string p_name, string e_id, string psw)
    {
        //p_name = name of your app; e_id = your gmail account; psw = password of your gmail account
        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)
        {
            Name nombreContacto = 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();
    }
}

But when i execute this i am getting below error. "Execution of authentication request returned unexpected result: 404" Even i am authenticating gmail with OAuth Gmail API,i am getting this error.Please help me to solve this problem. I am taking some of above code from below references.. http://www.codeproject.com/Tips/552508/ASP-NET-Import-Gmail-Contacts

Andres
  • 671
  • 4
  • 9
  • Check your OAuth, make sure you migrated your app to the OAuth 2.0. Go to this [link](https://developers.google.com/identity/protocols/OAuth_ref#migration) to verify if you're still running on the old Oauth. Also, this [SO](http://stackoverflow.com/questions/30469058/google-gdata-client-gdatarequestexception-authentication-suddenly-fails-in-old) should also answer your question. Hope that helps. – Andres Nov 18 '15 at 17:50
  • Thanks for your valuable answer. I am using OAuth 2.0 only. Still i am facing same issue – taylor jhon Nov 19 '15 at 08:03
  • If you're using OAuth 2, then you should expose the consent URL to the user click on it and allow importing. Take a look in this [CloudSponge demo page](http://www.cloudsponge.com/test-drive/). – Rael Gugelmin Cunha Nov 19 '15 at 11:58

0 Answers0