|
| 1 | +package com.github.scribejava.apis.examples; |
| 2 | + |
| 3 | +import java.util.Random; |
| 4 | +import java.util.Scanner; |
| 5 | + |
| 6 | +import com.github.scribejava.apis.StackExchangeApi; |
| 7 | +import com.github.scribejava.core.builder.ServiceBuilder; |
| 8 | +import com.github.scribejava.core.model.OAuthRequest; |
| 9 | +import com.github.scribejava.core.model.Response; |
| 10 | +import com.github.scribejava.core.model.Token; |
| 11 | +import com.github.scribejava.core.model.Verb; |
| 12 | +import com.github.scribejava.core.model.Verifier; |
| 13 | +import com.github.scribejava.core.oauth.OAuth20Service; |
| 14 | + |
| 15 | +public abstract class StackExchangeExample { |
| 16 | + |
| 17 | + private static final String NETWORK_NAME = "Stack Exchange"; |
| 18 | + private static final String PROTECTED_RESOURCE_URL = "https://api.stackexchange.com/2.2/me?site=stackoverflow&key="; |
| 19 | + |
| 20 | + public static void main(String... args) { |
| 21 | + // Replace these with your client id and secret |
| 22 | + final String clientId = "your client id"; |
| 23 | + final String clientSecret = "your client secret"; |
| 24 | + final String key = "your client key"; |
| 25 | + final String secretState = "secret" + new Random().nextInt(999_999); |
| 26 | + final OAuth20Service service = new ServiceBuilder() |
| 27 | + .apiKey(clientId) |
| 28 | + .apiSecret(clientSecret) |
| 29 | + .state(secretState) |
| 30 | + .callback("http://www.example.com/oauth_callback/") |
| 31 | + .build(StackExchangeApi.instance()); |
| 32 | + final Scanner in = new Scanner(System.in, "UTF-8"); |
| 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 authorizationUrl = service.getAuthorizationUrl(); |
| 40 | + System.out.println("Got the Authorization URL!"); |
| 41 | + System.out.println("Now go and authorize ScribeJava here:"); |
| 42 | + System.out.println(authorizationUrl); |
| 43 | + System.out.println("And paste the authorization code here"); |
| 44 | + System.out.print(">>"); |
| 45 | + final Verifier verifier = new Verifier(in.nextLine()); |
| 46 | + System.out.println(); |
| 47 | + |
| 48 | + System.out.println("And paste the state from server here. We have set 'secretState'='" + secretState + "'."); |
| 49 | + System.out.print(">>"); |
| 50 | + final String value = in.nextLine(); |
| 51 | + if (secretState.equals(value)) { |
| 52 | + System.out.println("State value does match!"); |
| 53 | + } else { |
| 54 | + System.out.println("Ooops, state value does not match!"); |
| 55 | + System.out.println("Expected = " + secretState); |
| 56 | + System.out.println("Got = " + value); |
| 57 | + System.out.println(); |
| 58 | + } |
| 59 | + |
| 60 | + // Trade the Request Token and Verfier for the Access Token |
| 61 | + System.out.println("Trading the Request Token for an Access Token..."); |
| 62 | + final Token accessToken = service.getAccessToken(verifier); |
| 63 | + System.out.println("Got the Access Token!"); |
| 64 | + System.out.println("(if your curious it looks like this: " + accessToken + " )"); |
| 65 | + System.out.println(); |
| 66 | + |
| 67 | + // Now let's go and ask for a protected resource! |
| 68 | + System.out.println("Now we're going to access a protected resource..."); |
| 69 | + final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL + key, service); |
| 70 | + service.signRequest(accessToken, request); |
| 71 | + final Response response = request.send(); |
| 72 | + System.out.println("Got it! Lets see what we found..."); |
| 73 | + System.out.println(); |
| 74 | + System.out.println(response.getCode()); |
| 75 | + System.out.println(response.getBody()); |
| 76 | + |
| 77 | + System.out.println(); |
| 78 | + System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)"); |
| 79 | + |
| 80 | + } |
| 81 | +} |
0 commit comments