zoukankan      html  css  js  c++  java
  • ASP.NET/C# WebRequest POST Google OAuth API

    这篇文章主要是分享一段代码,解决的问题是:通过 WebRequest 向 https://accounts.google.com/o/oauth2/token 发起 HTTP POST 请求,根据 authorization code 获取 access_token。

    C#代码如下:

    public ActionResult GoogleOAuthCallback()
    {
        var webRequest = WebRequest.Create("https://accounts.google.com/o/oauth2/token") as HttpWebRequest;
        webRequest.Method = "POST";
        webRequest.ContentType = "application/x-www-form-urlencoded";
    
        var postData = string.Format("code={0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type=authorization_code",
            Request.QueryString["code"],
                "Client ID",
                "Client secret",
                "Redirect URIs");
    
        using(var sw = new StreamWriter(webRequest.GetRequestStream()))
        {
            sw.Write(postData);
        }
    
        using (var response = webRequest.GetResponse())
        {
            using (var sr = new StreamReader(response.GetResponseStream()))
            {
                return Content(sr.ReadToEnd());
            }
        }
    }
  • 相关阅读:
    Redis宣言
    软件工程
    分布式编程
    编程泛型
    tcp/ip高效编程总结
    IP协议详解
    gevent程序员指南
    网站架构
    这些话,是乔布斯给世间留下的真正伟大礼物
    Flink/Spark 如何实现动态更新作业配置
  • 原文地址:https://www.cnblogs.com/dudu/p/webrequest_post_google_oauth.html
Copyright © 2011-2022 走看看