Skip to content

Commit c9401eb

Browse files
Merge pull request scribejava#354 from srelsner/aweber-example
AWeber api and example.
2 parents e7f578d + 6eb9208 commit c9401eb

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.scribe.builder.api;
2+
3+
import org.scribe.model.Token;
4+
5+
public class AWeberApi extends DefaultApi10a
6+
{
7+
private static final String AUTHORIZE_URL = "https://auth.aweber.com/1.0/oauth/authorize?oauth_token=%s";
8+
private static final String REQUEST_TOKEN_ENDPOINT = "https://auth.aweber.com/1.0/oauth/request_token";
9+
private static final String ACCESS_TOKEN_ENDPOINT = "https://auth.aweber.com/1.0/oauth/access_token";
10+
11+
@Override
12+
public String getAccessTokenEndpoint()
13+
{
14+
return ACCESS_TOKEN_ENDPOINT;
15+
}
16+
17+
@Override
18+
public String getRequestTokenEndpoint()
19+
{
20+
return REQUEST_TOKEN_ENDPOINT;
21+
}
22+
23+
@Override
24+
public String getAuthorizationUrl(Token requestToken)
25+
{
26+
return String.format(AUTHORIZE_URL, requestToken.getToken());
27+
}
28+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.scribe.examples;
2+
3+
import java.util.Scanner;
4+
5+
import org.scribe.builder.*;
6+
import org.scribe.builder.api.*;
7+
import org.scribe.model.*;
8+
import org.scribe.oauth.*;
9+
10+
public class AWeberExample
11+
{
12+
13+
//To get your consumer key/secret, and view API docs, see https://labs.aweber.com/docs
14+
private static final String ACCOUNT_RESOURCE_URL = "https://api.aweber.com/1.0/accounts/";
15+
16+
private static final String CONSUMER_KEY = "";
17+
private static final String CONSUMER_SECRET = "";
18+
19+
public static void main(String[] args)
20+
{
21+
OAuthService service = new ServiceBuilder()
22+
.provider(AWeberApi.class)
23+
.apiKey(CONSUMER_KEY)
24+
.apiSecret(CONSUMER_SECRET)
25+
.build();
26+
27+
Scanner in = new Scanner(System.in);
28+
29+
System.out.println("=== AWeber's OAuth Workflow ===");
30+
System.out.println();
31+
32+
// Obtain the Request Token
33+
System.out.println("Fetching the Request Token...");
34+
Token requestToken = service.getRequestToken();
35+
System.out.println("Got the Request Token!");
36+
System.out.println();
37+
38+
System.out.println("Now go and authorize Scribe here:");
39+
System.out.println(service.getAuthorizationUrl(requestToken));
40+
System.out.println("And paste the verifier here");
41+
System.out.print(">>");
42+
Verifier verifier = new Verifier(in.nextLine());
43+
System.out.println();
44+
45+
// Trade the Request Token and Verfier for the Access Token
46+
System.out.println("Trading the Request Token for an Access Token...");
47+
Token accessToken = service.getAccessToken(requestToken, verifier);
48+
System.out.println("Got the Access Token!");
49+
System.out.println("(if your curious it looks like this: " + accessToken + " )");
50+
System.out.println();
51+
52+
// Now let's go and ask for a protected resource!
53+
System.out.println("Now we're going to access a protected resource...");
54+
OAuthRequest request = new OAuthRequest(Verb.GET, ACCOUNT_RESOURCE_URL);
55+
service.signRequest(accessToken, request);
56+
Response response = request.send();
57+
System.out.println("Got it! Lets see what we found...");
58+
System.out.println();
59+
System.out.println(response.getBody());
60+
61+
System.out.println();
62+
System.out.println("Thats it man! Go and build something awesome with AWeber and Scribe! :)");
63+
}
64+
65+
}

0 commit comments

Comments
 (0)