Access token through the VKontakte application. How to get access_token for Vkontakte application. ✅ Obtaining a token through the official VK app

Developers quite often have to deal with applications and services that, in the process of interacting with the Vkontakte social network, require an access key - access_token.

In this tutorial, we'll go over two very similar methods to get it.

How will the process go

All we need to do is substitute the application id in the URL. It is also available to edit the sections to which we allow access through the created access_token.

In fact, we will form a ready-made url, and substitute the id of two different applications. This will be the difference. After that, we will go to the ready-made link and receive an access key.

Link to get the key

Here is the url ready. Here we are primarily interested in the data after the \u003d sign. In the example below, it says Application-ID. We, in turn, will substitute a specific number there.

Https://oauth.vk.com/authorize?client_id\u003dApplicationID&scope\u003dnotify,photos,friends,audio,video,notes,pages,docs,status,questions,offers,wall,groups,messages,notifications,stats, ads, offline & redirect_uri \u003d https: //api.vk.com/blank.html&display\u003dpage&response_type\u003dtoken

Also pay attention to the data after the word "scope". Here we list the sections to which the key will open access. In order to prevent applications from accessing the section with our friends, remove the text "friends" from the link. The rest is by analogy.

Now all that remains is to get an ID.

We get ID through our own application

To create it, we need to go to the appropriate section available in the developer's dashboard. Follow the link there.

Https://vk.com/apps?act\u003dmanage

And click on the button "Create application".

Indicate the name and type of application (see). Then click on the button "Connect application".

Recently, all operations in the developer's office must be confirmed via SMS. In the window that opens, select "Confirm via SMS"... Get the code for your mobile phone, then enter it into the form.

The application will be created. Now go to the "Settings" tab. Here in the block, the set of numbers we need will be indicated. Copy it.

We now have everything we need.

We use the ID of the official Vkontakte application

The second way to get an ID specialist is simply to take a ready-made application. And best of all - official app Vkontakte for Android. His ID is "2890984". You can use.

We get Access_token

Https://oauth.vk.com/authorize?client_id\u003d2890984&scope\u003dnotify,photos,friends,audio,video,notes,pages,docs,status,questions,offers,wall,groups,messages,notifications,stats,ads, offline & redirect_uri \u003d https: //api.vk.com/blank.html&display\u003dpage&response_type\u003dtoken

Social networks have become an indispensable tool for website promotion. For website promotion via social. network, you should create a group or page for this site. Recruit subscribers, post news on the wall and much more (this is a topic for a separate article). Many of these processes can be automated using the API (Application Programming Interface) appropriate social network... In this post I will analyze how to connect to the vk api (vkontakte), how to start working with vk api, and also give some examples of working with the vk api.

Creating a VKontakte application

And so let's get started. Vk api has many methods, but one of their main differences is that in order to make requests to Vk api through some methods, a special access key is required - a token (access_token). You can get it by creating your own application. We are offered several types of applications, but I choose the Standalone type. It's enough for me. To start creating an application, follow the link and get into the next window.


Here we select the type and name of our application. We press connect the application and receive an SMS with a code to the phone number associated with the VK account from which we create the application. We enter it and go to the next window. In this window, go to the settings tab.


In the settings tab, we see fields with the name of the application ID and the security key. We write this data somewhere. I didn't do anything else in the tabs. Application state left in disabled position. We press to save the settings. That's it, we have created a VKontakte application.

Getting a token (access_token) api vk

Https://oauth.vk.com/authorize?client_id\u003d & display \u003d [window view]& redirect_uri \u003d https: //oauth.vk.com/blank.html&scope\u003d [application rights]& response_type \u003d token & v \u003d 5.52

  • client_id - ID of our application obtained earlier.
  • display - view of the window in which authorization will take place. Can be page, popup, touch and wap
  • scope - the access rights of our application regarding user data. More on rights below.

The rights of the VK application relative to a given user can be set in text and digital form. In text it will look like scope \u003d friends, messages, groups. With this line of code, we allowed the vk application to access the user's friends, messages and groups. The rights are also assigned in digital form. For each rule there is a bit mask and the sum of these masks and will allow the application to do certain things. For example, the right friends (+2), messages (+4096), groups (+262144), as a result, the sum of bit masks will be 266242 and the code scope \u003d 266242 will be analogous to scope \u003d friends, messages, groups. I would like to pay special attention to the law offline. Setting this right makes the token we receive infinite. If this right is not asked through certain time the token will need to be received again. You can read more about the rights of the VK application here. As a result, let's create an address for receiving a token for an application with access rights to friends, messages and user groups, as well as with an immortal token. Let the application id be 123456. This address will look like this:

Https://oauth.vk.com/authorize?client_id\u003d123456&display\u003dpage&redirect_uri\u003dhttps://oauth.vk.com/blank.html& scope \u003d friends, messages, groups, offline & response_type \u003d token & v \u003d 5.52


We confirm the action and get to the warning page, take our received token from the address bar of the browser. This will be after # access_token \u003d, the expires_in \u003d 0 code tells us that the (access_token) api vk is immortal. Accordingly, user_id \u003d is the id of the user for whom we got the token.

Let's go to the account settings in the application settings tab and see our application.


Now we are ready to work with api vkontakte.

Examples of working with api vk

To work with the VK api, I use the language php programming... Therefore, I will give examples in php. In order to perform any action, we need to send a request to the api vk. We can send a request through address bar browser. That is, a request is a specific url address with specific parameters specified in it. The syntax for composing such url is described below.

Https://api.vkontakte.ru/method / [CALLED METHOD]? [CALLED METHOD PARAMETERS]

The list of api vk methods is here. We will not focus on this any longer, then on real examples everything will become clear. It remains only to figure out how to send a request to the VKontakte api php... For this, we will use the php function file_get_contents, as well as the json_decode function, since we will receive the response from the server in json format. Here is a template for executing a request to VK in php.

$ result \u003d json_decode (file_get_contents ("https://api.vkontakte.ru/ method / [CALLED METHOD]? [PARAMETERS OF CALLED METHOD]"));

well, now a few examples of working with api vk

$ userid \u003d 12345; $ mytoken \u003d 56789; / * will return an object with the gender and birthday of the user * / $ request_params \u003d array ("user_id" \u003d\u003e $ userid, "fields" \u003d\u003e "sex", "fields" \u003d\u003e "bdate"); $ get_params \u003d http_build_query ($ request_params); $ result \u003d json_decode (file_get_contents ("https://api.vk.com/method/users.get?". $ get_params)); / * checking the user for a VK ban * / $ request_params \u003d array ("user_id" \u003d\u003e $ user_id, "fields" \u003d\u003e "deactivated"); $ get_params \u003d http_build_query ($ request_params); $ result \u003d json_decode (file_get_contents ("https://api.vk.com/method/users.get?". $ get_params)); if (isset ($ result-\u003e response-\u003e deactivated)) (echo "The page is frozen or deleted";) / * send a message to the user with user_id \u003d 222222 * / $ mesage \u003d "Hello, how are you?"; // encode the string $ msg \u003d urlencode ($ mesage); $ result \u003d json_decode (file_get_contents ("https://api.vkontakte.ru/method/ messages.send? user_id \u003d 222222 & message \u003d". $ msg. "& access_token \u003d". $ mytoken)); / * invite a user with user_id \u003d 222222 to be friends * / $ mesage \u003d "Let's be friends"; $ msg \u003d urlencode ($ mesage); $ result \u003d json_decode (file_get_contents ("https://api.vkontakte.ru/ method / friends.adduser_id \u003d 222222 & text \u003d". $ msg. "& access_token \u003d". $ mytoken));

Api has a lot of methods, so I advise you to read the documentation. Of course, queries do not always run correctly and return error codes. Errors can be viewed here. So, just like that, you can simply connect to the vk api and automate your actions in contact.

I am trying to get a token. I read the documentation, didn't understand anything. What to do?

Tokens are API access keys. They are used for authorization for API requests.

Tokens allow you to restrict access to user data using the scope parameter. It defines the scope for your application. The token with scope \u003d friends gives access to friends and nothing else. Thus, the token becomes less dangerous than the login and password.

How to get the

The token can be obtained directly from the browser. To do this, you just need to follow the correct link. How to compose the correct link:

1. Create a Standalone application.

  • redirect_uri is not necessary because You don't need a website, the application is client-side.
  • leave response_type and display as shown in the example.
  • client_id you got in the second step.
  • v take with aPI version pages ... Choose the freshest one.

scope, choose depending on the methods you want to use. For example, to access the method