|
| 1 | +package com.github.scribejava.apis.examples; |
| 2 | + |
| 3 | +import com.github.scribejava.apis.Asana20; |
| 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.HashMap; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.Random; |
| 15 | +import java.util.Scanner; |
| 16 | +import java.util.concurrent.ExecutionException; |
| 17 | + |
| 18 | +public class AsanaExample { |
| 19 | + |
| 20 | + private static final String PROTECTED_RESOURCE_URL = "https://app.asana.com/api/1.0/users/me"; |
| 21 | + |
| 22 | + private AsanaExample() { |
| 23 | + |
| 24 | + } |
| 25 | + |
| 26 | + public static void main(String... args) throws IOException, InterruptedException, ExecutionException { |
| 27 | + final String apiKey = "your client id"; |
| 28 | + final String apiSecret = "your client secret"; |
| 29 | + final String secretState = "secret" + new Random().nextInt(999_999); |
| 30 | + final OAuth20Service service = new ServiceBuilder(apiKey) |
| 31 | + .apiSecret(apiSecret) |
| 32 | + .callback("https://localhost/") |
| 33 | + .build(Asana20.instance()); |
| 34 | + final Scanner in = new Scanner(System.in); |
| 35 | + |
| 36 | + // Obtain Auth URL |
| 37 | + System.out.println("Fetching the Authorication URL..."); |
| 38 | + System.out.println("Got the Authorization URL!"); |
| 39 | + final String authorizationUrl = service.getAuthorizationUrl(); |
| 40 | + System.out.println("Now go and authorize ScribeJava here:"); |
| 41 | + System.out.println(authorizationUrl); |
| 42 | + System.out.println("And paste the authorization code here"); |
| 43 | + System.out.print(">>"); |
| 44 | + final String code = in.nextLine(); |
| 45 | + System.out.println(); |
| 46 | + |
| 47 | + // Trade the Request Token and Verfier for the Access Token |
| 48 | + System.out.println("Trading the Request Token for an Access Token..."); |
| 49 | + OAuth2AccessToken accessToken = service.getAccessToken(code); |
| 50 | + System.out.println("Got the Access Token!"); |
| 51 | + System.out.println("(The raw response looks like this: " + accessToken.getRawResponse() + "')"); |
| 52 | + System.out.println(); |
| 53 | + |
| 54 | + System.out.println("And paste the state from server here. We have set 'secretState'='" + secretState + "'."); |
| 55 | + System.out.print(">>"); |
| 56 | + final String value = in.nextLine(); |
| 57 | + if (secretState.equals(value)) { |
| 58 | + System.out.println("State value does match!"); |
| 59 | + } else { |
| 60 | + System.out.println("Ooops, state value does not match!"); |
| 61 | + System.out.println("Expected = " + secretState); |
| 62 | + System.out.println("Got = " + value); |
| 63 | + System.out.println(); |
| 64 | + } |
| 65 | + |
| 66 | + System.out.println("Refreshing the Access Token..."); |
| 67 | + accessToken = service.refreshAccessToken(accessToken.getRefreshToken()); |
| 68 | + System.out.println("Refreshed the Access Token!"); |
| 69 | + System.out.println("(The raw response looks like this: " + accessToken.getRawResponse() + "')"); |
| 70 | + System.out.println(); |
| 71 | + |
| 72 | + System.out.println("Now we're going to access a protected resource..."); |
| 73 | + final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL); |
| 74 | + service.signRequest(accessToken, request); |
| 75 | + final Response response = service.execute(request); |
| 76 | + System.out.println("Got it! Lets see what we found..."); |
| 77 | + System.out.println(); |
| 78 | + System.out.println(response.getCode()); |
| 79 | + System.out.println(response.getBody()); |
| 80 | + |
| 81 | + System.out.println(); |
| 82 | + System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)"); |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments