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

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

Tokens allow you to limit access to user data using the scope parameter. It defines the visibility scope for your application. A token with scope=friends gives access to friends and nothing else. Thus, the token becomes less dangerous than a login and password.

How to get

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

1. Create a Standalone application.

  • redirect_uri does not need to be specified because You don’t need a website, it’s a client application.
  • Leave response_type and display as in the example.
  • you received client_id in the second step.
  • v take from API version pages. Choose the most recent one.

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

Social networks have become an integral tool for website promotion. To promote a website through social media. network should create a group or page for this site. Gain subscribers, post news on the wall and much more (this is a topic for a separate note). Many of these processes can be automated using the API (application programming interface) of the corresponding social network. In this article I will explain how to connect to the VK api (VKontakte), how to start working with the VK api, and also give several examples of working with the VKontakte api.

Creating a VKontakte application

And so let's get started. Vk api has many methods, but one of their main differences is that 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. I have enough of it. To start creating the application, follow the link and find yourself in the next window.


Here we select the type and name of our application. We click connect the application and receive an SMS with a code to the phone number linked to the VK account from which we are creating the application. Enter it and go to the next window. In this window, go to the settings tab.


In the settings tab we see fields named application ID and security key. We record this data somewhere. I didn't do anything else in the tabs. The application status was left in the disabled position. Click save settings. That's it, we created the VKontakte application.

Receiving a token (access_token) VK api

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

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

VK application rights relative to a given user can be specified in text and digital form. In text it will look like this scope=friends,messages,groups . With this line of code we allowed the vk application access to the user's friends, messages and groups. Rights are also set in digital form. Each rule has a bit mask and the sum of these masks and will allow the application to perform certain actions. For example, the right is friends(+2), messages(+4096), groups(+262144), as a result, the sum of the bit masks will be 266242 and the code scope=266242 will be analogous to scope=friends,messages,groups . I would like to pay special attention to the right to offline. Setting this right makes the token we receive infinite. If this right is not set via certain time the token will need to be obtained again. You can read more about VK application rights here. As a result, let’s create an address for the application to receive a token with access rights to the user’s friends, messages and groups, as well as an immortal token. Let the application ID be 123456. This address will look like this:

https://oauth.vk.com/authorize?client_id=123456&display=page&redirect_uri=https://oauth.vk.com/blank.html& scope=friends,messages,groups,offline&response_type=token&v=5.52


We confirm the action and get to a page with a warning; we take our received token from the address bar of the browser. This will be after #access_token= , the code expires_in=0 tells us that the token (access_token) VK api is immortal. Accordingly, user_id= is the id of the user for which we received the token.

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


Now we are all ready to work with the VKontakte API.

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 api vk. We can also send your request via address bar browser. That is, a request is a specific URL with certain parameters specified in it. The syntax for creating such a URL is described below.

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

The list of api vk methods is here. We won’t focus more on this for now, then with real examples everything will become clear. All that remains is to figure out how to send a request to the VKontakte API using PHP. For this we will use 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=json_decode(file_get_contents("https://api.vkontakte.ru/ method/[CALLED METHOD]?[CALLED METHOD PARAMETERS]"));

Well, now some examples of working with the VK API

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

The API has many methods, so I advise you to read the documentation. Of course, queries are not always executed 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.

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

In this tutorial we will look at two very similar methods to get it.

How will the process work?

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

Essentially, we will create a ready-made URL and substitute the id of two different applications. This will make the difference. After this, we will follow the prepared link and receive an access key.

Link to get the key

Here is the finished url address. Here we are primarily interested in the data after the = 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=ID-applications&scope=notify,photos,friends,audio,video,notes,pages,docs,status,questions,offers,wall,groups,messages,notifications,stats, ads,offline&redirect_uri=https://api.vk.com/blank.html&display=page&response_type=token

Also pay attention to the data after the word "scope". Here we list the sections to which the key will give 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 the ID.

We get ID through our own application

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

https://vk.com/apps?act=manage

And press the button "Create an application".

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

Recently, all operations in the developer account must be confirmed via SMS. In the window that opens, select “Confirm via SMS”. Receive the code on 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.

Now we have everything we need.

We use the ID of the official VKontakte application

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

We get Access_token

Https://oauth.vk.com/authorize?client_id=2890984&scope=notify,photos,friends,audio,video,notes,pages,docs,status,questions,offers,wall,groups,messages,notifications,stats,ads, offline&redirect_uri=https://api.vk.com/blank.html&display=page&response_type=token