📜 ⬆️ ⬇️

Authorization Vkontakte in your applications without a browser component

Hello! I am developing Vkontakte player music for phones.
It is clear that you need to use the recently published API for desktop and mobile applications. Everything is quite logical, but there is one thing - authorization is done only through the browser component, namely, we need the user to show the html code where he will enter his data.

That is, a simple api method in which we send the username and password, but we get the session just not!
And what to do on devices without such a component?

If we can somehow render html, then I’m not talking about javascript on j2me ...
This is where Firefox + Firebug + Firecookie comes to the rescue.
Having suffered, in one day I figured out what to send and where to get a session to use the API.
It turned out 3 requests, in each we receive the necessary data, and at the end of the session in JSON format.
Next, I post the clear code for the j2me platform. Those who wish will easily translate it into other languages ​​and platforms.
The code is still in beta, for a general understanding of the implementation, I plan to make a convenient class for working with all the functions of the API Vkontakte.
It uses meapplicationdevelopers.dev.java.net/mobileajax.html , but the one I have just supported for html support.
')
upload.com.ua/link/901600681

It works like this - VkApi.instance.login ("login", "pass");
After that we have the opportunity to get session data - getSid (), getUserId, getSecret (), getExpire ()

Well, the generation and sending of requests is the topic of another article.
Good luck in programming!
  1. import com.sun.me.web.request.Arg ;
  2. import com.sun.me.web.request.Request ;
  3. import com.sun.me.web.request.Response ;
  4. import org.json.me.JSONObject ;
  5. / **
  6. *
  7. * @author andryk
  8. * /
  9. public class VkApi {
  10. public static VkApi instance = new VkApi ( ) ;
  11. public static final String URL = "api.vkontakte.ru/api.php" ;
  12. // Fill in your application
  13. public static final String APP_ID = "" ;
  14. / *
  15. Application hash.
  16. Go to the login page of the browser and look for the variable 'var auth_hash' in the code
  17. This hash is needed for authorization.
  18. * /
  19. private static final String APP_HASH = "" ;
  20. private static final Arg FORM_HEADER = new Arg ( "Content-Type" , "application / x-www-form-urlencoded" ) ;
  21. protected String userId ;
  22. protected String sid ;
  23. protected String secret ;
  24. protected String expire ;
  25. protected boolean isLogged = false ;
  26. protected boolean needCaptcha = false ;
  27. protected String captchaSid = "" , captchaKey = "" ;
  28. public PopupBox captcha = null ;
  29. protected VkApi ( ) {
  30. }
  31. protected String findS ( String source ) throws Exception {
  32. System . out . println ( source ) ;
  33. String pattern = "id = 's' value ='" ;
  34. int start = source. indexOf ( pattern ) ;
  35. String s = source. substring ( start + pattern. length ( ) , start + pattern. length ( ) + 56 ) ;
  36. if ( s. length ( ) ! = 56 ) {
  37. throw new Exception ( "s not finded in form" ) ;
  38. }
  39. return s ;
  40. }
  41. public boolean isLogged ( ) {
  42. return isLogged ;
  43. }
  44. public boolean isNeedCaptcha ( ) {
  45. return needCaptcha ;
  46. }
  47. public String getSecret ( ) {
  48. return secret ;
  49. }
  50. public String getUserId ( ) {
  51. return userId ;
  52. }
  53. public String getSid ( ) {
  54. return sid ;
  55. }
  56. class NeedCaptchaException extends Exception {
  57. String sid ;
  58. public NeedCaptchaException ( String s ) {
  59. sid = s ;
  60. }
  61. public String getUrl ( ) {
  62. return "api.vk.com/captcha.php?sid=" + sid + "& s = 1" ;
  63. }
  64. public String getSid ( ) {
  65. return sid ;
  66. }
  67. }
  68. public void setCaptchaKey ( String captchaKey ) {
  69. this . captchaKey = captchaKey ;
  70. }
  71. public void login ( String login, String password ) {
  72. try {
  73. Response result = Request . post ( "login.vk.com/" , new Arg [ ] {
  74. new Arg ( "act" , "login" ) , new Arg ( "app" , APP_ID ) ,
  75. new Arg ( "app_hash" , APP_HASH ) , new Arg ( "captcha_key" , captchaKey ) ,
  76. new Arg ( "captcha_sid" , captchaSid ) ,
  77. new Arg ( "email" , login ) , new Arg ( "pass" , password ) ,
  78. new Arg ( "permanent" , "1" ) , new Arg ( "vk" , "" )
  79. } , new Arg [ ] { FORM_HEADER } , null , null , null ) ;
  80. // If the redirect code means something is wrong.
  81. if ( result. getCode ( ) == 302 ) {
  82. String sid = null ;
  83. int start = - 1 ;
  84. for ( int i = 0 ; i < result. getHeaders ( ) . length ; i ++ ) {
  85. if ( result. getHeaders ( ) [ i ] . getKey ( ) . toLowerCase ( ) . equals ( "location" ) ) {
  86. String l = result. getHeaders ( ) [ i ] . getValue ( ) ;
  87. start = l. indexOf ( "m = 1 & cs =" ) ;
  88. int end = l. indexOf ( "&" , start + 7 ) ;
  89. sid = l. substring ( start + 7 , end ) ;
  90. break ;
  91. }
  92. }
  93. // Or need captcha input
  94. if ( start > 0 0 )
  95. throw new NeedCaptchaException ( sid ) ;
  96. // Or incorrect login / password
  97. else
  98. throw new Exception ( "Wrong login / pass" ) ;
  99. }
  100. needCaptcha = false ;
  101. // Send a new request with a cookie from the previous one
  102. Response result2 = Request . get ( "login.vk.com/?vk=" , null , null , null , result. getCookies ( ) ) ;
  103. // Find SID
  104. //. getResult (). getRaw () - response body
  105. s = findS ( result2. getResult ( ) . getRaw ( ) ) ;
  106. // Last 3 query in which we will find the required data
  107. 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 ) } )) ;
  108. String r = result3. getResult ( ) . getRaw ( ) ;
  109. int start = r. indexOf ( "{ \" mid \ " " ) ;
  110. // Found our JSON object with session data
  111. String sess = r. substring ( start, r. indexOf ( "}" , start ) + 1 ) ;
  112. // Need to read it
  113. JSONObject session = new JSONObject ( sess ) ;
  114. // Remember the data
  115. userId = session. getString ( "mid" ) ;
  116. sid = session. getString ( "sid" ) ;
  117. secret = session. getString ( "secret" ) ;
  118. expire = session. getString ( "expire" ) ;
  119. isLogged = true ;
  120. } catch ( NeedCaptchaException e ) {
  121. // Display the captcha to the user, the address of the image is e.getUrl ()
  122. // After that set the code setCaptchaKey (String key)
  123. // And login again
  124. } catch ( Exception e ) {
  125. isLogged = false ;
  126. // Actions if the login / password is wrong
  127. }
  128. }
  129. }

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


All Articles