26 Commits
Author SHA1 Message Date
Pablo Fernandez 97f45d75e7 bounced version numbers for release 2011-05-04 12:00:45 -03:00
Pablo Fernandez 624fdfb714 removed unused statics 2011-04-29 15:08:35 -03:00
Pablo Fernandez bd51ae9f25 Added plaintext signature for Yammer Api and fixed fucked up indentation 2011-04-29 15:05:09 -03:00
Pablo Fernandez 40ab98d506 added Twitter SSL support 2011-04-27 11:44:55 -03:00
Pablo Fernandez 0abe7f80d4 fixed bug in facebook url generation 2011-04-25 19:21:00 -03:00
Pablo Fernandez 592b1b5fdd Added scope to facebook api 2011-04-25 13:24:11 -03:00
Pablo Fernandez 2112d3d8a9 Added scopes to 2.0 apis 2011-04-25 13:23:37 -03:00
Pablo Fernandez 109c93b4e4 Moved scope inside OAuthConfig 2011-04-25 13:15:25 -03:00
Pablo Fernandez 41b6da1a44 bounced version on pom.xml 2011-04-19 22:59:30 -03:00
Pablo Fernandez 91cb9b8407 changed readme to last version 2011-04-19 22:56:59 -03:00
Pablo Fernandez c265e730a3 removed auto-generated comments 2011-04-19 22:52:36 -03:00
Matthew Laudato 5719d7a18a Support for ConstantContact REST API 2011-04-19 22:42:25 -03:00
Pablo Fernandez 8abdde928a added convenience methods for the builder api 2011-04-19 22:32:32 -03:00
Pablo Fernandez 193dcd2e25 added SignatureType and querystring signing functionallity 2011-04-19 22:17:23 -03:00
Pablo Fernandez 931236561a rename method
to support both querystring and header signatures
2011-04-19 22:04:06 -03:00
Pablo Fernandez 5f40e0dca0 removed unused extractor (JsonTokenExtractor handles this) 2011-04-19 21:51:36 -03:00
Tom Tasche 8afb142b7f Add Foursquare2 API and example 2011-04-19 21:50:12 -03:00
Pablo Fernandez bfcf0655da added Dropbox Api 2011-04-13 19:03:56 -03:00
Pablo Fernandez db88d53807 made extractor independent of token/secret order 2011-04-13 19:02:45 -03:00
Pablo Fernandez cbfd88d749 removed picture from readme since link is broken 2011-04-11 14:06:39 -03:00
Pablo Fernandez 1eb0b1003c Added raw string to Token, for retrieving additional parameters 2011-04-11 14:06:08 -03:00
Stuart Grimshaw 75bcb0013d Added Lovefilm Api and example 2011-03-30 00:37:44 -03:00
Stuart Grimshaw 145c009fc3 Fix for HttpUrlConnection on pre-Gingerbread Android devices 2011-03-30 00:24:12 -03:00
Pablo Fernandez 72901165f8 updated readme with new pom version 2011-03-30 00:09:46 -03:00
Stuart Grimshaw 4f23cfaf20 Moved callback header above call to addOAthParams so it gets properly
signed.
2011-03-29 19:06:19 -03:00
Pablo Fernandez 5c0d150547 Added mvn-bundle script 2011-03-13 10:49:31 -03:00
32 changed files with 540 additions and 104 deletions

No files matched your search

+1 -3
View File
@@ -1,5 +1,3 @@
!http://img411.imageshack.us/img411/2851/scribelogo.png!
h2. Welcome to the home of Scribe, the simple OAuth Java lib!
h1. Why use Scribe?
@@ -63,7 +61,7 @@ You can pull scribe from a maven repository, just add this to your __pom.xml__ f
@<dependency>@
@<groupId>org.scribe</groupId>@
@<artifactId>scribe</artifactId>@
@<version>1.1.0</version>@
@<version>1.2.0</version>@
@</dependency>@
h1. Getting started in less than 2 minutes
Executable
+2
View File
@@ -0,0 +1,2 @@
#!/bin/sh
mvn source:jar javadoc:jar package gpg:sign repository:bundle-create -Dgpg.passphrase=$1
+20 -1
View File
@@ -30,4 +30,23 @@
* FIX: Removed isEmpty() from OAuth2.0 code in order to support older JVMs
* FEATURE: Overloaded provider() method to support instances as well as classes (thanks dustismo)
* FIX: White-space parameter bug (thanks Krogoth)
* FEATURE: OAuth 2.0 now implements server-flow (thanks Diego Silveira)
* FEATURE: OAuth 2.0 now implements server-flow (thanks Diego Silveira)
[1.1.2]
* FIX: Fixed ugly bug that made 1.1.1 practically unusable (thanks Stubbs)
[1.1.3]
* FEATURE: Added rawResponse to Token, for extracting additional parameters (thanks Dirk McCormick)
* FEATURE: Added Dropbox Api
* FIX: Token responses now work despite of the token/secret order
* FEATURE: Added Foursquare OAuth 2.0 Api (thanks Tom Tasche)
* FEATURE: Added support for OAuth1.0a signatures via querystring parameters
* FEATURE: Added ConstantContact Api (thanks Matthew Laudato)
[1.2]
* REFACTOR: Moved scope inside OAuthConfig (breaks backwards compatibility)
* FEATURE: Added scopes to Facebook 2.0 Api
* FEATURE: Added Plaintext signature for Yammer
* FEATURE: Added Twitter SSL endpoints (use TwitterApi.SSL.class)
+1 -1
View File
@@ -4,7 +4,7 @@
<groupId>org.scribe</groupId>
<artifactId>scribe</artifactId>
<packaging>jar</packaging>
<version>1.1.1</version>
<version>1.2.0</version>
<name>Scribe OAuth Library</name>
<description>The best OAuth library out there</description>
<url>http://github.com/fernandezpablo85/scribe-java</url>
@@ -20,6 +20,7 @@ public class ServiceBuilder
private String callback;
private Api api;
private String scope;
private SignatureType signatureType;
/**
* Default constructor
@@ -122,6 +123,19 @@ public class ServiceBuilder
this.scope = scope;
return this;
}
/**
* Configures the signature type, choose between header, querystring, etc. Defaults to Header
*
* @param scope The OAuth scope
* @return the {@link ServiceBuilder} instance for method chaining
*/
public ServiceBuilder signatureType(SignatureType type)
{
Preconditions.checkNotNull(type, "Signature type can't be null");
this.signatureType = type;
return this;
}
/**
* Returns the fully configured {@link OAuthService}
@@ -133,6 +147,6 @@ public class ServiceBuilder
Preconditions.checkNotNull(api, "You must specify a valid api through the provider() method");
Preconditions.checkEmptyString(apiKey, "You must provide an api key");
Preconditions.checkEmptyString(apiSecret, "You must provide an api secret");
return api.createService(new OAuthConfig(apiKey, apiSecret, callback), scope);
return api.createService(new OAuthConfig(apiKey, apiSecret, callback, signatureType, scope));
}
}
@@ -21,5 +21,5 @@ public interface Api
*
* @return fully configured {@link OAuthService}
*/
OAuthService createService(OAuthConfig config, String scope);
OAuthService createService(OAuthConfig config);
}
@@ -0,0 +1,26 @@
package org.scribe.builder.api;
import org.scribe.model.*;
public class ConstantContactApi extends DefaultApi10a
{
private static final String AUTHORIZE_URL = "https://oauth.constantcontact.com/ws/oauth/confirm_access?oauth_token=%s";
@Override
public String getAccessTokenEndpoint()
{
return "https://oauth.constantcontact.com/ws/oauth/access_token";
}
@Override
public String getAuthorizationUrl(Token requestToken)
{
return String.format(AUTHORIZE_URL, requestToken.getToken());
}
@Override
public String getRequestTokenEndpoint()
{
return "https://oauth.constantcontact.com/ws/oauth/request_token";
}
}
@@ -134,14 +134,7 @@ public abstract class DefaultApi10a implements Api
* @param callback OAuth callback (either URL or 'oob')
* @param scope OAuth scope (optional)
*/
public OAuthService createService(OAuthConfig config, String scope)
{
OAuthService service = doCreateService(config);
service.addScope(scope);
return service;
}
private OAuthService doCreateService(OAuthConfig config)
public OAuthService createService(OAuthConfig config)
{
return new OAuth10aServiceImpl(this, config);
}
@@ -62,7 +62,7 @@ public abstract class DefaultApi20 implements Api
/**
* {@inheritDoc}
*/
public OAuthService createService(OAuthConfig config, String scope)
public OAuthService createService(OAuthConfig config)
{
return new OAuth20ServiceImpl(this, config);
}
@@ -0,0 +1,25 @@
package org.scribe.builder.api;
import org.scribe.model.*;
public class DropBoxApi extends DefaultApi10a
{
@Override
public String getAccessTokenEndpoint()
{
return "https://api.dropbox.com/0/oauth/access_token";
}
@Override
public String getAuthorizationUrl(Token requestToken)
{
return "https://www.dropbox.com/0/oauth/authorize?oauth_token="+requestToken.getToken();
}
@Override
public String getRequestTokenEndpoint()
{
return "https://api.dropbox.com/0/oauth/request_token";
}
}
@@ -1,22 +1,34 @@
package org.scribe.builder.api;
import org.scribe.model.*;
import org.scribe.utils.*;
import static org.scribe.utils.URLUtils.*;
public class FacebookApi extends DefaultApi20
{
private static final String AUTHORIZE_URL = "https://www.facebook.com/dialog/oauth?client_id=%s&redirect_uri=%s";
private static final String SCOPED_AUTHORIZE_URL = AUTHORIZE_URL + "&scope=%s";
@Override
public String getAccessTokenEndpoint()
{
return "https://graph.facebook.com/oauth/access_token";
}
@Override
public String getAuthorizationUrl(OAuthConfig config)
{
Preconditions.checkValidUrl(config.getCallback(), "Must provide a valid url as callback. Facebook does not support OOB");
return String.format(AUTHORIZE_URL, config.getApiKey(), URLUtils.formURLEncode(config.getCallback()));
// Append scope if present
if(config.hasScope())
{
return String.format(SCOPED_AUTHORIZE_URL, config.getApiKey(), formURLEncode(config.getCallback()), formURLEncode(config.getScope()));
}
else
{
return String.format(AUTHORIZE_URL, config.getApiKey(), formURLEncode(config.getCallback()));
}
}
}
@@ -0,0 +1,29 @@
package org.scribe.builder.api;
import org.scribe.extractors.*;
import org.scribe.model.*;
import org.scribe.utils.*;
public class Foursquare2Api extends DefaultApi20
{
private static final String AUTHORIZATION_URL = "https://foursquare.com/oauth2/authenticate?client_id=%s&response_type=code&redirect_uri=%s";
@Override
public String getAccessTokenEndpoint()
{
return "https://foursquare.com/oauth2/access_token?grant_type=authorization_code";
}
@Override
public String getAuthorizationUrl(OAuthConfig config)
{
Preconditions.checkValidUrl(config.getCallback(), "Must provide a valid url as callback. Foursquare2 does not support OOB");
return String.format(AUTHORIZATION_URL, config.getApiKey(), URLUtils.formURLEncode(config.getCallback()));
}
@Override
public AccessTokenExtractor getAccessTokenExtractor()
{
return new JsonTokenExtractor();
}
}
@@ -0,0 +1,28 @@
package org.scribe.builder.api;
import org.scribe.model.Token;
public class LoveFilmApi extends DefaultApi10a
{
private static final String REQUEST_TOKEN_URL = "http://openapi.lovefilm.com/oauth/request_token";
private static final String ACCESS_TOKEN_URL = "http://openapi.lovefilm.com/oauth/access_token";
private static final String AUTHORIZE_URL = "https://www.lovefilm.com/activate?oauth_token=%s";
@Override
public String getRequestTokenEndpoint()
{
return REQUEST_TOKEN_URL;
}
@Override
public String getAccessTokenEndpoint()
{
return ACCESS_TOKEN_URL;
}
@Override
public String getAuthorizationUrl(Token requestToken)
{
return String.format(AUTHORIZE_URL, requestToken.getToken());
}
}
@@ -5,22 +5,39 @@ import org.scribe.model.Token;
public class TwitterApi extends DefaultApi10a
{
private static final String AUTHORIZE_URL = "https://api.twitter.com/oauth/authorize?oauth_token=%s";
private static final String REQUEST_TOKEN_RESOURCE = "api.twitter.com/oauth/request_token";
private static final String ACCESS_TOKEN_RESOURCE = "api.twitter.com/oauth/access_token";
@Override
public String getAccessTokenEndpoint()
{
return "http://api.twitter.com/oauth/access_token";
return "http://" + ACCESS_TOKEN_RESOURCE;
}
@Override
public String getRequestTokenEndpoint()
{
return "http://api.twitter.com/oauth/request_token";
return "http://" + REQUEST_TOKEN_RESOURCE;
}
@Override
public String getAuthorizationUrl(Token requestToken)
{
return String.format(AUTHORIZE_URL, requestToken.getToken());
}
public static class SSL extends TwitterApi
{
@Override
public String getAccessTokenEndpoint()
{
return "https://" + ACCESS_TOKEN_RESOURCE;
}
@Override
public String getRequestTokenEndpoint()
{
return "https://" + REQUEST_TOKEN_RESOURCE;
}
}
}
@@ -1,26 +1,33 @@
package org.scribe.builder.api;
import org.scribe.model.Token;
import org.scribe.model.*;
import org.scribe.services.*;
public class YammerApi extends DefaultApi10a
{
private static final String AUTHORIZATION_URL = "'https://www.yammer.com/oauth/authorize?oauth_token=%s'";
@Override
public String getRequestTokenEndpoint()
@Override
public String getRequestTokenEndpoint()
{
return "https://www.yammer.com/oauth/request_token";
}
return "https://www.yammer.com/oauth/request_token";
}
@Override
public String getAccessTokenEndpoint()
@Override
public String getAccessTokenEndpoint()
{
return "https://www.yammer.com/oauth/access_token";
}
@Override
public String getAuthorizationUrl(Token requestToken)
{
return String.format(AUTHORIZATION_URL, requestToken.getToken());
}
return "https://www.yammer.com/oauth/access_token";
}
@Override
public String getAuthorizationUrl(Token requestToken)
{
return String.format(AUTHORIZATION_URL, requestToken.getToken());
}
@Override
public SignatureService getSignatureService()
{
return new PlaintextSignatureService();
}
}
@@ -0,0 +1,28 @@
package org.scribe.extractors;
import java.util.regex.*;
import org.scribe.exceptions.*;
import org.scribe.model.*;
import org.scribe.utils.*;
public class JsonTokenExtractor implements AccessTokenExtractor
{
private Pattern accessTokenPattern = Pattern.compile("\"access_token\":\"(\\S*)\"");
@Override
public Token extract(String response)
{
Preconditions.checkEmptyString(response, "Cannot extract a token from a null or empty String");
Matcher matcher = accessTokenPattern.matcher(response);
if(matcher.find())
{
return new Token(matcher.group(1), "");
}
else
{
throw new OAuthException("Cannot extract an acces token. Response was: "+response);
}
}
}
@@ -26,7 +26,7 @@ public class TokenExtractor20Impl implements AccessTokenExtractor
if (matcher.matches())
{
String token = URLUtils.formURLDecode(matcher.group(1));
return new Token(token, EMPTY_SECRET);
return new Token(token, EMPTY_SECRET, response);
}
else
{
@@ -15,7 +15,8 @@ import org.scribe.utils.*;
*/
public class TokenExtractorImpl implements RequestTokenExtractor, AccessTokenExtractor
{
private static final String TOKEN_REGEX = "oauth_token=(\\S*)&oauth_token_secret=(\\S*?)(&(.*))?";
private static final Pattern TOKEN_REGEX = Pattern.compile("oauth_token=([^&]+)");
private static final Pattern SECRET_REGEX = Pattern.compile("oauth_token_secret=([^&]+)");
/**
* {@inheritDoc}
@@ -23,17 +24,21 @@ public class TokenExtractorImpl implements RequestTokenExtractor, AccessTokenExt
public Token extract(String response)
{
Preconditions.checkEmptyString(response, "Response body is incorrect. Can't extract a token from an empty string");
String token = extract(response, TOKEN_REGEX);
String secret = extract(response, SECRET_REGEX);
return new Token(token, secret, response);
}
Matcher matcher = Pattern.compile(TOKEN_REGEX).matcher(response);
if (matcher.matches())
private String extract(String response, Pattern p)
{
Matcher matcher = p.matcher(response);
if (matcher.find() && matcher.groupCount() >= 1)
{
String token = URLUtils.formURLDecode(matcher.group(1));
String secret = URLUtils.formURLDecode(matcher.group(2));
return new Token(token, secret);
return URLUtils.formURLDecode(matcher.group(1));
}
else
{
throw new OAuthException("Response body is incorrect. Can't extract a token from this: '" + response + "'", null);
throw new OAuthException("Response body is incorrect. Can't extract token and secret from this: '" + response + "'", null);
}
}
}
+24 -20
View File
@@ -7,20 +7,24 @@ package org.scribe.model;
*/
public class OAuthConfig
{
private String apiKey;
private String apiSecret;
private String callback;
private final String apiKey;
private final String apiSecret;
private final String callback;
private final SignatureType signatureType;
private final String scope;
public OAuthConfig(String key, String secret)
{
this(key,secret,null);
this(key, secret, null, null, null);
}
public OAuthConfig(String key, String secret, String callback)
public OAuthConfig(String key, String secret, String callback, SignatureType type, String scope)
{
this.apiKey = key;
this.apiSecret = secret;
this.callback = callback;
this.callback = callback != null ? callback : OAuthConstants.OUT_OF_BAND;
this.signatureType = (type != null) ? type : SignatureType.Header;
this.scope = scope;
}
public String getApiKey()
@@ -28,28 +32,28 @@ public class OAuthConfig
return apiKey;
}
public void setApiKey(String apiKey)
{
this.apiKey = apiKey;
}
public String getApiSecret()
{
return apiSecret;
}
public void setApiSecret(String apiSecret)
{
this.apiSecret = apiSecret;
}
public String getCallback()
{
return callback != null ? callback : OAuthConstants.OUT_OF_BAND;
return callback;
}
public void setCallback(String callback)
public SignatureType getSignatureType()
{
this.callback = callback;
return signatureType;
}
public String getScope()
{
return scope;
}
public boolean hasScope()
{
return scope != null;
}
}
@@ -64,6 +64,7 @@ class Request
String effectiveUrl = URLUtils.appendParametersToQueryString(url, querystringParams);
if (connection == null)
{
System.setProperty("http.keepAlive", "false");
connection = (HttpURLConnection) new URL(effectiveUrl).openConnection();
}
}
@@ -0,0 +1,7 @@
package org.scribe.model;
public enum SignatureType
{
Header,
QueryString
}
+16
View File
@@ -13,6 +13,7 @@ public class Token implements Serializable
private final String token;
private final String secret;
private final String rawResponse;
/**
* Default constructor
@@ -21,9 +22,15 @@ public class Token implements Serializable
* @param secret token secret
*/
public Token(String token, String secret)
{
this(token, secret, null);
}
public Token(String token, String secret, String rawResponse)
{
this.token = token;
this.secret = secret;
this.rawResponse = rawResponse;
}
public String getToken()
@@ -36,6 +43,15 @@ public class Token implements Serializable
return secret;
}
public String getRawResponse()
{
if (rawResponse == null)
{
throw new IllegalStateException("This token object was not constructed by scribe and does not have a rawResponse");
}
return rawResponse;
}
@Override
public String toString()
{
@@ -1,5 +1,7 @@
package org.scribe.oauth;
import java.util.*;
import org.scribe.builder.api.*;
import org.scribe.model.*;
@@ -10,12 +12,10 @@ import org.scribe.model.*;
*/
public class OAuth10aServiceImpl implements OAuthService
{
private static final String NO_SCOPE = null;
private static final String VERSION = "1.0";
private OAuthConfig config;
private DefaultApi10a api;
private String scope;
/**
* Default constructor
@@ -27,7 +27,6 @@ public class OAuth10aServiceImpl implements OAuthService
{
this.api = api;
this.config = config;
this.scope = NO_SCOPE;
}
/**
@@ -36,9 +35,9 @@ public class OAuth10aServiceImpl implements OAuthService
public Token getRequestToken()
{
OAuthRequest request = new OAuthRequest(api.getRequestTokenVerb(), api.getRequestTokenEndpoint());
addOAuthParams(request, OAuthConstants.EMPTY_TOKEN);
request.addOAuthParameter(OAuthConstants.CALLBACK, config.getCallback());
addOAuthHeader(request);
addOAuthParams(request, OAuthConstants.EMPTY_TOKEN);
addSignature(request);
Response response = request.send();
return api.getRequestTokenExtractor().extract(response.getBody());
}
@@ -50,7 +49,7 @@ public class OAuth10aServiceImpl implements OAuthService
request.addOAuthParameter(OAuthConstants.CONSUMER_KEY, config.getApiKey());
request.addOAuthParameter(OAuthConstants.SIGN_METHOD, api.getSignatureService().getSignatureMethod());
request.addOAuthParameter(OAuthConstants.VERSION, getVersion());
if(scope != NO_SCOPE) request.addOAuthParameter(OAuthConstants.SCOPE, scope);
if(config.hasScope()) request.addOAuthParameter(OAuthConstants.SCOPE, config.getScope());
request.addOAuthParameter(OAuthConstants.SIGNATURE, getSignature(request, token));
}
@@ -63,7 +62,7 @@ public class OAuth10aServiceImpl implements OAuthService
request.addOAuthParameter(OAuthConstants.TOKEN, requestToken.getToken());
request.addOAuthParameter(OAuthConstants.VERIFIER, verifier.getValue());
addOAuthParams(request, requestToken);
addOAuthHeader(request);
addSignature(request);
Response response = request.send();
return api.getAccessTokenExtractor().extract(response.getBody());
}
@@ -75,7 +74,7 @@ public class OAuth10aServiceImpl implements OAuthService
{
request.addOAuthParameter(OAuthConstants.TOKEN, token.getToken());
addOAuthParams(request, token);
addOAuthHeader(request);
addSignature(request);
}
/**
@@ -86,14 +85,6 @@ public class OAuth10aServiceImpl implements OAuthService
return VERSION;
}
/**
* {@inheritDoc}
*/
public void addScope(String scope)
{
this.scope = scope;
}
/**
* {@inheritDoc}
*/
@@ -108,9 +99,20 @@ public class OAuth10aServiceImpl implements OAuthService
return api.getSignatureService().getSignature(baseString, config.getApiSecret(), token.getSecret());
}
private void addOAuthHeader(OAuthRequest request)
private void addSignature(OAuthRequest request)
{
String oauthHeader = api.getHeaderExtractor().extract(request);
request.addHeader(OAuthConstants.HEADER, oauthHeader);
switch (config.getSignatureType())
{
case Header:
String oauthHeader = api.getHeaderExtractor().extract(request);
request.addHeader(OAuthConstants.HEADER, oauthHeader);
break;
case QueryString:
for (Map.Entry<String, String> entry : request.getOauthParameters().entrySet())
{
request.addQuerystringParameter(entry.getKey(), entry.getValue());
}
break;
}
}
}
@@ -22,14 +22,6 @@ public class OAuth20ServiceImpl implements OAuthService
this.config = config;
}
/**
* {@inheritDoc}
*/
public void addScope(String scope)
{
throw new UnsupportedOperationException("OAuth 2 does not use scopes");
}
/**
* {@inheritDoc}
*/
@@ -40,6 +32,7 @@ public class OAuth20ServiceImpl implements OAuthService
request.addQuerystringParameter(OAuthConstants.CLIENT_SECRET, config.getApiSecret());
request.addQuerystringParameter(OAuthConstants.CODE, verifier.getValue());
request.addQuerystringParameter(OAuthConstants.REDIRECT_URI, config.getCallback());
if(config.hasScope()) request.addQuerystringParameter(OAuthConstants.SCOPE, config.getScope());
Response response = request.send();
return api.getAccessTokenExtractor().extract(response.getBody());
}
@@ -41,13 +41,6 @@ public interface OAuthService
* @return oauth version as string
*/
public String getVersion();
/**
* Adds the 'scope' parameter. This is **not** a default OAuth parameter and the setting is optional.
*
* @param scope OAuth Api scope (optional)
*/
public void addScope(String scope);
/**
* Returns the URL where you should redirect your users to authenticate
@@ -0,0 +1,40 @@
package org.scribe.services;
import org.scribe.exceptions.*;
import org.scribe.utils.*;
/**
* plaintext implementation of {@SignatureService}
*
* @author Pablo Fernandez
*
*/
public class PlaintextSignatureService implements SignatureService
{
private static final String METHOD = "plaintext";
/**
* {@inheritDoc}
*/
public String getSignature(String baseString, String apiSecret, String tokenSecret)
{
try
{
Preconditions.checkEmptyString(apiSecret, "Api secret cant be null or empty string");
return URLUtils.percentEncode(apiSecret) + '&' + URLUtils.percentEncode(tokenSecret);
}
catch (Exception e)
{
throw new OAuthSignatureException(baseString, e);
}
}
/**
* {@inheritDoc}
*/
public String getSignatureMethod()
{
return METHOD;
}
}
@@ -18,12 +18,13 @@ public class ServiceBuilderTest
}
@Test
public void shouldReturnOOBasDefaultCallback()
public void shouldReturnConfigDefaultValues()
{
builder.provider(ApiMock.class).apiKey("key").apiSecret("secret").build();
assertEquals(ApiMock.config.getApiKey(), "key");
assertEquals(ApiMock.config.getApiSecret(), "secret");
assertEquals(ApiMock.config.getCallback(), OAuthConstants.OUT_OF_BAND);
assertEquals(ApiMock.config.getSignatureType(), SignatureType.Header);
}
@Test
@@ -35,6 +36,15 @@ public class ServiceBuilderTest
assertEquals(ApiMock.config.getCallback(), "http://example.com");
}
@Test
public void shouldAcceptASignatureType()
{
builder.provider(ApiMock.class).apiKey("key").apiSecret("secret").signatureType(SignatureType.QueryString).build();
assertEquals(ApiMock.config.getApiKey(), "key");
assertEquals(ApiMock.config.getApiSecret(), "secret");
assertEquals(ApiMock.config.getSignatureType(), SignatureType.QueryString);
}
@Test(expected=IllegalArgumentException.class)
public void shouldNotAcceptAnInvalidUrlAsCallback()
{
@@ -47,19 +57,17 @@ public class ServiceBuilderTest
builder.provider(ApiMock.class).apiKey("key").apiSecret("secret").scope("rss-api").build();
assertEquals(ApiMock.config.getApiKey(), "key");
assertEquals(ApiMock.config.getApiSecret(), "secret");
assertEquals(ApiMock.scope, "rss-api");
assertEquals(ApiMock.config.getScope(), "rss-api");
}
public static class ApiMock implements Api
{
public static OAuthConfig config;
public static String scope;
@Override
public OAuthService createService(OAuthConfig config, String scope)
public OAuthService createService(OAuthConfig config)
{
ApiMock.config = config;
ApiMock.scope = scope;
return null;
}
}
@@ -0,0 +1,63 @@
package org.scribe.examples;
import java.util.*;
import org.scribe.builder.*;
import org.scribe.builder.api.*;
import org.scribe.model.*;
import org.scribe.oauth.*;
public class Foursquare2Example
{
private static final String PROTECTED_RESOURCE_URL = "https://api.foursquare.com/v2/users/self/friends?oauth_token=";
private static final Token EMPTY_TOKEN = null;
public static void main(String[] args)
{
// Replace these with your own api key and secret
String apiKey = "FEGFXJUFANVVDHVSNUAMUKTTXCP1AJQD53E33XKJ44YP1S4I";
String apiSecret = "AYWKUL5SWPNC0CTQ202QXRUG2NLZYXMRA34ZSDW4AUYBG2RC";
OAuthService service = new ServiceBuilder()
.provider(Foursquare2Api.class)
.apiKey(apiKey)
.apiSecret(apiSecret)
.callback("http://localhost:9000/")
.build();
Scanner in = new Scanner(System.in);
System.out.println("=== Foursquare2'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 + accessToken.getToken());
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! :)");
}
}
@@ -0,0 +1,64 @@
package org.scribe.examples;
import java.util.*;
import org.scribe.builder.*;
import org.scribe.builder.api.*;
import org.scribe.model.*;
import org.scribe.oauth.*;
public class LoveFilmExample
{
private static final String NETWORK_NAME = "LoveFilm";
private static final String PROTECTED_RESOURCE_URL = "https://api.lovefilm.com/users";
public static void main(String[] args)
{
// Replace these with your own api key and secret
String apiKey = "your_key";
String apiSecret = "your_secret";
OAuthService service = new ServiceBuilder().provider(LoveFilmApi.class).apiKey(apiKey).apiSecret(apiSecret).build();
Scanner in = new Scanner(System.in);
System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
System.out.println();
// Grab a request token.
System.out.println("Fetching request token.");
Token requestToken = service.getRequestToken();
System.out.println("Got it ... ");
System.out.println(requestToken.getToken());
// Obtain the Authorization URL
System.out.println("Fetching the Authorization URL...");
String authorizationUrl = service.getAuthorizationUrl(requestToken);
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(requestToken, 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! :)");
}
}
@@ -0,0 +1,31 @@
package org.scribe.extractors;
import static org.junit.Assert.*;
import org.junit.*;
import org.scribe.model.*;
public class JsonTokenExtractorTest
{
private String response = "'{ \"access_token\":\"I0122HHJKLEM21F3WLPYHDKGKZULAUO4SGMV3ABKFTDT3T3X\"}'";
private JsonTokenExtractor extractor = new JsonTokenExtractor();
@Test
public void shouldParseResponse()
{
Token token = extractor.extract(response);
assertEquals(token.getToken(), "I0122HHJKLEM21F3WLPYHDKGKZULAUO4SGMV3ABKFTDT3T3X");
}
@Test(expected=IllegalArgumentException.class)
public void shouldThrowExceptionIfForNullParameters()
{
extractor.extract(null);
}
@Test(expected=IllegalArgumentException.class)
public void shouldThrowExceptionIfForEmptyStrings()
{
extractor.extract("");
}
}
@@ -26,6 +26,15 @@ public class TokenExtractorTest
assertEquals("hdhd0244k9j7ao03", extracted.getSecret());
}
@Test
public void shouldExtractTokenFromInvertedOAuthStandardResponse()
{
String response = "oauth_token_secret=hh5s93j4hdidpola&oauth_token=hdhd0244k9j7ao03";
Token extracted = extractor.extract(response);
assertEquals("hh5s93j4hdidpola", extracted.getSecret());
assertEquals("hdhd0244k9j7ao03", extracted.getToken());
}
@Test
public void shouldExtractTokenFromResponseWithCallbackConfirmed()
{
@@ -12,12 +12,14 @@ public class OAuthConfigTest
{
OAuthConfig config = new OAuthConfig("key", "secret");
assertEquals(OAuthConstants.OUT_OF_BAND, config.getCallback());
assertEquals(SignatureType.Header, config.getSignatureType());
assertFalse(config.hasScope());
}
@Test
public void shouldOverrideDefaultsIfSet()
{
OAuthConfig config = new OAuthConfig("key", "secret", "http://callback");
OAuthConfig config = new OAuthConfig("key", "secret", "http://callback", SignatureType.Header, "scope");
assertEquals("http://callback", config.getCallback());
assertEquals("key", config.getApiKey());
assertEquals("secret", config.getApiSecret());