【问题标题】:Get OAuth2 access token from CefSharp从 CefSharp 获取 OAuth2 访问令牌
【发布时间】:2019-07-03 18:58:38
【问题描述】:

感谢您阅读我的问题。 我正在 Stex.com 上开发“硬币交换器”桌面应用程序。 我可以通过邮递员获取 access_token 和 refresh_token。 这是细节。 https://help.stex.com/en/articles/2740368-how-to-connect-to-the-stex-api-v3-using-postman

现在我想在没有邮递员的情况下获取 access_token 和 refresh_token。所以我将 CefSharp Nuget Package 添加到我的软件中并手动登录。 轮到获取access_token和refresh_token了。 我以为我可以通过 cookie 获得它们,但我失败了。 这就是我所做的。

        private void btnGetAuth_Click(object sender, EventArgs e)
        {
            CookieVisitor _cookieVisitor = new CookieVisitor();
            var mgr = browser.GetCookieManager();
            mgr.VisitAllCookies(_cookieVisitor);            
        }

..............................
public class CookieVisitor : ICookieVisitor
    {
        public CookieVisitor()
        {
            IsReady = true;
        }
        public bool Visit(Cookie cookie, int count, int total, ref bool deleteCookie)
        {
            lock (this)
            {
                if (_cookies.ContainsKey(cookie.Name))
                {
                    _cookies[cookie.Name] = new System.Net.Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain);
                    _cookies[cookie.Name].Name = cookie.Name;
                    _cookies[cookie.Name].Value = cookie.Value;
                    _cookies[cookie.Name].Path = cookie.Path;
                    _cookies[cookie.Name].Domain = cookie.Domain;
                }
                else _cookies.Add(cookie.Name, new System.Net.Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain));
                //fire when complete
                IsReady = count == total - 1;   //
            }
            return true;
        }

        public void Dispose()
        {
            Constants._cookies = _cookies;
        }

        Dictionary<string, System.Net.Cookie> _cookies = new Dictionary<string, System.Net.Cookie>();
        public Dictionary<string, System.Net.Cookie> AllCookies { get { return _cookies; } }
        public bool IsReady { get; set; }
        public System.Net.Cookie this[string name]
        {
            get { return _cookies.ContainsKey(name) ? _cookies[name] : null; }
        }
        ~CookieVisitor()
        {
        }
    }

我检查了 _cookies 变量,但没有 access_token、refresh_token。

我想从 CefSharp 或任何其他 WebBrowser 获取 OAuth2 令牌。 请帮帮我。

【问题讨论】:

  • 我认为您应该遵循来自Microsoft Authentication flow 的任何身份验证流程,您想要 C# 示例吗?
  • 我真的很想要,因为我花了3天时间才解决这个问题。
  • 好的,我现在正在尝试使用示例 Graph Request 示例来回答 C# 身份验证示例。
  • 看看,你可以试试,希望对你有帮助。如果您有任何疑问,请随时分享。谢谢
  • 我认为您需要检查 CefSharp 的 javascript 代码。我不知道 Stex.com,但如果你可以登录 CefSharp,就会有一个会话。您需要通过 javascript 代码获取会话。希望你是幸运的。

标签: c#


【解决方案1】:

这是我用来获取初始令牌的代码

实际上不仅仅是代码

您需要一个网络浏览器。 “需要”网络浏览器部分不必编码。您只需打开网络浏览器并粘贴相关网址即可。

Private Async Function getAccessTokenFromCodeAsync() As Task(Of String)
    Dim openthisinbrowserwhenloggedin = "https://app.stex.com/oauth/authorize?response_type=code&client_id=" + _apiKey1 + "&redirect_uri=https%3A%2F%2Fapidocs.stex.com%2Foauth2-redirect.html&scope=trade profile&state=123"
    'openthisinbrowserwhenloggedin =https://app.stex.com/oauth/authorize?response_type=code&client_id=144&redirect_uri=https%3A%2F%2Fapidocs.stex.com%2Foauth2-redirect.html&scope=trade profile&state=123
    'it'll redirect to https://apidocs.stex.com/oauth2-redirect.html?code=def50200973e2b64d7569bc22dacbdc8b4e360d844380f71acc9ca239ff7d2d0eb5730378b27a6ab3f08eec50ab0e5b29f6bf886d6cfb44417548656036f2af2c6100d71b5c6d064ec6631d16318a2422209d0a46a7f311c8aa45f9af7226b03083a771b5a421fbbe03598d2b60e887ff9f563d6e4453bd377f6c0c709bb962a79f8d0f9999aa36310ef50593ccfc1d5d132016c46e5e274f3c5261de6d22fe9ed66d16c8eac7a55572d30b437b5cd42db5a1376a5fd8292b28bcd4e70984445222b859c791da9c8a8dc2325561a2301b7417d231b4c75a7266bdf56496f8bcfa62796a350d35e33c0ec938fb1464eb036ae1874507a2ec20887b0b900e53ecbd6416e5680d0870066cef57d2ba1925d7da3352d6102e52da2d4c2822412694a4fb8017d6da464095d7a968e6822bc468f8dae62c8b8a9c9c47644ce6c22f179cdf55a607131e5300919136fe0a088d6cfe7d8772a9b685880d4fcf2decb4e302294c6684d52ce85f0b587b49e72a355ded84e92e134fd052ae5037ed0d775727e5d5738341efe&state=123
    Dim code = "def50200b01c29bc92530a41695edd94747c90bc880817920368b752e8b543723bd44b02903f84122ef98ce677cd23f6160026d4e2d86efebeab49fc250a9df3f24e93b61602fada54133ac336be1c72aad025e4de78d295665d256ed6d2104e8c6c47a7e42a0053d74f5639ec5759ca57153af184c625df56936091e1b78a7dd8ad8cb30fc5d05da689484e9ee53e36e857e73eccd96ed34663c826eec13ed6486fe422f3ed78c3c1bdac1c475e88dd28577abc01ba012f5332435506713ea821022b986f48b66c2ce96cbbb1820cc58f88613154e0099163ec1e5cbff170991f61599c2352f6235945e8d057845a3868793fecec11ee5c6eb42511a926ff489efa8be541e979fe1401391853f91e1d85ec86b5fca2522ec2661f562f603be387499a0070b557410e6a1fc838f91a5fffd749292b10df2020d9b1ed0d67f4f1990e495a852a04c6354e2a55ec0ec09ef12706e7e94027f078dc15d0b7a7bc6d57e91e18c04e2b8cf27935c54bef8d7426cdc3b7ae7e493b5cde28e23187c97c8b55c5c5024dec"
    'Dim redirectencoded = URLEncode("https://apidocs.stex.com/oauth2-redirect.html")
    Dim postGetAuth = "grant_type=authorization_code&code=" + code + "&client_id=" + _apiKey1 + "&client_secret=" + _secret1 + "&redirect_uri=https://apidocs.stex.com/oauth2-redirect.html"
    'postGetAuth=grant_type=authorization_code&code=def502007dd66506c7d67b32d5795f5588dccd6105b9894ccb425307f3263855330f103c64f4ca3f70f568d262b32cc8db60e2ff9da3419d9fab306b1d773a9d59e7ccffe639f6881e84b543a0be52902dfe0a90c5225a400a89d65487a6adaf92e3a88afa5aebb20e7e14f8ba0024fe2113b776a659cd57b0e748459d5c5b1c2b22c8a1f7d0b32032141468e5d12397b96809d3f0de1294d4722116912572fcda76509170bb275a59233103eb0d592e0f2fd1deb84c465ef654acf81e6280f45b245532c32b7fbc4440349a33f527ec43a10432a79445d8f1b8b5f4378ad8165ebeae7a01d03b08727dd2725c33208d68ba7f529fee060db97bb01ef0bc805913ee7b213230eb3c31680f547010b9b647deaf768feeefba8c35f3a92e174cba524959b43166583f6287e087daf7b6358e7c235a2d1e5c48d7017321845e3aee229db80080a03639c928618cc62dd895bc163d8822dcb25b8931930271ec16d3b1c3cd847557cf7c438a71c203985173aed2fc59d5905d75fc0df4bbee562ccfbee8aa77a35925&client_id=144&client_secret=lcUPy7ANJ0rkqkvt25JQdJoL3w4hYsyX3SWP97jL&redirect_uri=https://apidocs.stex.com/oauth2-redirect.html
    Dim token2 = Await CookieAwareWebClient.downloadString1Async("https://api3.stex.com/oauth/token", postGetAuth)
    Return token2
End Function

所以基本上你看这个

openthisinbrowserwhenloggedin =https://app.stex.com/oauth/authorize?response_type=code&client_id=144&redirect_uri=https%3A%2F%2Fapidocs.stex.com%2Foauth2-redirect.html&scope=trade profile&state=123

你去https://app.stex.com/oauth/authorize?response_type=code&client_id=144&redirect_uri=https%3A%2F%2Fapidocs.stex.com%2Foauth2-redirect.html&scope=tradeprofile&state=123

您将重定向。然后你得到了代码。

现在我在这里使用调试模式。我把代码放进去

dim code="I pasted their code here

剩下的就跟在后面

【讨论】:

  • Op 明确要求提供 C# 示例。这不是相关的答案。
  • 不完美。一个真正的程序必须能够在没有调试信息的情况下做到这一点。我想你可以有一个包含代码的文本文件。您的程序读取了代码并且可以正常工作。如果代码不起作用,就崩溃。
  • 你需要用你的号码和资料来改变客户号码
  • @MdFaridUddinKiron 我正在使用 vb.net,它很容易转换。我也认为这是他能得到的最好的答案。我花了一个月的时间试图弄清楚这一点。很少有人自动化 stex
  • @shildra 无论如何我们可以在现实生活中聊天吗?看起来我们都是量化专家
猜你喜欢
  • 2023-04-05
  • 2021-11-10
  • 1970-01-01
  • 2015-07-27
  • 2015-02-11
  • 2013-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多