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

代码片段1>>>

            string clientId = "**********.apps.googleusercontent.com";
            string scope = "";
            string clientSecret = "********************";
            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)
                );

代码片段2>>>


            // create an request object
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetURL);
            // set up the method
            request.Method = method;
            request.ContentType = "application/x-www-form-urlencoded";
            // set up the headers 可以不加。GData-Version:3.0这个头已经不需要了吧。这里只是为了只GData-Version:3.0
            for (int i = 0; i < header.Count;i++ )
            {
                request.Headers.Add(header.ElementAt(i).Key + ":" + header.ElementAt(i).Value);
            }

            // set up the http body
            Stream requestStream = request.GetRequestStream();
            StreamWriter writer = new StreamWriter(requestStream);
            writer.Write(httpBody);
            writer.Flush();
            writer.Close();
            // get the response
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            string result = reader.ReadToEnd();
            this.RecieveData.Text = result;


你可能感兴趣的:(C#)