N
N
Nester Shufrich2016-07-26 23:03:07
In contact with
Nester Shufrich, 2016-07-26 23:03:07

Getting Token in VK for api?

Hello everyone who responded to my question.
I want to get a token to use the API in VK. I use java, httpclient library
I send request according to VK documentation.

https://oauth.vk.com/authorize?client_id=1&display=page&redirect_uri=http://example.com/callback&scope=friends&response_type=token&v=5.53

I get to the login page. Tell me how to correctly compose an authorization request in order to receive the token I need in response to work with the VK API ???
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vk_api;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.CookieHandler;
import java.net.CookieManager;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;

/**
 *
 * @author Main
 */
public class VK_Api {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        String client_id = "5493815";
        String display = "page";
        String redirect_uri = "https://oauth.vk.com/blank.html";
        String scope = "friends";
        String response_type = "token";
        String v = "5.53";

        String anchor_origin = "name=\"_origin\"";
        String start = "value=\"";
        String stop = "\"";
        String _origin = "";

        String anchor_ip_h = "name=\"ip_h\"";
        String ip_h = "";

        String anchor_lg_h = "name=\"lg_h\"";
        String lg_h = "";

        String anchor_to = "name=\"to\"";
        String to = "";

        HttpClient HttpClient = new DefaultHttpClient();
        HttpResponse response;

        HttpPost post = new HttpPost("https://oauth.vk.com/authorize?"
                + "client_id=" + client_id
                + "&display=" + display
                + "&redirect_uri=" + redirect_uri
                + "&scope=" + scope
                + "&response_type=" + response_type
                + "&v=" + v);
        post.addHeader("Accept-Language", "ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3");
        System.out.println(post);
        response = HttpClient.execute(post);
        System.out.println(response);

        BufferedReader rd = new BufferedReader(
                new InputStreamReader(response.getEntity().getContent()));

        String line = "";
        while ((line = rd.readLine()) != null) {
            boolean con1 = line.contains(anchor_origin);
            if (con1) {
                SubStr substr = new SubStr(start, stop, line);
                _origin = substr.result_substr;

            }
            boolean con2 = line.contains(anchor_ip_h);
            if (con2) {
                SubStr substr = new SubStr(start, stop, line);
                ip_h = substr.result_substr;

            }
            boolean con3 = line.contains(anchor_lg_h);
            if (con3) {
                SubStr substr = new SubStr(start, stop, line);
                lg_h = substr.result_substr;

            }
            boolean con4 = line.contains(anchor_to);
            if (con4) {
                SubStr substr = new SubStr(start, stop, line);
                to = substr.result_substr;

            }
            System.out.println(line);
        }
        rd.close();
        System.out.println(_origin);
        System.out.println(ip_h);
        System.out.println(lg_h);
        System.out.println(to);

        String email = ""; // !!!!! Тут мыло от вк
        String expire = "0";
        String pass = ""; // !!!!! Tут пароль от вк

        post = new HttpPost("https://login.vk.com/?act=login&soft=1&utf8=1"
                + "&_origin=" + _origin
                + "&email=" + email
                + "&expire=" + expire
                + "&ip_h=" + ip_h
                + "&lg_h=" + lg_h
                + "&pass=" + pass
                + "&to=" + to);
//        post.setHeader("_origin", _origin);
//        post.setHeader("email", email);
//        post.setHeader("expire", expire);
//        post.setHeader("ip_h", ip_h);
//        post.setHeader("lg_h", lg_h);
//        post.setHeader("pass", pass);
//        post.setHeader("to", to);

        System.out.println(post);
        response = HttpClient.execute(post);
        System.out.println(response);

        BufferedReader rd_body = new BufferedReader(
                new InputStreamReader(response.getEntity().getContent()));

        String body = "";
        while ((body = rd_body.readLine()) != null) {
            System.out.println(body);
        }
        rd_body.close();
    }

    private static class SubStr {

        int start = 0;
        int stop = 0;

        String result_substr;

        SubStr(String start_str, String stop_str, String line) {
            //System.out.println (line);
            start = line.indexOf(start_str) + start_str.length();
            stop = line.indexOf(stop_str, start);
            result_substr = line.substring(start, stop);
            return;

        }

        SubStr() {

        }

    }
}

I will be grateful for any information!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rou1997, 2016-07-26
@Rou1997

You don’t need to do it through requests, do it through WebView, but rather use the VK SDK if Android.

N
Nester Shufrich, 2016-07-26
@Nester21

Not an android VK SDK will not work. Vopsche really make authorization in VK through httpclient ????

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question