Y
Y
Yaroslav Dusanyuk2016-11-23 14:16:04
.NET
Yaroslav Dusanyuk, 2016-11-23 14:16:04

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

1 answer(s)
A
Alexey Nemiro, 2016-11-23
@ydusanyuk

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);

The authorizeUrl address can be opened in a WebBrowser : webBrowser1.Navigate(authorizeUrl) . In the DocumentCompleted event handler, you can receive an authorization code or an access token (depending on which response_type is used). Using the default handler
example from my library:
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 - точно уже не помню
  }
}

But in general, things can be a little more complicated. You need to consider the IE version and ideally use the latest one (see SetIEVersion ). The probability of occurrence of errors in the client code and an adequate response to them. I don't remember all the pitfalls now.
If response_type=code , as in the above code for obtaining the authorization address, then you can convert the authorization code into an access token by making a request (most likely POST ) to the page https://oauth.vk.com/access_token , to which you need to pass the following parameters:
code=полученный код
client_id=идентификатор приложения
client_secret=секретный ключ
grant_type=authorization_code

Access token request should be done without WebBrowser , but for example with WebClient or HttpClient .
If everything is correct, the server will return an access_token .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question