Using VK.com for our own purposes

By | 06.07.2017

To yong users VK gives unlimited opportunities of online communication. Mature employees of special services – voluntarily and personally created the dossier on the above group of citizens. And what can it give to the programmer? Of course, the free infrastructure for its application! In conditions when there is no server, and someone else offers only a test unstable key, which does not allow running the application in a large voyage, VK.com seems to be just the ideal option. And no headaches with server reliability and hosting payment ;).

Possibilities of native VK

VK API allows a lot. In groups, news is often published, up to ten attachments can be attached to one recording on the wall (photo, video, audio). We can make the application to read the news feeds. We can store a video here – count, a movie theater in your pocket. Place your MP3-collections, store photos or just documents.

The main thing is to properly disguise content from hunters for unlicensed content (encryption of files or their names). However, I’m sure that you are not a pirate and do not need such measures to protect your content;). If we sum, then with VK we can do:

  • News application;
  • Online cinema;
  • Online MP3 player;
  • Own photo server;
  • Killer Google Docs – MyVKDoc!

Theory VK API

Description of the VK API methods you can find here.

The methods that require authorization for our backend are not comfortable – users generally do not like it when they are asked about something else. In order to quickly pick up any information, the VK API has a method called wall.get. He returns a list of records from the wall of the user or community. The great thing is that this is an open method that does not require access_token.

That is, if the wall is open, anyone can read it. This is what you need for the client application!

Working with VK API via Android

C VK API can work both with the usual POST and GET requests, and through the android application, which we, as usual, will help the Android SDK. Using it, you can upload files to VK server and do publications on walls (method VKApi.wall(). post).

Extract from the official documentation: preparation for use

Before you start working with the VK SDK, you need to create a Standalone application on the application creation page. Save the ID of your application and fill in the “Package name for Android” field, “Main Activity for Android”, “Imprint certificate for Android”.

With the keys and the Standalone application everything is clear, there is only one nuance: the user, on whose behalf we will post the records on the wall, must have the necessary rights in this group. This is set up in the section “Community Management → Participants”. The application must also request permissions on the first start (for example, VKScope.WALL, VKScope.DOCS). They are specified in the method VKSdk.login().

If an official application from VK is installed on the device, a pop-up window will appear. Or if there is no application, then we will see a fragment that requires entering a login-password.

Now we can directly access the Android SDK methods without thinking about authorizations.

Then we are faced with the following obstacle: you can upload up to 50 posts per day to the wall of the group.

API errorVKError (code: 214; ; Access to adding post denied: you can only add 50 posts a day

In addition, if we will ship very quickly, the SDK will stop us with a captcha.

I got it out after 20 documents downloaded in a row. The algorithm for the appearance of captcha developers will not tell us either by friendship, for money, or under torture. Experienced, I found out that it would be safe enough to add a new record every 29 minutes. From such a colossal speed captcha should not, and in the limit of 50 downloads per day, we also meet.

News application

Usually when you publish news put text and pictures. To load pictures, we use the method VKApi.uploadWallPhotoRequest. After downloading the server will return us the data needed for the method of publication of the record on the wall VKApi.wall().post.

VKRequest post = VKApi.wall().post(VKParameters.from(VKApiConst.OWNER_ID, "-" + TARGET_GROUP, VKApiConst.ATTACHMENTS, attachments, VKApiConst.MESSAGE, message));

The attachments parameter is obtained in response to VKApi.uploadWallPhotoRequest.

VKRequest request = VKApi.docs().uploadWallDocRequest(new File(file_path), TARGET_GROUP);
request.attempts = 10;
Log.d("request", "uploadWallDocRequest file_path=" + file_path);
request.executeWithListener(new VKRequest.VKRequestListener() {
  @Override
  public void onComplete(VKResponse response) {
    sendProgress("file", "The file is uploaded to the server " + file_path);
    // sendProgress("file", "Server Response: " + response.json);
    Log.d("request", "onComplete response=" + response.json);
    VKApiDocument photoModel = ((VKDocsArray) response.parsedModel).get(0);
    new File(file_path).delete();
    sendProgress("file", "File deleted " + file_path);
    makePost(new VKAttachments(photoModel), null, s);
  }

  @Override
  public void onError(VKError error) {
    Log.d("request", "onError error=" + error);
    // Error. We inform the user about error
  }

  @Override
  public void attemptFailed(VKRequest request, int attemptNumber, int totalAttempts) {
    Log.d("request", "attemptFailed=" + request.toString());
    // Unsuccessful attempt. The arguments will show the number of the attempt and the total number of attempts
  }
});

The text of the news should be sent to the message line, its key is VKApiConst.MESSAGE.

One publication can have up to ten attachments – pictures, video, audio, documents.

The detected feature: if we load GIF-files as pictures, then a normal JPEG will be published on the wall without animation. Therefore, we need to load GIF as a document. Documents are available in groups and are not available in public pages. A public page can be quickly translated into a group.

Online cinema

For downloading, there is a method called VKApi.video().save(); it will also return the server address (required for download) and video data. Next, you need to perform a POST request for the received address. The video_file field must contain a video file in the format AVI, MP4, 3GP, MPEG, MOV, MP3, FLV or WMV. In response, the application receives the size of the downloaded file and the video id or error message in the JSON format:

{"size":1234,"video_id":1234567}
or
{"error":Error description}

After downloading the video is processed and in the list of video recordings may appear after some time.

Online MP3 Player

Here we can use the method VKApi.audio().getUploadServer() – with it we will get the server to load. Then, as with the video, you need to download the file with a POST request, the file field must contain an MP3 file. In response, the application receives server, audio and hash data as JSON:

{"server": "1234", "audio": "1234", "hash": "12345abcde"}

Using the VKApi.audio().save() method, the application passes the received data to the server (server, audio and hash) and receives data about the downloaded audio record.

Own photo server

You can download pictures without POST requests using the simple methods VKApi.uploadWallPhotoRequest and VKApi.uploadAlbumPhotoRequest. The names speak for themselves. For its own “Instagram” VKApi.uploadAlbumPhotoRequest is more preferable, since the photos should be placed on albums, rather than throw them on the wall in picturesque chaos.

VKRequest request = VKApi.uploadWallPhotoRequest(VKUploadImage image, int user_id, int group_id);
VKRequest request = VKApi.uploadAlbumPhotoRequest(VKUploadImage image, int album_id, int group_id);
VKRequest request = VKApi.uploadWallPhotoRequest(File image, int user_id, int group_id);
VKRequest request = VKApi.uploadAlbumPhotoRequest(File image, int album_id, int group_id);

When uploading a photo to your wall or wall of the group user_id == 0, otherwise – id of the user, on the wall of which we will upload a photo.

When uploading a photo to a wall or to an album group_id == id of the target group, otherwise group_id == 0 (for example, uploading photos to your album). In album_id, respectively, is the photo album id.

MyVKDoc

For loading documents, we need VKApi.docs().uploadDocRequest() or VKApi.docs().uploadWallDocRequest for the “wall” documentation. Well and further, as usual, we execute VKApi.wall().post with the parameters which have come from VKApi.docs().uploadWallDocRequest.

Conclusion

Apparently, VK API gives us a lot of space for creativity. A completely free data store – is not this what every mobile programmer dreams about? 🙂 In the next article on the example of the Android-client for the group, we will consider in detail how you can take data from an open wall.

Leave a Reply

Your email address will not be published. Required fields are marked *