What is a VK token. How to get access_token VKontakte Getting access token VKontakte php

Recently, a huge number of online services, computer or mobile applications, scripts have appeared that are designed for VKontakte, but for their work you need to go through authorization through access_token.

Some services provide the ability to obtain an access key, which is required for authorization. This takes a few seconds. But what if you uploaded the script, but the required access_token key is missing?

Brief instructions for obtaining a community token

Go to community settings. (if you do not have a community, then it):

1. Working with API > 2. Get key > 3. Copy it (this is the key (token) and is the access_token of the community)

That's all. And if you need a user token, then all the necessary information is located below in the article.

Get a token through the application:

Where can I get access_token?

Today we will analyze how to log in to VKontakte using a direct link. API VKontakte (based on the protocol OAuth) or, as it is also called, implicit flow. Authorization, if you use this method, is done through the VK application, which is indicated as ID. Today, this method is considered the safest. You may come across articles that talk about methods for obtaining access_token using applications that cause (and for good reason!) suspicion. But today we will talk about how to log in through the official VKontakte applications.

APP_ID

If this is sorted out, the following question naturally pops up: where to get this link?

✅ Getting a token through your own application.

  • Using your own application. This method is obviously designed for the fact that you have an application of your own design. If you don't have one, now is the time to create it. It is very easy to do it: just follow the link vk.com/apps?act=manage and click on " Create Application».

The system will ask you to enter a name for the application. As an example, you can use " Getting an access_token". Check if the checkbox " Standalone application". Next, click on " Connect application».

To confirm the application, you will need to enter the code that you will receive on the specified phone number. At this stage of creating the application, you can fix the mobile device to the VK page. To do this, click on " Link device". You can do it without linking to an account. Then you just need to follow the link " Confirm via SMS».

After you confirm the registration, a page will open that contains information about the application that was created. Press on " Settings", which are located in the left corner. There is located client_id - Application ID VKontakte.

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

5563738 is the ID of the application you created. You will get a similar combination. After we have created the application, let's move on to the question itself.

Getting a token through the official VK application.

The method differs from the one described earlier, only in that you do not need to create your own application. Use what you already have. He can be 100% trusted.

The method will be considered using the example of VKontakte for Android. ID like this: 2890984 . It is this combination that must be substituted in the link.

You will get the following:

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=http://api.vk.com/blank. html&display=page&response_type=token

This concludes the part of the article in which we looked at the application identification options that can be used for authorization. There are only a few things left to touch on:

✅ Access rights:

  • In the examples described above, the parameter scope contains many names of sections of the social network VKontakte: audio, photos, notify, friends. These are the sections that will be open to the application. The access_token can be used in a variety of ways. The ID you are using belongs to a trusted application. That is why you can create an access_token that has all access rights. It becomes versatile so that it can be used everywhere.

✅ access_token:

The last question to be touched upon is that how to get the key itself access_token . After you receive the link (using one of the methods), you will need to follow it to open the access right.

After that, the necessary key will appear in your address bar. It is copied manually: after access_token= and before &expires_in.

Well, it’s worth finishing with a few tips:

  • Do not share the access_token key with third parties.
  • You should not go through authorization using applications that are not trustworthy. It is recommended to use only your own or official ones.
  • Delete the key after you have used it. You can always create a new one if needed.
  • All active sessions should be terminated when they are no longer needed. You can do this through your account security settings.

I'm trying to get a token. Read the documentation, understood nothing. What to do?

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

Tokens allow you to restrict access to user data using the scope parameter. It defines the scope for your application. A token with scope=friends gives access to friends and nothing else. Thus, the token becomes less dangerous than the username 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 make a correct link:

1. Create a standalone application.

  • redirect_uri does not need to be specified. You don't need a website, the application is a client one.
  • leave response_type and display as in the example.
  • client_id you got in the second step.
  • v take from API version pages. Choose the latest.

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

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

In this tutorial, we'll look at two very similar methods to get it.

How will the process go

All that is required of us is to substitute the id of the application 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 generate a ready-made url, and substitute the id of two different applications. This will make the difference. After that, we will follow the prepared link and receive an access key.

Link to get the key

Here is the completed url. 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=application-id&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 note 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 it remains only to get the ID.

We get an 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 push the button "Create Application".

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

Since recently, all operations in the developer account must be confirmed via SMS. In the window that opens, select the item "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 app for Android. His ID is "2890984". You can use.

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

Social networks have become an essential tool for website promotion. To promote the site through the social. network should create a group or page of 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 respective social network. In this note, I will analyze how to connect to vk api (vkontakte), how to start working with vk api, and also give some examples of working with api vkontakte.

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 application. We are offered several types of applications, but I choose the Standalone type. I have enough of it. 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 we receive an SMS with a code to the phone number linked to the VK account from which we create 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 with the name of the application ID and the security key. We write down this data somewhere. I didn't do anything else in the tabs. The application state is left in the disabled position. Click save settings. That's it, we have created a VKontakte application.

Getting a token (access_token) api VK

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

  • client_id - ID of our application received earlier.
  • display - type of window in which authorization will take place. Can be page, popup, touch and wap
  • scope - 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=friends,messages,groups . With this line of code, we have allowed the vk application to access the user's friends, messages, and groups. The same rights are set in digital form. For each rule there is a bitmask and the sum of these masks and will allow the application to perform certain actions. For example, the right friends(+2), messages(+4096), groups(+262144), as a result, the sum of bit masks will be 266242 and the scope=266242 code will be analogous to scope=friends,messages,groups . I want to pay special attention to the offline right. Setting this right makes the token we receive infinite. If this right is not set after a 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 compose 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=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 the page with a warning, we take our received token from the address bar of the browser. It will be after #access_token= , the expires_in=0 code tells us that the token (access_token) api vk is immortal. Accordingly, user_id= is the id of the user for whom we received the token.

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


Now we have everything ready to work with the VKontakte api.

Examples of working with api vk

To work with api VK, I use the php programming language. 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 a request through the address bar of the browser. That is, a request is a specific url address with certain parameters specified in it. The syntax for compiling 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 will not focus on this more for now, then everything will become clear with real examples. It remains only to figure out how to send a request to the VKontakte api using php. To do 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=json_decode(file_get_contents("https://api.vkontakte.ru/ method/[CALLED METHOD]?[CALLED METHOD PARAMETERS]"));

well, now a few examples of working with api vk

$useid=12345; $mytoken=56789; /*returns 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 if the user is banned from VK*/ $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 message to user with user_id=222222*/ $mesage="Hi, how are you?"; //encode 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)); /* 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 a lot of methods, so I advise you to read the documentation. Of course queries don't always run correctly and return error codes. Errors can be viewed here. So just like that, you can simply connect to api vk and automate your actions in contact.


Top