Added support for Google's 'scope' param
This commit is contained in:
1 parent
c3f113f39b
commit
1f94997120
6 files changed
+48
-3
No files matched your search
@@ -20,6 +20,7 @@ public class ServiceBuilder
|
||||
private String apiSecret;
|
||||
private String callback;
|
||||
private Api api;
|
||||
private String scope;
|
||||
|
||||
public ServiceBuilder()
|
||||
{
|
||||
@@ -92,6 +93,18 @@ public class ServiceBuilder
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the OAuth scope. This is only necessary in some APIs (like Google's).
|
||||
*
|
||||
* @param scope The OAuth scope
|
||||
* @return the {@link ServiceBuilder} instance for method chaining
|
||||
*/
|
||||
public ServiceBuilder scope(String scope)
|
||||
{
|
||||
this.scope = scope;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the fully configured {@link OAuthService}
|
||||
*
|
||||
@@ -102,6 +115,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(apiKey, apiSecret, callback);
|
||||
return api.createService(apiKey, apiSecret, callback, scope);
|
||||
}
|
||||
}
|
||||
@@ -20,4 +20,16 @@ public interface Api
|
||||
* @return fully configured {@link OAuthService}
|
||||
*/
|
||||
OAuthService createService(String apiKey, String apiSecret, String callback);
|
||||
|
||||
/**
|
||||
* Creates an {@link OAuthService}
|
||||
*
|
||||
* @param apiKey your application api key
|
||||
* @param apiSecret your application api secret
|
||||
* @param callback the callback url (or 'oob' for out of band OAuth)
|
||||
* @param scope the OAuth scope
|
||||
*
|
||||
* @return fully configured {@link OAuthService}
|
||||
*/
|
||||
OAuthService createService(String apiKey, String apiSecret, String callback, String scope);
|
||||
}
|
||||
@@ -50,7 +50,6 @@ public abstract class DefaultApi10a implements Api
|
||||
public abstract String getRequestTokenEndpoint();
|
||||
public abstract String getAccessTokenEndpoint();
|
||||
|
||||
@Override
|
||||
public OAuthService createService(String apiKey, String apiSecret, String callback)
|
||||
{
|
||||
return new OAuth10aServiceImpl( getSignatureService(),
|
||||
@@ -61,6 +60,13 @@ public abstract class DefaultApi10a implements Api
|
||||
getAccessTokenExtractor(),
|
||||
createConfig(apiKey, apiSecret, callback));
|
||||
}
|
||||
|
||||
public OAuthService createService(String apiKey, String apiSecret, String callback, String scope)
|
||||
{
|
||||
OAuthService service = createService(apiKey, apiSecret, callback);
|
||||
service.addScope(scope);
|
||||
return service;
|
||||
}
|
||||
|
||||
private OAuthConfig createConfig(String apiKey, String apiSecret, String callback)
|
||||
{
|
||||
|
||||
@@ -38,4 +38,5 @@ public class OAuthConstants
|
||||
public static final String VERIFIER = "oauth_verifier";
|
||||
public static final String HEADER = "Authorization";
|
||||
public static final Token EMPTY_TOKEN = new Token("", "");
|
||||
public static final String SCOPE = "scope";
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import org.scribe.services.*;
|
||||
|
||||
public class OAuth10aServiceImpl implements OAuthService
|
||||
{
|
||||
|
||||
private static final String NO_SCOPE = null;
|
||||
private static final String VERSION = "1.0";
|
||||
|
||||
private OAuthConfig config;
|
||||
@@ -16,6 +16,7 @@ public class OAuth10aServiceImpl implements OAuthService
|
||||
private HeaderExtractor headerExtractor;
|
||||
private RequestTokenExtractor rtExtractor;
|
||||
private AccessTokenExtractor atExtractor;
|
||||
private String scope;
|
||||
|
||||
public OAuth10aServiceImpl(SignatureService signatureService, TimestampService timestampService, BaseStringExtractor baseStringExtractor,
|
||||
HeaderExtractor headerExtractor, RequestTokenExtractor rtExtractor, AccessTokenExtractor atExtractor, OAuthConfig config)
|
||||
@@ -27,6 +28,7 @@ public class OAuth10aServiceImpl implements OAuthService
|
||||
this.rtExtractor = rtExtractor;
|
||||
this.atExtractor = atExtractor;
|
||||
this.config = config;
|
||||
this.scope = NO_SCOPE;
|
||||
}
|
||||
|
||||
public Token getRequestToken()
|
||||
@@ -47,6 +49,10 @@ public class OAuth10aServiceImpl implements OAuthService
|
||||
request.addOAuthParameter(OAuthConstants.VERSION, getVersion());
|
||||
request.addOAuthParameter(OAuthConstants.CALLBACK, config.getCallback());
|
||||
request.addOAuthParameter(OAuthConstants.SIGNATURE, getSignature(request, token));
|
||||
if(scope != NO_SCOPE)
|
||||
{
|
||||
request.addOAuthParameter(OAuthConstants.SCOPE, scope);
|
||||
}
|
||||
}
|
||||
|
||||
public Token getAccessToken(Token requestToken, Verifier verifier)
|
||||
@@ -72,6 +78,11 @@ public class OAuth10aServiceImpl implements OAuthService
|
||||
return VERSION;
|
||||
}
|
||||
|
||||
public void addScope(String scope)
|
||||
{
|
||||
this.scope = scope;
|
||||
}
|
||||
|
||||
private String getSignature(OAuthRequest request, Token token)
|
||||
{
|
||||
String baseString = baseStringExtractor.extract(request);
|
||||
|
||||
@@ -11,4 +11,6 @@ public interface OAuthService
|
||||
public void signRequest(Token accessToken, OAuthRequest request);
|
||||
|
||||
public String getVersion();
|
||||
|
||||
public void addScope(String scope);
|
||||
}
|
||||
Reference in new issue
Block a user