public string reqToken(string Scope, WebBrowser webBrowserAuth)
{
using (var req = new HttpRequest())
{
req.UserAgent = Http.ChromeUserAgent();
CookieDictionary cookies = new CookieDictionary(false);
req.Cookies = cookies;
String Data = req.Get(String.Format("https://oauth.vk.com/authorize?client_id={0}&scope={1}&display=page&response_type=token&redirect_uri=https://oauth.vk.com/blank.html", appID, Scope)).ToString();
// Вот это действие нужно выполнить только после редиректа
Uri u = new Uri(req.Response.Address.ToString());
webBrowserAuth.Url = u;
char[] simbol = { '=', '&' };
string[] str = webBrowserAuth.Url.ToString().Split(simbol);
string token = str[1];
return token;
}
}
string vkUrl = webBrowser1.Url.AbsoluteUri;
if (vkUrl.Contains("access_token"))
{
var match = Regex.Match(vkUrl, @"=(.*?)&");
vkAT = match.Groups[1].ToString();
match = Regex.Match(vkUrl, @"&user_id=(.*)");
vkUserId = match.Groups[1].ToString();
string[] dataNames = {"UID", "AT"};
string[] data = {vkUserId, vkAT};
}
webBrowserAuth.Navigated += (sender, e) =>
{
if(e.Url.GetLeftPart(UriPartial.Query) == "https://oauth.vk.com/blank.html")
{
var url = new Uri(e.Url.AbsoluteUri.Replace('#', '?'));
var parameters = HttpUtility.ParseQueryString(url.Query);
var accessToken = parameters.Get("access_token");
var expiresIn = parameters.Get("expires_in");
var userId = parameters.Get("user_id");
}
};
public Vk Authorize()
{
var form = new Form { Width = 800, Height = 600 };
var vk = new Vk();
var thread = new Thread(() =>
{
var browser = new WebBrowser { Parent = form, Dock = DockStyle.Fill };
var authLink =
$"https://oauth.vk.com/authorize?client_id={_appId}&display=popup&redirect_uri=https://oauth.vk.com/blank.html&scope={_scopes}&response_type=token&v=5.37";
browser.Navigate(authLink);
browser.Navigated += (sender, e) =>
{
if (e.Url.GetLeftPart(UriPartial.Query) == "https://oauth.vk.com/blank.html")
{
var url = new Uri(e.Url.AbsoluteUri.Replace('#', '?'));
var parameters = HttpUtility.ParseQueryString(url.Query);
var accessToken = parameters.Get("access_token");
var expiresIn = parameters.Get("expires_in");
var userId = parameters.Get("user_id");
vk = new Vk(accessToken, expiresIn, userId);
form.Close();
}
};
form.ShowDialog();
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
return vk;
}