感谢您阅读我的问题。 我正在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变量,但没有访问令牌,刷新令牌。
我想从CefSharp或任何其他WebBrowser获取OAuth2令牌。 请帮助我。
答案 0 :(得分:1)
这是我用来获取初始令牌的代码
实际上不只是代码
您需要一个网络浏览器。 “需要” Web浏览器部分不必进行编码。您只需打开网络浏览器并粘贴相关的URL。
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=trade profile&state = 123
您将重定向。然后您得到了代码。
现在我在这里使用调试模式。我将代码放入
dim code="I pasted their code here
其余的紧随其后