Skip to content

Commit

Permalink
Added Automatic OAuth2 API
Browse files Browse the repository at this point in the history
Signed-off-by: Sriram <[email protected]>
  • Loading branch information
ramsrib committed Mar 9, 2018
1 parent dc54b59 commit 7228206
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ ScribeJava support out-of-box several HTTP clients:

### Supports all major 1.0a and 2.0 OAuth APIs out-of-the-box

* Automatic (http://www.automatic.com/)
* AWeber (http://www.aweber.com/)
* Box (https://www.box.com/)
* Dataporten (https://docs.dataporten.no/)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.github.scribejava.apis;

import com.github.scribejava.core.builder.api.ClientAuthenticationType;
import com.github.scribejava.core.builder.api.DefaultApi20;
import com.github.scribejava.core.extractors.OAuth2AccessTokenExtractor;
import com.github.scribejava.core.extractors.OAuth2AccessTokenJsonExtractor;
import com.github.scribejava.core.extractors.TokenExtractor;
import com.github.scribejava.core.model.OAuth2AccessToken;
import com.github.scribejava.core.model.Verb;

public class AutomaticAPI extends DefaultApi20 {

private static final String AUTHORIZE_URL = "https://accounts.automatic.com/oauth/authorize";
private static final String REFRESH_TOKEN_ENDPOINT = "https://accounts.automatic.com/oauth/refresh_token";
private static final String ACCESS_TOKEN_ENDPOINT = "https://accounts.automatic.com/oauth/access_token";

protected AutomaticAPI() {
}

private static class InstanceHolder {
private static final AutomaticAPI INSTANCE = new AutomaticAPI();
}

public static AutomaticAPI instance() {
return InstanceHolder.INSTANCE;
}

@Override
public Verb getAccessTokenVerb() {
return Verb.POST;
}

@Override
public String getAccessTokenEndpoint() {
return ACCESS_TOKEN_ENDPOINT;
}

@Override
public String getRefreshTokenEndpoint() {
return REFRESH_TOKEN_ENDPOINT;
}

@Override
protected String getAuthorizationBaseUrl() {
return AUTHORIZE_URL;
}

@Override
public TokenExtractor<OAuth2AccessToken> getAccessTokenExtractor() {
return OAuth2AccessTokenJsonExtractor.instance();
}

@Override
public ClientAuthenticationType getClientAuthenticationType() {
return ClientAuthenticationType.REQUEST_BODY;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.github.scribejava.apis.examples;

import com.github.scribejava.apis.AutomaticAPI;
import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.core.model.OAuth2AccessToken;
import com.github.scribejava.core.model.OAuthRequest;
import com.github.scribejava.core.model.Response;
import com.github.scribejava.core.model.Verb;
import com.github.scribejava.core.oauth.OAuth20Service;

import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;

public final class AutomaticExample {

private static final String NETWORK_NAME = "Automatic";
private static final String PROTECTED_RESOURCE_URL = "https://api.automatic.com/user/me/";

private AutomaticExample() {
}

public static void main(String... args) throws IOException, InterruptedException, ExecutionException {
// Replace these with your client id and secret
final String clientId = "your client id";
final String clientSecret = "your client secret";
final String secretState = "secret" + new Random().nextInt(999_999);
final OAuth20Service service = new ServiceBuilder(clientId)
.apiSecret(clientSecret)
.state(secretState)
.callback("http://www.example.com/oauth_callback/")
.scope("scope:user:profile").debug()
.build(AutomaticAPI.instance());
final Scanner in = new Scanner(System.in, "UTF-8");

System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
System.out.println();

// Obtain the Authorization URL
System.out.println("Fetching the Authorization URL...");
final String authorizationUrl = service.getAuthorizationUrl();
System.out.println("Got the Authorization URL!");
System.out.println("Now go and authorize ScribeJava here:");
System.out.println(authorizationUrl);
System.out.println("And paste the authorization code here");
System.out.print(">>");
final String code = in.nextLine();
System.out.println();

System.out.println("And paste the state from server here. We have set 'secretState'='" + secretState + "'.");
System.out.print(">>");
final String value = in.nextLine();
if (secretState.equals(value)) {
System.out.println("State value does match!");
} else {
System.out.println("Ooops, state value does not match!");
System.out.println("Expected = " + secretState);
System.out.println("Got = " + value);
System.out.println();
}

// Trade the Authorization Code for the Access Token
System.out.println("Trading the Authorization Code for an Access Token...");
final OAuth2AccessToken accessToken = service.getAccessToken(code);
System.out.println("Got the Access Token!");
System.out.println("(The raw response looks like this: " + accessToken.getRawResponse() + "')");
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...");
final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
service.signRequest(accessToken, request);
final Response response = service.execute(request);
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 ScribeJava! :)");
}
}

0 comments on commit 7228206

Please sign in to comment.