Answer the question
In order to leave comments, you need to log in
How to run a browser dialog with a dll and get the result?
Hello.
I need to get access token for vk.com via OAuth. How can you launch the default browser from the class library with the desired page ( https://oauth.vk.com/authorize...) and get the token from it?
Answer the question
In order to leave comments, you need to log in
Too general a question :-)
Manipulating a live browser is a bad idea, it's unreliable and you can stumble on all sorts of antiviruses. At the application level, it is better to use the WebBrowser component (in fact - Internet Explorer ) or an alternative (it will be at least 20-40 megabytes to the size of the program).
A long time ago I wrote an article on working with VKontakte , I don’t know how relevant and whether it works now:
Development of a desktop application for VKontakte in C#
There is also an open source library:
https://github.com/alekseynemiro/nemiro.oauth .dll
Try to pull out the client for VKontakte(including dependencies) or implement it entirely if you don't like the presence of an extra library in the project (even two, if you use Windows Forms ) :-)
Ready-made forms for Windows Forms :
https://github.com/alekseynemiro/Nemiro.OAuth.Logi. ..
An article on using the library: OAuth authorization in .NET projects Fra... ASP.NET MVC
demo : demo-oauth.nemiro.net
Obtaining an address for authorization is done like this:
string applicationId = "идентификатор вашего приложения";
string scope = "status,email";
string authorizeUrl = "https://oauth.vk.com/authorize";
authorizeUrl += String.Format("?client_id={0}&response_type=code", applicationId);
// authorizeUrl += String.Format("&state={0}", "все что вам нужно");
authorizeUrl += String.Format("&scope={0}", scope);
protected internal void DefaultCallback(object sender, WebBrowserCallbackEventArgs e)
{
// ожидаем, когда будет получен результат
if (e.Url.Query.IndexOf("code=") != -1 || e.Url.Query.IndexOf("oauth_verifier=") != -1)
{
// результат получен, извлекаем код авторизации
// из строка параметров запроса
// e.Url.Query // <= строка параметров запроса
// либо oauth_verifier, либо code - точно уже не помню
}
}
code=полученный код
client_id=идентификатор приложения
client_secret=секретный ключ
grant_type=authorization_code
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question