Answer the question
In order to leave comments, you need to log in
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
Check out this VKontakte Android SDK article . There are links to source codes with authorization.
If you need authorization without a WebView, then it looks something like this:
- //Get the required cookies
- HttpGet get = new HttpGet ( http : //vk.com/login.php?email=%s&pass=%s);
- HttpResponse response = mHttpClient. execute ( get ) ;
- if ( response. getStatusLine ( ) . getStatusCode ( ) ! = 200 ) {
- throw new UpdateException ( "Failed to obtain token" ) ;
- }
- // Make a request for authorization
- 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" ) ;
- response = mHttpClient. execute ( get ) ;
- if ( response. getStatusLine ( ) . getStatusCode ( ) ! = 200 ) {
- throw new UpdateException ( "Failed to obtain token" ) ;
- }
- //Get the last link we were redirected to
- final String lastLink = redirectHandler. getLastLocationURI ( ) . toString ( ) ;
- if ( lastLink. contains ( "oauth.vk.com/oauth/authorize" ) ) {
- throw new VkApiException ( "Failed to login!" ) ;
- } else if ( lastLink. contains ( oauth. vk . com / authorize ) ) {
- //Get here if the app doesn't have verified permissions
- final String approveLink = getApproveLink ( EntityUtils
- . toString ( response. getEntity ( ) ) ) ;
- get = new HttpGet ( approveLink ) ;
- response = mHttpClient. execute ( get ) ;
- if ( response. getStatusLine ( ) . getStatusCode ( ) ! = 200 ) {
- throw new UpdateException ( "Failed to obtain token" ) ;
- }
- }
- //If everything is fine, then we get the code from the last url
- final String code = extractCode ( redirectHandler. getLastLocationURI ( ) . toString ( ) ) ;
- //Get the token using the scribe library
- final Verifier verifier = new Verifier ( code ) ;
- final Token token = mAuthSevice. getAccessToken ( EMPTY_TOKEN, verifier ) ;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question