【问题标题】:Use Google for OAuth in winform webbrowser control在 winform 网络浏览器控件中使用 Google 进行 OAuth
【发布时间】:2012-10-30 06:11:17
【问题描述】:

我想使用 C# winform 中内置的 webbrowser 控件来使用 Google 的 OAuth 对用户进行身份验证。我能够找到的所有内容都要求用户通过网络浏览器中的登录过程来获取访问令牌,然后将该令牌粘贴到我的应用程序中。如何自动获取该访问令牌,以便用户无需复制/粘贴?

【问题讨论】:

    标签: c# winforms browser oauth-2.0


    【解决方案1】:

    乔恩, 通常,Bearer 令牌作为 JSON 文档在 html-response 正文中发送回。 例如,来自规范。

      HTTP/1.1 200 OK
      Content-Type: application/json;charset=UTF-8
      Cache-Control: no-store
      Pragma: no-cache
    
      {
        "access_token":"mF_9.B5f-4.1JqM",
        "token_type":"Bearer",
        "expires_in":3600,
        "refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA"
      }
    

    如果是这种情况,您可以使用 WebBrowser.DocumentCompleted 事件获取响应,然后使用 JavaScriptSerializer 捕获 JSON

    JavaScriptSerializer jss = new JavaScriptSerializer();
    String json = webbrowser.DocumentText
    TokenResponse token = jss.Deserialize<TokenResponse>(json);
    
    
       public class TokenResponse
        {
            public string access_token { get; set; }
            public string token_type { get; set; }
            public string expires_in { get; set; }
            public string refresh_token { get; set; }
        }
    

    【讨论】:

      猜你喜欢
      • 2018-03-17
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 2011-03-19
      • 1970-01-01
      • 1970-01-01
      • 2011-06-11
      • 1970-01-01
      相关资源
      最近更新 更多