Skip to content

Commit 7228206

Browse files
committed
Added Automatic OAuth2 API
Signed-off-by: Sriram <sri.kmb@gmail.com>
1 parent dc54b59 commit 7228206

3 files changed

Lines changed: 142 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ ScribeJava support out-of-box several HTTP clients:
4040

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

43+
* Automatic (http://www.automatic.com/)
4344
* AWeber (http://www.aweber.com/)
4445
* Box (https://www.box.com/)
4546
* Dataporten (https://docs.dataporten.no/)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.github.scribejava.apis;
2+
3+
import com.github.scribejava.core.builder.api.ClientAuthenticationType;
4+
import com.github.scribejava.core.builder.api.DefaultApi20;
5+
import com.github.scribejava.core.extractors.OAuth2AccessTokenExtractor;
6+
import com.github.scribejava.core.extractors.OAuth2AccessTokenJsonExtractor;
7+
import com.github.scribejava.core.extractors.TokenExtractor;
8+
import com.github.scribejava.core.model.OAuth2AccessToken;
9+
import com.github.scribejava.core.model.Verb;
10+
11+
public class AutomaticAPI extends DefaultApi20 {
12+
13+
private static final String AUTHORIZE_URL = "https://accounts.automatic.com/oauth/authorize";
14+
private static final String REFRESH_TOKEN_ENDPOINT = "https://accounts.automatic.com/oauth/refresh_token";
15+
private static final String ACCESS_TOKEN_ENDPOINT = "https://accounts.automatic.com/oauth/access_token";
16+
17+
protected AutomaticAPI() {
18+
}
19+
20+
private static class InstanceHolder {
21+
private static final AutomaticAPI INSTANCE = new AutomaticAPI();
22+
}
23+
24+
public static AutomaticAPI instance() {
25+
return InstanceHolder.INSTANCE;
26+
}
27+
28+
@Override
29+
public Verb getAccessTokenVerb() {
30+
return Verb.POST;
31+
}
32+
33+
@Override
34+
public String getAccessTokenEndpoint() {
35+
return ACCESS_TOKEN_ENDPOINT;
36+
}
37+
38+
@Override
39+
public String getRefreshTokenEndpoint() {
40+
return REFRESH_TOKEN_ENDPOINT;
41+
}
42+
43+
@Override
44+
protected String getAuthorizationBaseUrl() {
45+
return AUTHORIZE_URL;
46+
}
47+
48+
@Override
49+
public TokenExtractor<OAuth2AccessToken> getAccessTokenExtractor() {
50+
return OAuth2AccessTokenJsonExtractor.instance();
51+
}
52+
53+
@Override
54+
public ClientAuthenticationType getClientAuthenticationType() {
55+
return ClientAuthenticationType.REQUEST_BODY;
56+
}
57+
58+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.github.scribejava.apis.examples;
2+
3+
import com.github.scribejava.apis.AutomaticAPI;
4+
import com.github.scribejava.core.builder.ServiceBuilder;
5+
import com.github.scribejava.core.model.OAuth2AccessToken;
6+
import com.github.scribejava.core.model.OAuthRequest;
7+
import com.github.scribejava.core.model.Response;
8+
import com.github.scribejava.core.model.Verb;
9+
import com.github.scribejava.core.oauth.OAuth20Service;
10+
11+
import java.io.IOException;
12+
import java.util.Random;
13+
import java.util.Scanner;
14+
import java.util.concurrent.ExecutionException;
15+
16+
public final class AutomaticExample {
17+
18+
private static final String NETWORK_NAME = "Automatic";
19+
private static final String PROTECTED_RESOURCE_URL = "https://api.automatic.com/user/me/";
20+
21+
private AutomaticExample() {
22+
}
23+
24+
public static void main(String... args) throws IOException, InterruptedException, ExecutionException {
25+
// Replace these with your client id and secret
26+
final String clientId = "your client id";
27+
final String clientSecret = "your client secret";
28+
final String secretState = "secret" + new Random().nextInt(999_999);
29+
final OAuth20Service service = new ServiceBuilder(clientId)
30+
.apiSecret(clientSecret)
31+
.state(secretState)
32+
.callback("http://www.example.com/oauth_callback/")
33+
.scope("scope:user:profile").debug()
34+
.build(AutomaticAPI.instance());
35+
final Scanner in = new Scanner(System.in, "UTF-8");
36+
37+
System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
38+
System.out.println();
39+
40+
// Obtain the Authorization URL
41+
System.out.println("Fetching the Authorization URL...");
42+
final String authorizationUrl = service.getAuthorizationUrl();
43+
System.out.println("Got the Authorization URL!");
44+
System.out.println("Now go and authorize ScribeJava here:");
45+
System.out.println(authorizationUrl);
46+
System.out.println("And paste the authorization code here");
47+
System.out.print(">>");
48+
final String code = in.nextLine();
49+
System.out.println();
50+
51+
System.out.println("And paste the state from server here. We have set 'secretState'='" + secretState + "'.");
52+
System.out.print(">>");
53+
final String value = in.nextLine();
54+
if (secretState.equals(value)) {
55+
System.out.println("State value does match!");
56+
} else {
57+
System.out.println("Ooops, state value does not match!");
58+
System.out.println("Expected = " + secretState);
59+
System.out.println("Got = " + value);
60+
System.out.println();
61+
}
62+
63+
// Trade the Authorization Code for the Access Token
64+
System.out.println("Trading the Authorization Code for an Access Token...");
65+
final OAuth2AccessToken accessToken = service.getAccessToken(code);
66+
System.out.println("Got the Access Token!");
67+
System.out.println("(The raw response looks like this: " + accessToken.getRawResponse() + "')");
68+
System.out.println();
69+
70+
// Now let's go and ask for a protected resource!
71+
System.out.println("Now we're going to access a protected resource...");
72+
final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
73+
service.signRequest(accessToken, request);
74+
final Response response = service.execute(request);
75+
System.out.println("Got it! Lets see what we found...");
76+
System.out.println();
77+
System.out.println(response.getCode());
78+
System.out.println(response.getBody());
79+
80+
System.out.println();
81+
System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)");
82+
}
83+
}

0 commit comments

Comments
 (0)