Skip to content

Commit 4f48736

Browse files
committed
Merge branch 'feature/asana-integration' of https://github.com/joestazak/scribejava into joestazak-feature/asana-integration
2 parents 1f02491 + b29892c commit 4f48736

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

README.md

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

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

55+
* Asana (https://app.asana.com/) [example](https://github.com/scribejava/scribejava/blob/master/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/AsanaExample.java)
5556
* Automatic (https://www.automatic.com/) [example](https://github.com/scribejava/scribejava/blob/master/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/AutomaticExample.java)
5657
* AWeber (http://www.aweber.com/) [example](https://github.com/scribejava/scribejava/blob/master/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/AWeberExample.java)
5758
* Box (https://www.box.com/) [example](https://github.com/scribejava/scribejava/blob/master/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/Box20Example.java)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.github.scribejava.apis;
2+
3+
import com.github.scribejava.core.builder.api.DefaultApi20;
4+
import com.github.scribejava.core.oauth2.clientauthentication.ClientAuthentication;
5+
import com.github.scribejava.core.oauth2.clientauthentication.RequestBodyAuthenticationScheme;
6+
7+
public class Asana20 extends DefaultApi20 {
8+
protected Asana20() {
9+
}
10+
11+
private static class InstanceHolder {
12+
private static final Asana20 INSTANCE = new Asana20();
13+
}
14+
15+
public static Asana20 instance() {
16+
return InstanceHolder.INSTANCE;
17+
}
18+
19+
@Override
20+
public String getAccessTokenEndpoint() {
21+
return "https://app.asana.com/-/oauth_token";
22+
}
23+
24+
@Override
25+
protected String getAuthorizationBaseUrl() {
26+
return "https://app.asana.com/-/oauth_authorize";
27+
}
28+
29+
@Override
30+
public ClientAuthentication getClientAuthentication() {
31+
return RequestBodyAuthenticationScheme.instance();
32+
}
33+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)