⬆️ ⬇️

Algorithm for the application to the API VKontakte

Often we want to automate a particular action. Sometimes a rush or just laziness does not give us the opportunity to get acquainted with all the documentation (according to API VK).

Developing a program to work with the VKontakte API is easier than it seems. Any language that is able to send http get requests (respectively, receive a response) will do. For example: Delphi, shell, C, Perl, PHP, Python, etc.

image



So, let's analyze the algorithm for creating a program and integrating it with VKontakte.

The first thing we need to do is create (register) a Desktop application.

Click on the button " Connect site " in the section "Developers"

Enter the name and select the type - Standalone-application.

image



After that, your mobile phone (tied to an account) will receive a code that must be entered in the test field.

The application is created, in the settings we need the application ID. image



Authorization of the application with VKontakte is based on the OAuth 2.0 protocol and consists of 3 steps:



')

_http://api.vk.com/oauth/authorize?client_id=##ID##&redirect_uri=http://api.vk.com/blank.html&scope=####&display=#### &response_type=token

where:

## ID ## - Id of your application received earlier;

## DISPLAY ## - the appearance of the authorization window (page, popup, touch and wap).

## RIGHT ## - list of application access rights to user data. Specify a comma (notify, friends, photos, audio, video, docs, notes, pages, wall, groups, messages, ads) or in digital form, summing up the necessary rights (1,2,4,8,16,32,64 , ..., 262144).

Example: Get the rights to Access to friends and Access to user photos:

2 + 4 = 6; scope = 1026 or scope = friends, photos

More information about access rights can be read here and here .

image



In response to the request, the server will return:



access_token=93f22a20ddf1174f939108d43e936cd6bb193469344639c2d1c268f3f65fa86 &expires_in=86400&user_id=11347652

where access_token is a parameter we need to send subsequent requests to the Vkontakte API;

expires_in - token lifetime, after expiration it is necessary to update

( UPD: if you specify “offline” or 999999 in the rights, then the token will be infinite);

user_id - user id.

Communication program-application-user installed. Now we can send any requests to Api (in measures allowed by the user).

Query syntax:



_https://api.vkontakte.ru/method/####?##_##=##_## &access_token=##,_##

The list of methods and their description can be found here and here .



For example, consider the method of sending a personal message.


Method Name: messages.send

Required right: 4096

Parameters: uid - the id of the user to whom the message is sent is mandatory.

message - the text of the message is mandatory if the attachment is not specified.

attachment - attachments to the message, separated by commas in the format

<><>_<id> (Example: photo100172_166443618).

title - Title.

type - 0 (default) - normal, 1 - from the chat.



An example of a request to send a message to the user id66392446:

_https://api.vkontakte.ru/method/messages.send?uid=66392446&message=&title=&access_token= 93f22a20ddf1174f939108d43e936cd6bb193469344639c2d1c268f3f65fa86

In response, we get the message ID or error code.

{"response":10847}



As an example of the program, I cite my php-script of automatic updating of Vkontakte status


(Until the new year, there are ... days + Random phrase from the file).

Create a php file that will send the status:

pub.php

<?php

$token='93f22a20ddf1174f939108d43e936cd6bb193469344639c2d1c268f3f65fa86';

// ,

$day = '15';

$month = '11';

//,

$year = '2011';

function dateDiff($startDay, $endDay) {

$endDay = strtotime($endDay);

$startDay= strtotime($startDay);



$difference = abs($endDay - $startDay);

$return['days'] = floor($difference / 86400);

$return['hours'] = floor($difference / 3600) % 24;

$return['minutes'] = floor($difference / 60) % 60;



return $return;

}



$date=dateDiff(date("jnY G:i"),'19-11-2011 9:00');



$phrases = "/var/www/public/1.txt";//

$z = file ($phrases);

$cz = count ($z) -1;

$r = rand (0, $cz);

//



$text=" $date[days] , $date[hours] .$z[$r]";



$sRequest = "https://api.vkontakte.ru/method/status.set?text=$text&access_token=$token";

echo $text;

//

$oResponce = json_decode(file_get_contents($sRequest));




Now we add pub.php to cron with the update, no more than once a minute, and see the result. image



image



In this way, you can automatically publish articles, photos, videos, send messages and many other interesting things.

Experiment! Thank you for your attention .



UPD: before sending a text string, you must encode $text=urlencode($text);

Source: https://habr.com/ru/post/131943/



All Articles