Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/main/java/org/scribe/builder/ServiceBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class ServiceBuilder
private Api api;
private String scope;
private SignatureType signatureType;
private String grantType;
private OutputStream debugStream;

/**
Expand Down Expand Up @@ -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");
Expand All @@ -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));
}
}
68 changes: 68 additions & 0 deletions src/main/java/org/scribe/builder/api/ForceApi.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
28 changes: 22 additions & 6 deletions src/main/java/org/scribe/model/OAuthConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class OAuthConfig
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)
Expand All @@ -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()
Expand Down Expand Up @@ -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)
{
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/scribe/model/OAuthConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,7 @@ public class OAuthConstants
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";


}
5 changes: 0 additions & 5 deletions src/main/java/org/scribe/model/Parameter.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package org.scribe.model;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.scribe.exceptions.OAuthException;
import org.scribe.utils.OAuthEncoder;

/**
* @author: Pablo Fernandez
*/
public class Parameter implements Comparable<Parameter>
{
private static final String UTF = "UTF8";

private final String key;
private final String value;

Expand Down
2 changes: 0 additions & 2 deletions src/main/java/org/scribe/model/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
*/
public class Response
{
private static final String EMPTY = "";

private int code;
private String body;
private InputStream stream;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/scribe/oauth/OAuth20ServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/scribe/builder/ServiceBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
73 changes: 73 additions & 0 deletions src/test/java/org/scribe/examples/ForceExample.java
Original file line number Diff line number Diff line change
@@ -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! :)");

}
}
5 changes: 2 additions & 3 deletions src/test/java/org/scribe/model/ParameterListTest.java
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down