Привет всем, кто откликнулся на мой вопрос.
Хочу получить токен для использования API в VK. Использую java, библиотеку httpclient
Посылаю запрос согласно VK документации.
https://oauth.vk.com/authorize?client_id=1&display=page&redirect_uri=http://example.com/callback&scope=friends&response_type=token&v=5.53
Попадаю на страничку авторизации. Подскажите как правильно составить запрос авторизации чтобы в ответ получить нужный мне токен для работы с API VK ???
/*
* 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() {
}
}
}
За любую информацию буду благодарен!