forked from scribejava/scribejava
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Sriram <[email protected]>
- Loading branch information
Showing
3 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
scribejava-apis/src/main/java/com/github/scribejava/apis/AutomaticAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
scribejava-apis/src/test/java/com/github/scribejava/apis/examples/AutomaticExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! :)"); | ||
} | ||
} |