- import com.sun.me.web.request.Arg ;
- import com.sun.me.web.request.Request ;
- import com.sun.me.web.request.Response ;
- import org.json.me.JSONObject ;
- / **
- *
- * @author andryk
- * /
- public class VkApi {
- public static VkApi instance = new VkApi ( ) ;
- public static final String URL = "api.vkontakte.ru/api.php" ;
- // Fill in your application
- public static final String APP_ID = "" ;
- / *
- Application hash.
- Go to the login page of the browser and look for the variable 'var auth_hash' in the code
- This hash is needed for authorization.
- * /
- private static final String APP_HASH = "" ;
- private static final Arg FORM_HEADER = new Arg ( "Content-Type" , "application / x-www-form-urlencoded" ) ;
- protected String userId ;
- protected String sid ;
- protected String secret ;
- protected String expire ;
- protected boolean isLogged = false ;
- protected boolean needCaptcha = false ;
- protected String captchaSid = "" , captchaKey = "" ;
- public PopupBox captcha = null ;
- protected VkApi ( ) {
- }
- protected String findS ( String source ) throws Exception {
- System . out . println ( source ) ;
- String pattern = "id = 's' value ='" ;
- int start = source. indexOf ( pattern ) ;
- String s = source. substring ( start + pattern. length ( ) , start + pattern. length ( ) + 56 ) ;
- if ( s. length ( ) ! = 56 ) {
- throw new Exception ( "s not finded in form" ) ;
- }
- return s ;
- }
- public boolean isLogged ( ) {
- return isLogged ;
- }
- public boolean isNeedCaptcha ( ) {
- return needCaptcha ;
- }
- public String getSecret ( ) {
- return secret ;
- }
- public String getUserId ( ) {
- return userId ;
- }
- public String getSid ( ) {
- return sid ;
- }
- class NeedCaptchaException extends Exception {
- String sid ;
- public NeedCaptchaException ( String s ) {
- sid = s ;
- }
- public String getUrl ( ) {
- return "api.vk.com/captcha.php?sid=" + sid + "& s = 1" ;
- }
- public String getSid ( ) {
- return sid ;
- }
- }
- public void setCaptchaKey ( String captchaKey ) {
- this . captchaKey = captchaKey ;
- }
- public void login ( String login, String password ) {
- try {
- Response result = Request . post ( "login.vk.com/" , new Arg [ ] {
- new Arg ( "act" , "login" ) , new Arg ( "app" , APP_ID ) ,
- new Arg ( "app_hash" , APP_HASH ) , new Arg ( "captcha_key" , captchaKey ) ,
- new Arg ( "captcha_sid" , captchaSid ) ,
- new Arg ( "email" , login ) , new Arg ( "pass" , password ) ,
- new Arg ( "permanent" , "1" ) , new Arg ( "vk" , "" )
- } , new Arg [ ] { FORM_HEADER } , null , null , null ) ;
- // If the redirect code means something is wrong.
- if ( result. getCode ( ) == 302 ) {
- String sid = null ;
- int start = - 1 ;
- for ( int i = 0 ; i < result. getHeaders ( ) . length ; i ++ ) {
- if ( result. getHeaders ( ) [ i ] . getKey ( ) . toLowerCase ( ) . equals ( "location" ) ) {
- String l = result. getHeaders ( ) [ i ] . getValue ( ) ;
- start = l. indexOf ( "m = 1 & cs =" ) ;
- int end = l. indexOf ( "&" , start + 7 ) ;
- sid = l. substring ( start + 7 , end ) ;
- break ;
- }
- }
- // Or need captcha input
- if ( start > 0 0 )
- throw new NeedCaptchaException ( sid ) ;
- // Or incorrect login / password
- else
- throw new Exception ( "Wrong login / pass" ) ;
- }
- needCaptcha = false ;
- // Send a new request with a cookie from the previous one
- Response result2 = Request . get ( "login.vk.com/?vk=" , null , null , null , result. getCookies ( ) ) ;
- // Find SID
- //. getResult (). getRaw () - response body
- s = findS ( result2. getResult ( ) . getRaw ( ) ) ;
- // Last 3 query in which we will find the required data
- Response result3 = Request . get ( "vkontakte.ru/login.php?app=" + APP_ID + "& layout = popup & type = browser & settings = 1054" , null , null , null , new Arg [ ] { new Arg ( "remixsid" , s ) } )) ;
- String r = result3. getResult ( ) . getRaw ( ) ;
- int start = r. indexOf ( "{ \" mid \ " " ) ;
- // Found our JSON object with session data
- String sess = r. substring ( start, r. indexOf ( "}" , start ) + 1 ) ;
- // Need to read it
- JSONObject session = new JSONObject ( sess ) ;
- // Remember the data
- userId = session. getString ( "mid" ) ;
- sid = session. getString ( "sid" ) ;
- secret = session. getString ( "secret" ) ;
- expire = session. getString ( "expire" ) ;
- isLogged = true ;
- } catch ( NeedCaptchaException e ) {
- // Display the captcha to the user, the address of the image is e.getUrl ()
- // After that set the code setCaptchaKey (String key)
- // And login again
- } catch ( Exception e ) {
- isLogged = false ;
- // Actions if the login / password is wrong
- }
- }
- }
Source: https://habr.com/ru/post/92693/
All Articles