I
I
ideas4ru2012-10-10 11:07:32
Java
ideas4ru, 2012-10-10 11:07:32

Vkontakte authorization in java?

Hello everyone,
I am developing an application in java and I would like to fasten authorization through VKontakte.
Is there anyone among the readers who has already implemented something similar and can share the code?
Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
wrmax, 2012-10-10
@wrmax

Check out this VKontakte Android SDK article . There are links to source codes with authorization.

B
bracadabra, 2012-10-10
@bracadabra

If you need authorization without a WebView, then it looks something like this:

  1. //Get the required cookies
  2. HttpGet get  =  new  HttpGet ( http : //vk.com/login.php?email=%s&pass=%s);
  3. HttpResponse response  =  mHttpClient. execute ( get ) ;
  4. if  ( response. getStatusLine ( ) . getStatusCode ( )  ! =  200 )  {
  5. throw  new  UpdateException ( "Failed to obtain token" ) ;
  6. }
  7.  
  8. // Make a request for authorization
  9. get  =  new  HttpGet ( " api.vk.com/oauth/authorize?client_id=_&redirect_uri=http%3A%2F%2Fapi.vk.com%2Fblank.html&response_type=code&scope=audio ,offline" ) ;
  10. response  =  mHttpClient. execute ( get ) ;
  11. if  ( response. getStatusLine ( ) . getStatusCode ( )  ! =  200 )  {
  12. throw  new  UpdateException ( "Failed to obtain token" ) ;
  13. }
  14.  
  15. //Get the last link we were redirected to
  16. final String lastLink  =  redirectHandler. getLastLocationURI ( ) . toString ( ) ;
  17. if  ( lastLink. contains ( "oauth.vk.com/oauth/authorize" ) )  {
  18. throw  new  VkApiException ( "Failed to login!" ) ;
  19. }  else  if  ( lastLink. contains ( oauth. vk . com / authorize ) )  {
  20. //Get here if the app doesn't have verified permissions
  21. final String approveLink  =  getApproveLink ( EntityUtils
  22. . toString ( response. getEntity ( ) ) ) ;
  23. get  =  new  HttpGet ( approveLink ) ;
  24. response  =  mHttpClient. execute ( get ) ;
  25. if  ( response. getStatusLine ( ) . getStatusCode ( )  ! =  200 )  {
  26. throw  new  UpdateException ( "Failed to obtain token" ) ;
  27. }
  28. }
  29.  
  30. //If everything is fine, then we get the code from the last url
  31. final String code  =  extractCode ( redirectHandler. getLastLocationURI ( ) . toString ( ) ) ;
  32.  
  33. //Get the token using the scribe library
  34. final Verifier verifier  =  new  Verifier ( code ) ;
  35. final Token token  =   mAuthSevice. getAccessToken ( EMPTY_TOKEN, verifier ) ;

PS It used to work, but it seems that something has changed with them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question