Google通讯录API无法正常使用

时间:2015-11-17 11:25:37

标签: .net google-api gmail-api google-contacts

您好我将开始使用Google应用程序进行教育,以便与我的.net网络应用程序集成。因为我正在使用Google联系人API进行一些R&amp; D工作。我正在使用以下代码来使用Contacts API获取联系人。< / p>

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();
    }
}

但是当我执行此操作时,我遇到了错误。 &#34; 执行身份验证请求返回意外结果:404 &#34; 即使我使用OAuth Gmail API对gmail进行身份验证,我也会收到此错误。请帮我解决此问题。 我从下面的参考文献中采取了上面的一些代码.. http://www.codeproject.com/Tips/552508/ASP-NET-Import-Gmail-Contacts

0 个答案:

没有答案