From ca7950004bf17251129b5bfad07df46d412a15a9 Mon Sep 17 00:00:00 2001 From: Boris Date: Wed, 20 Apr 2011 22:56:38 +0700 Subject: [PATCH] Implemented Vkontakte.ru support --- changelog.txt | 1 + .../org/scribe/builder/api/VkontakteApi.java | 41 +++++++++++ .../scribe/extractors/JsonTokenExtractor.java | 6 +- .../org/scribe/examples/VkontakteExample.java | 68 +++++++++++++++++++ 4 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/scribe/builder/api/VkontakteApi.java create mode 100644 src/test/java/org/scribe/examples/VkontakteExample.java diff --git a/changelog.txt b/changelog.txt index 33462ac..faed428 100644 --- a/changelog.txt +++ b/changelog.txt @@ -53,3 +53,4 @@ [1.2.1] * FEATURE: Added custom charset support to Request (thanks Eric Genet) + * FEATURE: Added support for Vkontakte (thanks dotbg) diff --git a/src/main/java/org/scribe/builder/api/VkontakteApi.java b/src/main/java/org/scribe/builder/api/VkontakteApi.java new file mode 100644 index 0000000..6bb4b6b --- /dev/null +++ b/src/main/java/org/scribe/builder/api/VkontakteApi.java @@ -0,0 +1,41 @@ +package org.scribe.builder.api; + +import org.scribe.extractors.*; +import org.scribe.utils.*; +import org.scribe.model.*; + +/** + * @author Boris G. Tsirkin + * @since 20.4.2011 + */ +public class VkontakteApi extends DefaultApi20 +{ + private static final String AUTHORIZE_URL = "https://api.vkontakte.ru/oauth/authorize?client_id=%s&redirect_uri=%s&response_type=code"; + private static final String SCOPED_AUTHORIZE_URL = String.format("%s&scope=%%s", AUTHORIZE_URL); + + @Override + public String getAccessTokenEndpoint() + { + return "https://api.vkontakte.ru/oauth/access_token"; + } + + @Override + public String getAuthorizationUrl(OAuthConfig config) + { + Preconditions.checkValidUrl(config.getCallback(), "Valid url is required for a callback. Vkontakte does not support OOB"); + if(config.hasScope())// Appending scope if present + { + return String.format(SCOPED_AUTHORIZE_URL, config.getApiKey(), URLUtils.formURLEncode(config.getCallback()),URLUtils.formURLEncode(config.getScope())); + } + else + { + return String.format(AUTHORIZE_URL, config.getApiKey(), URLUtils.formURLEncode(config.getCallback())); + } + } + + @Override + public AccessTokenExtractor getAccessTokenExtractor() + { + return new JsonTokenExtractor(); + } +} diff --git a/src/main/java/org/scribe/extractors/JsonTokenExtractor.java b/src/main/java/org/scribe/extractors/JsonTokenExtractor.java index fbbe3d1..3bec660 100644 --- a/src/main/java/org/scribe/extractors/JsonTokenExtractor.java +++ b/src/main/java/org/scribe/extractors/JsonTokenExtractor.java @@ -8,7 +8,7 @@ import org.scribe.utils.*; public class JsonTokenExtractor implements AccessTokenExtractor { - private Pattern accessTokenPattern = Pattern.compile("\"access_token\":\"(\\S*)\""); + private Pattern accessTokenPattern = Pattern.compile("\"access_token\":\"(\\S*?)\""); @Override public Token extract(String response) @@ -17,11 +17,11 @@ public class JsonTokenExtractor implements AccessTokenExtractor Matcher matcher = accessTokenPattern.matcher(response); if(matcher.find()) { - return new Token(matcher.group(1), ""); + return new Token(matcher.group(1), "", response); } else { - throw new OAuthException("Cannot extract an acces token. Response was: "+response); + throw new OAuthException("Cannot extract an acces token. Response was: " + response); } } diff --git a/src/test/java/org/scribe/examples/VkontakteExample.java b/src/test/java/org/scribe/examples/VkontakteExample.java new file mode 100644 index 0000000..f9e4134 --- /dev/null +++ b/src/test/java/org/scribe/examples/VkontakteExample.java @@ -0,0 +1,68 @@ +package org.scribe.examples; + +import org.scribe.builder.*; +import org.scribe.builder.api.*; +import org.scribe.model.*; +import org.scribe.oauth.*; + +import java.util.*; + +/** + * @author Boris G. Tsirkin + * @since 20.4.2011 + */ +public class VkontakteExample +{ + private static final String NETWORK_NAME = "Vkontakte.ru"; + private static final String PROTECTED_RESOURCE_URL = "https://api.vkontakte.ru/method/friends.get"; + private static final Token EMPTY_TOKEN = null; + + public static void main(String[] args) + { + // Replace these with your own api key and secret + final String clientId = "your app id"; + final String apiSecret = "your api secret"; + OAuthService service = new ServiceBuilder() + .provider(VkontakteApi.class) + .apiKey(clientId) + .apiSecret(apiSecret) + .scope("friends,wall,offline") // replace with desired scope + .callback("http://your.site.com/callback") + .build(); + Scanner in = new Scanner(System.in); + + System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ==="); + System.out.println(); + + // Obtain the Authorization URL + System.out.println("Fetching the Authorization URL..."); + String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN); + System.out.println("Got the Authorization URL!"); + System.out.println("Now go and authorize Scribe here:"); + System.out.println(authorizationUrl); + System.out.println("And paste the authorization code here"); + System.out.print(">>"); + Verifier verifier = new Verifier(in.nextLine()); + System.out.println(); + + // Trade the Request Token and Verfier for the Access Token + System.out.println("Trading the Request Token for an Access Token..."); + Token accessToken = service.getAccessToken(EMPTY_TOKEN, verifier); + System.out.println("Got the Access Token!"); + System.out.println("(if your curious it looks like this: " + accessToken + " )"); + System.out.println(); + + // Now let's go and ask for a protected resource! + System.out.println("Now we're going to access a protected resource..."); + OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL); + service.signRequest(accessToken, request); + Response response = request.send(); + System.out.println("Got it! Lets see what we found..."); + System.out.println(); + System.out.println(response.getCode()); + System.out.println(response.getBody()); + + System.out.println(); + System.out.println("Thats it man! Go and build something awesome with Scribe! :)"); + } +}