|
| 1 | +package com.github.scribejava.apis.examples; |
| 2 | + |
| 3 | +import com.github.scribejava.apis.MeetupApi20; |
| 4 | +import com.github.scribejava.core.builder.ServiceBuilder; |
| 5 | +import com.github.scribejava.core.model.*; |
| 6 | +import com.github.scribejava.core.oauth.OAuth20Service; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.util.Random; |
| 10 | +import java.util.Scanner; |
| 11 | +import java.util.concurrent.ExecutionException; |
| 12 | + |
| 13 | +public class Meetup20Example { |
| 14 | + |
| 15 | + private static final String NETWORK_NAME = "Meetup"; |
| 16 | + private static final String PROTECTED_RESOURCE_URL = " https://api.meetup.com/self/groups"; |
| 17 | + |
| 18 | + private Meetup20Example() { |
| 19 | + } |
| 20 | + |
| 21 | + @SuppressWarnings("PMD.SystemPrintln") |
| 22 | + public static void main(String... args) throws IOException, InterruptedException, ExecutionException { |
| 23 | + // Replace these with your Meetup consumer id and secret |
| 24 | + final String consumerKey = "your consumer key"; |
| 25 | + final String consumerSecret = "your consumer secret"; |
| 26 | + final OAuth20Service service = new ServiceBuilder(consumerKey) |
| 27 | + .apiSecret(consumerSecret) |
| 28 | + .defaultScope("basic") // replace with desired scopes |
| 29 | + .callback("http://example.com/callback") // replace with appropriate URI for your consumer, |
| 30 | + // see https://www.meetup.com/meetup_api/auth/#redirect_uris |
| 31 | + .build(MeetupApi20.instance()); |
| 32 | + final Scanner in = new Scanner(System.in); |
| 33 | + |
| 34 | + System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ==="); |
| 35 | + System.out.println(); |
| 36 | + |
| 37 | + // Obtain the Authorization URL |
| 38 | + System.out.println("Fetching the Authorization URL..."); |
| 39 | + final String secretState = "secret" + new Random().nextInt(999_999); |
| 40 | + final String authorizationUrl = service.getAuthorizationUrl(secretState); |
| 41 | + System.out.println("Got the Authorization URL!"); |
| 42 | + System.out.println("Now go and authorize ScribeJava here:"); |
| 43 | + System.out.println(authorizationUrl); |
| 44 | + System.out.println("And paste the authorization code here"); |
| 45 | + System.out.print(">>"); |
| 46 | + final String code = in.nextLine(); |
| 47 | + System.out.println(); |
| 48 | + |
| 49 | + System.out.println("Trading the Authorization Code for an Access Token..."); |
| 50 | + final OAuth2AccessToken accessToken = service.getAccessToken(code); |
| 51 | + System.out.println("Got the Access Token!"); |
| 52 | + System.out.println("(The raw response looks like this: " + accessToken.getRawResponse() + "')"); |
| 53 | + System.out.println(); |
| 54 | + |
| 55 | + System.out.println("Now we're going to the user's groups...."); |
| 56 | + |
| 57 | + final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL); |
| 58 | + service.signRequest(accessToken, request); |
| 59 | + final Response response = service.execute(request); |
| 60 | + System.out.println(); |
| 61 | + System.out.println(response.getCode()); |
| 62 | + System.out.println(response.getBody()); |
| 63 | + |
| 64 | + System.out.println(); |
| 65 | + } |
| 66 | +} |
0 commit comments