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