C# 实现 使用OAuth2.0 登录 Google 服务的代码

将写内容过程中比较常用的一些内容段做个珍藏,下面内容内容是关于C# 实现 使用OAuth2.0 登录 Google 服务的内容,应该对各位朋友有所用。

            string scope = "";

            string redirectUri = "urn:ietf:wg:oauth:2.0:oob";

            string grant_type = "authorization_code";

            this.HttpBody.Text = string.Format(

                "code={0}&redirect_uri={1}&client_id={2}&scope=&client_secret={3}&grant_type={4}",

                Uri.EscapeDataString(this.AuthorizationCode.Text),

                Uri.EscapeDataString(redirectUri),

                Uri.EscapeDataString(clientId),

                Uri.EscapeDataString(clientSecret),

                Uri.EscapeDataString(grant_type)

                );

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetURL);

            request.Method = method;

            request.ContentType = "application/x-www-form-urlencoded";

            for (int i = 0; i < header.Count;i++ )

            {

                request.Headers.Add(header.ElementAt(i).Key + ":" + header.ElementAt(i).Value);

            }

            Stream requestStream = request.GetRequestStream();

            StreamWriter writer = new StreamWriter(requestStream);

            writer.Write(httpBody);

            writer.Flush();

            writer.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Stream responseStream = response.GetResponseStream();

            StreamReader reader = new StreamReader(responseStream);

            string result = reader.ReadToEnd();

            this.RecieveData.Text = result;

你可能感兴趣的:(C# 实现 使用OAuth2.0 登录 Google 服务的代码)