如何在Windows窗体应用程序C#中检索Google联系人?

如何在Windows窗体应用程序C#中检索Google联系人?,c#,winforms,api,google-api,google-contacts-api,C#,Winforms,Api,Google Api,Google Contacts Api,我已经在asp.NETC#中使用OAuth2实现了检索Google联系人。但是我想为Windows窗体实现同样的功能?如何做到这一点?你能告诉我如何从C#修改Windows吗?及 之后如何注销用户? 我还想在Gridview中显示联系人的电子邮件ID。 先谢谢你 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using Syste

我已经在asp.NETC#中使用OAuth2实现了检索Google联系人。但是我想为Windows窗体实现同样的功能?如何做到这一点?你能告诉我如何从C#修改Windows吗?及 之后如何注销用户? 我还想在Gridview中显示联系人的电子邮件ID。 先谢谢你

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Net;
using Newtonsoft.Json;
using Google.GData.Client;
using Google.Contacts;
using Google.GData.Contacts;
using Google.GData.Extensions;
public partial class TutorialCode_GoogleContactAPI_google_contact_api : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["code"] != null)
            GetAccessToken();
    }
    protected void googleButton_Click(object sender, EventArgs e)
    {
        /*https://developers.google.com/accounts/docs/OAuth2InstalledApp
          https://developers.google.com/google-apps/contacts/v3/
          https://developers.google.com/accounts/docs/OAuth2WebServer
          https://developers.google.com/oauthplayground/
        */
        string clientId = "";
        string redirectUrl = "http://localhost:2216/index.aspx";
        Response.Redirect("https://accounts.google.com/o/oauth2/auth?redirect_uri=" + redirectUrl + "&response_type=code&client_id=" + clientId + "&scope=https://www.google.com/m8/feeds/&approval_prompt=force&access_type=offline");
    }
    public void GetAccessToken()
    {
        string code = Request.QueryString["code"];
        string google_client_id = "";
        string google_client_sceret = "";
        string google_redirect_url = "http://localhost:2216/index.aspx";

        /*Get Access Token and Refresh Token*/
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token");
        webRequest.Method = "POST";
        string parameters = "code=" + code + "&client_id=" + google_client_id + "&client_secret=" + google_client_sceret + "&redirect_uri=" + google_redirect_url + "&grant_type=authorization_code";
        byte[] byteArray = Encoding.UTF8.GetBytes(parameters);
        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.ContentLength = byteArray.Length;
        Stream postStream = webRequest.GetRequestStream();
        // Add the post data to the web request
        postStream.Write(byteArray, 0, byteArray.Length);
        postStream.Close();
        WebResponse response = webRequest.GetResponse();
        postStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(postStream);
        string responseFromServer = reader.ReadToEnd();
        GooglePlusAccessToken serStatus = JsonConvert.DeserializeObject<GooglePlusAccessToken>(responseFromServer);
        /*End*/
        GetContacts(serStatus);
    }
    public void GetContacts(GooglePlusAccessToken serStatus)
    {
        string refreshToken = serStatus.refresh_token;
        string accessToken = serStatus.access_token;
        string scopes = "https://www.google.com/m8/feeds/contacts/default/full/";
        OAuth2Parameters oAuthparameters = new OAuth2Parameters()
        {
            RedirectUri = "http://localhost:2216/index.aspx",
            Scope = scopes,
            AccessToken = accessToken,
            RefreshToken = refreshToken
        };


        RequestSettings settings = new RequestSettings("<var>YOUR_APPLICATION_NAME</var>", oAuthparameters);
        ContactsRequest cr = new ContactsRequest(settings);
        ContactsQuery query = new ContactsQuery(ContactsQuery.CreateContactsUri("default"));
        query.NumberToRetrieve = 5000;
        Feed<Contact> feed = cr.Get<Contact>(query);

        StringBuilder sb = new StringBuilder();
        int i = 1;
        foreach (Contact entry in feed.Entries)
        {
            foreach (EMail email in entry.Emails)
            {
                sb.Append(i + "&nbsp;").Append(email.Address).Append("<br/>");
                i++;
            }
        }
        /*End*/
        dataDiv.InnerHtml = sb.ToString();
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.IO;
使用系统文本;
Net系统;
使用Newtonsoft.Json;
使用Google.GData.Client;
使用Google.Contacts;
使用Google.GData.Contacts;
使用Google.GData.Extensions;
公共部分类教程代码\u谷歌联系人api\u谷歌联系人\u api:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
if(Request.QueryString[“code”!=null)
GetAccessToken();
}
受保护的无效Google按钮\u单击(对象发送者,事件参数e)
{
/*https://developers.google.com/accounts/docs/OAuth2InstalledApp
https://developers.google.com/google-apps/contacts/v3/
https://developers.google.com/accounts/docs/OAuth2WebServer
https://developers.google.com/oauthplayground/
*/
字符串clientId=“”;
字符串重定向URL=”http://localhost:2216/index.aspx";
响应。重定向(“https://accounts.google.com/o/oauth2/auth?redirect_uri=“+redirectUrl+”&response_type=code&client_id=“+clientId+”&scope=https://www.google.com/m8/feeds/&approval_prompt=force&access_type=offline");
}
public void GetAccessToken()
{
字符串代码=Request.QueryString[“code”];
字符串google_client_id=“”;
字符串google_client_sceret=“”;
字符串google\u redirect\u url=”http://localhost:2216/index.aspx";
/*获取访问令牌和刷新令牌*/
HttpWebRequest webRequest=(HttpWebRequest)webRequest.Create(“https://accounts.google.com/o/oauth2/token");
webRequest.Method=“POST”;
string parameters=“code=”+code+”&client\u id=“+google\u client\u id+”&client\u secret=“+google\u client\u sceret+”&redirect\u uri=“+google\u redirect\u url+”&grant\u type=authorization\u code”;
byte[]byteArray=Encoding.UTF8.GetBytes(参数);
webRequest.ContentType=“application/x-www-form-urlencoded”;
webRequest.ContentLength=byteArray.Length;
Stream postStream=webRequest.GetRequestStream();
//将post数据添加到web请求
Write(byteArray,0,byteArray.Length);
postStream.Close();
WebResponse=webRequest.GetResponse();
postStream=response.GetResponseStream();
StreamReader=新的StreamReader(postStream);
字符串responseFromServer=reader.ReadToEnd();
GooglePlusAccessToken serStatus=JsonConvert.DeserializeObject(responseFromServer);
/*结束*/
获取联系人(serStatus);
}
public void GetContacts(GooglePlusAccessToken serStatus)
{
字符串refreshttoken=serStatus.refresh\u token;
字符串accessToken=serStatus.access\u token;
字符串范围=”https://www.google.com/m8/feeds/contacts/default/full/";
OAuth2Parameters OAuth2Parameters=新的OAuth2Parameters()
{
重定向URI=”http://localhost:2216/index.aspx",
范围=范围,
AccessToken=AccessToken,
RefreshToken=RefreshToken
};
RequestSettings设置=新的RequestSettings(“您的应用程序名称”,oAuthparameters);
ContactsRequest cr=新的ContactsRequest(设置);
ContactsQuery query=newcontactSquery(ContactsQuery.CreateContactsUri(“默认”));
query.numbertorretrieve=5000;
Feed=cr.Get(查询);
StringBuilder sb=新的StringBuilder();
int i=1;
foreach(feed.Entries中的联系人条目)
{
foreach(条目中的电子邮件。电子邮件)
{
sb.Append(i+)。Append(email.Address)。Append(“
”); i++; } } /*结束*/ dataDiv.InnerHtml=sb.ToString(); } }

希望这有帮助。您可以在UI上使用提要返回值和绑定/显示

using Google.Contacts;
using Google.GData.Client;
using Google.GData.Contacts;
using Google.GData.Extensions;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            GetAccessToken();

        }

        public void GetAccessToken()
        {
            string code = "";
            string google_client_id = "";
            string google_client_sceret = "";
            string google_redirect_url = "http://localhost:2216/index.aspx";

            /*Get Access Token and Refresh Token*/
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token");
            webRequest.Method = "POST";
            string parameters = "code=" + code + "&client_id=" + google_client_id + "&client_secret=" + google_client_sceret + "&redirect_uri=" + google_redirect_url + "&grant_type=authorization_code";
            byte[] byteArray = Encoding.UTF8.GetBytes(parameters);
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ContentLength = byteArray.Length;
            Stream postStream = webRequest.GetRequestStream();
            // Add the post data to the web request
            postStream.Write(byteArray, 0, byteArray.Length);
            postStream.Close();
            WebResponse response = webRequest.GetResponse();
            postStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(postStream);
            string responseFromServer = reader.ReadToEnd();
            GooglePlusAccessToken serStatus = JsonConvert.DeserializeObject<GooglePlusAccessToken>(responseFromServer);
            /*End*/
            GetContacts(serStatus);
        }
        public Feed<Contact> GetContacts(GooglePlusAccessToken serStatus)
        {
            string refreshToken = serStatus.refresh_token;
            string accessToken = serStatus.access_token;
            string scopes = "https://www.google.com/m8/feeds/contacts/default/full/";
            OAuth2Parameters oAuthparameters = new OAuth2Parameters()
            {
                RedirectUri = "http://localhost:2216/index.aspx",
                Scope = scopes,
                AccessToken = accessToken,
                RefreshToken = refreshToken
            };


            RequestSettings settings = new RequestSettings("<var>YOUR_APPLICATION_NAME</var>", oAuthparameters);
            ContactsRequest cr = new ContactsRequest(settings);
            ContactsQuery query = new ContactsQuery(ContactsQuery.CreateContactsUri("default"));
            query.NumberToRetrieve = 5000;
            Feed<Contact> feed = cr.Get<Contact>(query);

            return feed;
        }

    }
}
使用Google.Contacts;
使用Google.GData.Client;
使用Google.GData.Contacts;
使用Google.GData.Extensions;
使用Newtonsoft.Json;
使用制度;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.IO;
使用System.Linq;
Net系统;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
命名空间WindowsFormsApp1
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
GetAccessToken();
}
public void GetAccessToken()
{
字符串代码=”;
字符串google_client_id=“”;
字符串google_client_sceret=“”;
字符串google\u redirect\u url=”http://localhost:2216/index.aspx";
/*获取访问令牌和刷新令牌*/
HttpWebRequest webRequest=(HttpWebRequest)webRequest.Create(“https://accounts.google.com/o/oauth2/token");
webRequest.Method=“POST”;
string parameters=“code=”+code+”&client\u id=“+google\u client\u id+”&client\u secret=“+google\u client\u sceret+”&redirect\u uri=“+google\u redirect\u url+”&grant\u type=authorization\u code”;
byte[]byteArray=Encoding.UTF8.GetBytes(参数);
webRequest.ContentType=“application/x-www-form-urlencoded”;
webRequest.ContentLength=byteArray.Length;
Stream postStream=webRequest.GetRequestStream();
//将post数据添加到web请求