Good evening) I am writing a small application using the VK SDK. The server's secure. * API methods are needed (for example, secure.sendNotification). To call them, you need to get another token using a special scheme that is poorly described in the documentation.

'https://oauth.vk.com/access_token?client_id=' + CLIENT_ID + '&client_secret=' + CLIENT_SECRET + '&v=5.50&grant_type=client_credentials' 

where I insert my client_id and client_secret. I just don’t know how to call this request and get the token back to use it in secure methods. *.

Maybe someone knows how to do this? It is possible an example both on java and on C #.

  • This is a one-time action. It does not make sense to automate it. Open the browser, insert the link, click "confirm" - access_token appears on the screen - insert it into the code / config of your application, and that's it. - Sergey Rufanov
  • So I need the application user to get this token and use them. I think it will be inconvenient for him to do some strange things with a browser and a token - strevg
  • They should not be provided to the client in any way. Otherwise, there is a possibility that it is not so sick of getting into the money debt to VK, or rather quickly snatching the application's bank, or even a developer account. They need to be stored on your secure server, which will process user requests in accordance with the logic you need. - Sergey Rufanov
  • Still, I don’t understand how to use these secure methods. The documentation says you need to pass such authentication and send the received token in the secure request. And examples - "ZERO" - strevg
  • @strevg, if one of the answers offered to you solves the stated problem, then you can mark it as “correct” - Yuriy SPb

1 answer 1

Here is a working Java example using either OkHttp :

 Thread thread = new Thread() { @Override public void run() { try { OkHttpClient client = new OkHttpClient.Builder().build(); Request.Builder request = new Request.Builder(); String CLIENT_ID = "Цифры тут из настроек приложения в ВК"; String CLIENT_SECRET = "Секретный код из настроек приложения в ВК"; request.url("https://oauth.vk.com/access_token?client_id=" + CLIENT_ID + "&client_secret=" + CLIENT_SECRET + "&v=5.50&grant_type=client_credentials"); Response response = client.newCall(request.build()).execute(); String answer = response.body().string(); Log.e("LOG", answer); } catch (IOException e) { e.printStackTrace(); } } }; thread.start(); 

This will output:

{"access_token": "sdfsdfsdfsdfsdf8sdfsdfsdfsdfsdfsdfsdf", "expires_in": 0}

So you just need to read the answer of the VC to your request. Here are some more examples of Java code for extracting information from a response received via a URL: tyk

Actually, you can see the same line-answer of the VC and just having driven the resulting address into the browser.

  • I haven't sang yet to check - strevg