Skip to content

Commit 1f94997

Browse files
Added support for Google's 'scope' param
1 parent c3f113f commit 1f94997

File tree

6 files changed

+48
-3
lines changed

6 files changed

+48
-3
lines changed

src/main/java/org/scribe/builder/ServiceBuilder.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class ServiceBuilder
2020
private String apiSecret;
2121
private String callback;
2222
private Api api;
23+
private String scope;
2324

2425
public ServiceBuilder()
2526
{
@@ -92,6 +93,18 @@ public ServiceBuilder apiSecret(String apiSecret)
9293
return this;
9394
}
9495

96+
/**
97+
* Configures the OAuth scope. This is only necessary in some APIs (like Google's).
98+
*
99+
* @param scope The OAuth scope
100+
* @return the {@link ServiceBuilder} instance for method chaining
101+
*/
102+
public ServiceBuilder scope(String scope)
103+
{
104+
this.scope = scope;
105+
return this;
106+
}
107+
95108
/**
96109
* Returns the fully configured {@link OAuthService}
97110
*
@@ -102,6 +115,6 @@ public OAuthService build()
102115
Preconditions.checkNotNull(api, "You must specify a valid api through the provider() method");
103116
Preconditions.checkEmptyString(apiKey, "You must provide an api key");
104117
Preconditions.checkEmptyString(apiSecret, "You must provide an api secret");
105-
return api.createService(apiKey, apiSecret, callback);
118+
return api.createService(apiKey, apiSecret, callback, scope);
106119
}
107120
}

src/main/java/org/scribe/builder/api/Api.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,16 @@ public interface Api
2020
* @return fully configured {@link OAuthService}
2121
*/
2222
OAuthService createService(String apiKey, String apiSecret, String callback);
23+
24+
/**
25+
* Creates an {@link OAuthService}
26+
*
27+
* @param apiKey your application api key
28+
* @param apiSecret your application api secret
29+
* @param callback the callback url (or 'oob' for out of band OAuth)
30+
* @param scope the OAuth scope
31+
*
32+
* @return fully configured {@link OAuthService}
33+
*/
34+
OAuthService createService(String apiKey, String apiSecret, String callback, String scope);
2335
}

src/main/java/org/scribe/builder/api/DefaultApi10a.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public Verb getRequestTokenVerb()
5050
public abstract String getRequestTokenEndpoint();
5151
public abstract String getAccessTokenEndpoint();
5252

53-
@Override
5453
public OAuthService createService(String apiKey, String apiSecret, String callback)
5554
{
5655
return new OAuth10aServiceImpl( getSignatureService(),
@@ -61,6 +60,13 @@ public OAuthService createService(String apiKey, String apiSecret, String callba
6160
getAccessTokenExtractor(),
6261
createConfig(apiKey, apiSecret, callback));
6362
}
63+
64+
public OAuthService createService(String apiKey, String apiSecret, String callback, String scope)
65+
{
66+
OAuthService service = createService(apiKey, apiSecret, callback);
67+
service.addScope(scope);
68+
return service;
69+
}
6470

6571
private OAuthConfig createConfig(String apiKey, String apiSecret, String callback)
6672
{

src/main/java/org/scribe/model/OAuthConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ public class OAuthConstants
3838
public static final String VERIFIER = "oauth_verifier";
3939
public static final String HEADER = "Authorization";
4040
public static final Token EMPTY_TOKEN = new Token("", "");
41+
public static final String SCOPE = "scope";
4142
}

src/main/java/org/scribe/oauth/OAuth10aServiceImpl.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
public class OAuth10aServiceImpl implements OAuthService
88
{
9-
9+
private static final String NO_SCOPE = null;
1010
private static final String VERSION = "1.0";
1111

1212
private OAuthConfig config;
@@ -16,6 +16,7 @@ public class OAuth10aServiceImpl implements OAuthService
1616
private HeaderExtractor headerExtractor;
1717
private RequestTokenExtractor rtExtractor;
1818
private AccessTokenExtractor atExtractor;
19+
private String scope;
1920

2021
public OAuth10aServiceImpl(SignatureService signatureService, TimestampService timestampService, BaseStringExtractor baseStringExtractor,
2122
HeaderExtractor headerExtractor, RequestTokenExtractor rtExtractor, AccessTokenExtractor atExtractor, OAuthConfig config)
@@ -27,6 +28,7 @@ public OAuth10aServiceImpl(SignatureService signatureService, TimestampService t
2728
this.rtExtractor = rtExtractor;
2829
this.atExtractor = atExtractor;
2930
this.config = config;
31+
this.scope = NO_SCOPE;
3032
}
3133

3234
public Token getRequestToken()
@@ -47,6 +49,10 @@ private void addOAuthParams(OAuthRequest request, Token token)
4749
request.addOAuthParameter(OAuthConstants.VERSION, getVersion());
4850
request.addOAuthParameter(OAuthConstants.CALLBACK, config.getCallback());
4951
request.addOAuthParameter(OAuthConstants.SIGNATURE, getSignature(request, token));
52+
if(scope != NO_SCOPE)
53+
{
54+
request.addOAuthParameter(OAuthConstants.SCOPE, scope);
55+
}
5056
}
5157

5258
public Token getAccessToken(Token requestToken, Verifier verifier)
@@ -72,6 +78,11 @@ public String getVersion()
7278
return VERSION;
7379
}
7480

81+
public void addScope(String scope)
82+
{
83+
this.scope = scope;
84+
}
85+
7586
private String getSignature(OAuthRequest request, Token token)
7687
{
7788
String baseString = baseStringExtractor.extract(request);

src/main/java/org/scribe/oauth/OAuthService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ public interface OAuthService
1111
public void signRequest(Token accessToken, OAuthRequest request);
1212

1313
public String getVersion();
14+
15+
public void addScope(String scope);
1416
}

0 commit comments

Comments
 (0)