Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don’t worry, you can still create the pull request.
  • 2 commits
  • 11 files changed
  • 0 commit comments
  • 1 contributor
View
17 src/main/java/org/scribe/builder/ServiceBuilder.java
@@ -22,6 +22,7 @@
private Api api;
private String scope;
private SignatureType signatureType;
+ private String grantType;
private OutputStream debugStream;
/**
@@ -141,6 +142,20 @@ public ServiceBuilder signatureType(SignatureType type)
return this;
}
+
+ /**
+ * Configures the OAuth grant type . This is only necessary in some APIs (like Salesforce's).
+ *
+ * @param String - OAuth grant type
+ * @return the {@link ServiceBuilder} instance for method chaining
+ */
+ public ServiceBuilder grantType(String type)
+ {
+ Preconditions.checkEmptyString(type, "Invalid grant type");
+ this.grantType = type;
+ return this;
+ }
+
public ServiceBuilder debugStream(OutputStream stream)
{
Preconditions.checkNotNull(stream, "debug stream can't be null");
@@ -164,6 +179,6 @@ public OAuthService build()
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, signatureType, scope, debugStream));
+ return api.createService(new OAuthConfig(apiKey, apiSecret, callback, signatureType, scope, grantType, debugStream));
}
}
View
68 src/main/java/org/scribe/builder/api/ForceApi.java
@@ -0,0 +1,68 @@
+package org.scribe.builder.api;
+
+import org.scribe.extractors.AccessTokenExtractor;
+import org.scribe.extractors.JsonTokenExtractor;
+import org.scribe.model.OAuthConfig;
+import org.scribe.model.Verb;
+import org.scribe.utils.Preconditions;
+
+public class ForceApi extends DefaultApi20 {
+
+ public static final String TYPE = "authorization_code";
+
+ private static final String AUTHORIZE_PARAM = "/services/oauth2/authorize?response_type=code&client_id=%s&redirect_uri=%s";
+
+ private static final String ACCESS_URL_PATH = "/services/oauth2/token";
+
+ protected String baseURL = "https://login.salesforce.com";
+
+ public static class Sandbox extends ForceApi
+ {
+ public Sandbox ()
+ {
+ baseURL = "https://test.salesforce.com";
+ }
+ }
+
+ public static class PreRelease extends ForceApi
+ {
+ public PreRelease ()
+ {
+ baseURL = "https://prerellogin.pre.salesforce.com";
+ }
+ }
+
+ @Override
+ public String getAccessTokenEndpoint() {
+ return baseURL + ACCESS_URL_PATH;
+ }
+
+ @Override
+ public String getAuthorizationUrl(OAuthConfig config) {
+ Preconditions.checkValidUrl(config.getCallback(), "Must provide a valid url as callback");
+ return String.format(baseURL + AUTHORIZE_PARAM , config.getApiKey(), config.getCallback() );
+ }
+
+ /**
+ * Returns the access token extractor.
+ *
+ * @return access token extractor
+ */
+ @Override
+ public AccessTokenExtractor getAccessTokenExtractor()
+ {
+ return new JsonTokenExtractor();
+ }
+
+ /**
+ * Returns the verb for the access token endpoint (defaults to GET)
+ *
+ * @return access token endpoint verb
+ */
+ @Override
+ public Verb getAccessTokenVerb()
+ {
+ return Verb.POST;
+ }
+
+}
View
5 src/main/java/org/scribe/exceptions/OAuthConnectionException.java
@@ -5,7 +5,10 @@
*/
public class OAuthConnectionException extends OAuthException
{
- private static final String MSG = "There was a problem while creating a connection to the remote service.";
+
+ private static final long serialVersionUID = 8845426292529379604L;
+
+ private static final String MSG = "There was a problem while creating a connection to the remote service.";
public OAuthConnectionException(Exception e)
{
View
28 src/main/java/org/scribe/model/OAuthConfig.java
@@ -14,6 +14,7 @@
private final String callback;
private final SignatureType signatureType;
private final String scope;
+ private final String grantType;
private final OutputStream debugStream;
public OAuthConfig(String key, String secret)
@@ -23,12 +24,17 @@ public OAuthConfig(String key, String secret)
public OAuthConfig(String key, String secret, String callback, SignatureType type, String scope, OutputStream stream)
{
- this.apiKey = key;
- this.apiSecret = secret;
- this.callback = callback;
- this.signatureType = type;
- this.scope = scope;
- this.debugStream = stream;
+ this( key, secret, callback, type , scope, null, stream );
+ }
+
+ public OAuthConfig(String key, String secret, String callback, SignatureType type, String scope, String grantType, OutputStream stream){
+ this.apiKey = key;
+ this.apiSecret = secret;
+ this.callback = callback;
+ this.signatureType = type;
+ this.scope = scope;
+ this.grantType = grantType;
+ this.debugStream = stream;
}
public String getApiKey()
@@ -60,6 +66,16 @@ public boolean hasScope()
{
return scope != null;
}
+
+ public String getGrantType()
+ {
+ return grantType;
+ }
+
+ public boolean hasGrantType()
+ {
+ return grantType != null;
+ }
public void log(String message)
{
View
2 src/main/java/org/scribe/model/OAuthConstants.java
@@ -45,5 +45,7 @@
public static final String CLIENT_SECRET = "client_secret";
public static final String REDIRECT_URI = "redirect_uri";
public static final String CODE = "code";
+ public static final String GRANT_TYPE= "grant_type";
+
}
View
5 src/main/java/org/scribe/model/Parameter.java
@@ -1,8 +1,5 @@
package org.scribe.model;
-import java.io.UnsupportedEncodingException;
-import java.net.URLEncoder;
-import org.scribe.exceptions.OAuthException;
import org.scribe.utils.OAuthEncoder;
/**
@@ -10,8 +7,6 @@
*/
public class Parameter implements Comparable<Parameter>
{
- private static final String UTF = "UTF8";
-
private final String key;
private final String value;
View
2 src/main/java/org/scribe/model/Response.java
@@ -14,8 +14,6 @@
*/
public class Response
{
- private static final String EMPTY = "";
-
private int code;
private String body;
private InputStream stream;
View
1 src/main/java/org/scribe/oauth/OAuth20ServiceImpl.java
@@ -33,6 +33,7 @@ public Token getAccessToken(Token requestToken, Verifier verifier)
request.addQuerystringParameter(OAuthConstants.CODE, verifier.getValue());
request.addQuerystringParameter(OAuthConstants.REDIRECT_URI, config.getCallback());
if(config.hasScope()) request.addQuerystringParameter(OAuthConstants.SCOPE, config.getScope());
+ if(config.hasGrantType()) request.addQuerystringParameter(OAuthConstants.GRANT_TYPE, config.getGrantType());
Response response = request.send();
return api.getAccessTokenExtractor().extract(response.getBody());
}
View
10 src/test/java/org/scribe/builder/ServiceBuilderTest.java
@@ -59,6 +59,16 @@ public void shouldAcceptAnScope()
assertEquals(ApiMock.config.getApiSecret(), "secret");
assertEquals(ApiMock.config.getScope(), "rss-api");
}
+
+ @Test
+ public void shouldAcceptAGrantType()
+ {
+ builder.provider(ApiMock.class).apiKey("key").apiSecret("secret").grantType("some_type").build();
+ assertEquals(ApiMock.config.getApiKey(), "key");
+ assertEquals(ApiMock.config.getApiSecret(), "secret");
+ assertEquals(ApiMock.config.getGrantType(), "some_type");
+ }
+
public static class ApiMock implements Api
{
View
73 src/test/java/org/scribe/examples/ForceExample.java
@@ -0,0 +1,73 @@
+package org.scribe.examples;
+
+import java.util.Scanner;
+
+import org.scribe.builder.ServiceBuilder;
+import org.scribe.builder.api.ForceApi;
+import org.scribe.model.OAuthRequest;
+import org.scribe.model.Response;
+import org.scribe.model.Token;
+import org.scribe.model.Verb;
+import org.scribe.model.Verifier;
+import org.scribe.oauth.OAuthService;
+
+public class ForceExample
+{
+ private static final String NETWORK_NAME = "Salesforce";
+ private static final Token EMPTY_TOKEN = null;
+
+ public static void main(String[] args)
+ {
+ // Replace these with your own api key and secret
+ final String apiKey = "your_app_id";
+ final String apiSecret = "your_api_secret";
+ OAuthService service = new ServiceBuilder()
+ .provider(ForceApi.Sandbox.class)
+ .apiKey(apiKey)
+ .apiSecret(apiSecret)
+ .grantType( ForceApi.TYPE )
+ .callback("http://www.example.com/oauth_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...");
+ System.out.println("Get the 'id' URI that accompanies the access token and paste here to request a wealth of information regarding the user and org:");
+ System.out.println(accessToken.getRawResponse());
+ System.out.print(">>");
+ verifier = new Verifier(in.nextLine());
+
+ OAuthRequest request = new OAuthRequest(Verb.GET, verifier.getValue() );
+ service.signRequest(accessToken, request);
+ Response response = request.send();
+ System.out.println("Got it! Lets see the user information...");
+ 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! :)");
+
+ }
+}
View
5 src/test/java/org/scribe/model/ParameterListTest.java
@@ -1,12 +1,11 @@
package org.scribe.model;
+import static org.junit.Assert.assertNotSame;
+
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotSame;
-
/**
* @author: Pablo Fernandez
*/

No commit comments for this range

Something went wrong with that request. Please try again.