Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过asp.net从Google mail中检索联系人_Asp.net_Asp.net Mvc 4 - Fatal编程技术网

如何通过asp.net从Google mail中检索联系人

如何通过asp.net从Google mail中检索联系人,asp.net,asp.net-mvc-4,Asp.net,Asp.net Mvc 4,我一直在使用此代码,因为有一个错误: 执行身份验证请求返回意外结果:404 DataSet ds=新数据集(); ds.Tables.Add(“Gmail联系人”); 表[0].Columns.Add(“EmailId”); RequestSettings rs=新的RequestSettings(“GetGmail联系人”,txtUsername.Text,txtPassword.Text); rs.autopage=true; ContactsRequest cr=新的ContactsReq

我一直在使用此代码,因为有一个错误:

执行身份验证请求返回意外结果:404

DataSet ds=新数据集();
ds.Tables.Add(“Gmail联系人”);
表[0].Columns.Add(“EmailId”);
RequestSettings rs=新的RequestSettings(“GetGmail联系人”,txtUsername.Text,txtPassword.Text);
rs.autopage=true;
ContactsRequest cr=新的ContactsRequest(rs);
Feed f=cr.GetContacts();
foreach(f.条目中的联系人t)
{
foreach(t.Emails中的电子邮件)
{
DataRow row=ds.Tables[0].NewRow();
行[“EmailId”]=email.Address.ToString();
ds.Tables[0].Rows.Add(row);
}
}
GridView1.DataSource=ds.Tables[0];
GridView1.DataBind();
lblStatus.Text=“+txtUsername.Text+”的总联系人:“+ds.Tables[0].Rows.Count.ToString();

台阶

1转到并创建项目

2选择项目,从左上角菜单中选择API和身份验证

3选择凭证

4使用按钮创建OAuth创建新的客户端ID(应用程序类型-已安装的应用程序)

5填写产品名称字段

6保存

之后,您将获得本机应用程序的客户机ID:客户机ID、客户机机密、重定向URI

从NuGet安装Google.api.Auth

代码

DataSet ds = new DataSet();
            ds.Tables.Add("GmailContact");
            ds.Tables[0].Columns.Add("EmailId");
            RequestSettings rs = new RequestSettings("GetGmailContact", txtUsername.Text, txtPassword.Text);
            rs.AutoPaging = true;
            ContactsRequest cr = new ContactsRequest(rs);
            Feed<Contact> f = cr.GetContacts();
            foreach (Contact t in f.Entries)
            {
                foreach (EMail email in t.Emails)
                {
                    DataRow row = ds.Tables[0].NewRow();
                    row["EmailId"] = email.Address.ToString();
                    ds.Tables[0].Rows.Add(row);
                }
            }
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
            lblStatus.Text = "Total Contact For" + txtUsername.Text + ":" + ds.Tables[0].Rows.Count.ToString();
 string clientId = null; // https://console.developers.google.com/project/xxx
        string clientSecret = null; // https://console.developers.google.com/project/xxx
        string accessCode = null; // You will get this code after GetAccessToken method
        string redirectUri = null; // https://console.developers.google.com/project/xxx
        string applicationName = null; // https://console.developers.google.com/project/xxx
        // Scopes https://support.google.com/a/answer/162106?hl=en
        string scopes = null; // put your scope like https://www.google.com/m8/feeds/
        string accessType = "offline";
        string tokenType = "refresh";

        OAuth2Parameters parameters = new OAuth2Parameters
        {
            ClientId = clientId,
            ClientSecret = clientSecret,
            RedirectUri = redirectUri,
            Scope = scopes,
            AccessType = accessType,
            TokenType = tokenType
        };

        if (accessCode == null)
        {
            string url = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);
            // Start webbrowser
            Process.Start(url);

            // Load code from web via popup, etc.
            parameters.AccessCode = accessCodeFromWeb;
        }

        // Check accessToken and refreshToken
        // After first acceess with  GetAccessToken you will get that information
        if (accessToken == null || refreshToken == null)
        {
            OAuthUtil.GetAccessToken(parameters);

            // Save yours accessToken and refreshToken for next connection
            accessToken = parameters.AccessToken;
            refreshToken = parameters.RefreshToken;
        }
        else
        {
            // Restore your token from config file, etc.
            parameters.AccessToken = accessToken;
            parameters.RefreshToken = refreshToken;
        }

        RequestSettings rs = new RequestSettings(applicationName, parameters);

        return new ContactsRequest(rs);