|
| 1 | +package com.github.scribejava.apis.examples; |
| 2 | + |
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | +import com.github.scribejava.apis.GeniusApi; |
| 6 | +import com.github.scribejava.core.builder.ServiceBuilder; |
| 7 | +import com.github.scribejava.core.model.OAuth2AccessToken; |
| 8 | +import com.github.scribejava.core.model.OAuthRequest; |
| 9 | +import com.github.scribejava.core.model.Response; |
| 10 | +import com.github.scribejava.core.model.Verb; |
| 11 | +import com.github.scribejava.core.oauth.OAuth20Service; |
| 12 | + |
| 13 | +public abstract class GeniusExample { |
| 14 | + |
| 15 | + private static final String NETWORK_NAME = "Genius"; |
| 16 | + private static final String PROTECTED_RESOURCE_URL = "https://api.genius.com/songs/378195"; |
| 17 | + |
| 18 | + public static void main(String... args) { |
| 19 | + // Replace these with your client id and secret |
| 20 | + final String clientId = "your client id"; |
| 21 | + final String clientSecret = "your client secret"; |
| 22 | + final String secretState = "100"; |
| 23 | + final OAuth20Service service = new ServiceBuilder() |
| 24 | + .apiKey(clientId) |
| 25 | + .apiSecret(clientSecret) |
| 26 | + .scope("me") |
| 27 | + .state(secretState) |
| 28 | + .callback("com.scribejavatest://callback") |
| 29 | + .userAgent("ScribeJava") |
| 30 | + .build(GeniusApi.instance()); |
| 31 | + |
| 32 | + System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ==="); |
| 33 | + System.out.println(); |
| 34 | + |
| 35 | + // Obtain the Authorization URL |
| 36 | + System.out.println("Fetching the Authorization URL..."); |
| 37 | + final String authorizationUrl = service.getAuthorizationUrl(); |
| 38 | + System.out.println("Got the Authorization URL!"); |
| 39 | + System.out.println("Now go and authorize ScribeJava here:"); |
| 40 | + System.out.println(authorizationUrl); |
| 41 | + System.out.println("And paste the authorization code here"); |
| 42 | + System.out.print(">>"); |
| 43 | + final String code; |
| 44 | + final String value; |
| 45 | + try (Scanner in = new Scanner(System.in, "UTF-8")) { |
| 46 | + code = in.nextLine(); |
| 47 | + System.out.println(); |
| 48 | + |
| 49 | + System.out.println("And paste the state from server here. We have set 'secretState'='" + secretState |
| 50 | + + "'."); |
| 51 | + System.out.print(">>"); |
| 52 | + value = in.nextLine(); |
| 53 | + } |
| 54 | + |
| 55 | + if (secretState.equals(value)) { |
| 56 | + System.out.println("State value does match!"); |
| 57 | + } else { |
| 58 | + System.out.println("Ooops, state value does not match!"); |
| 59 | + System.out.println("Expected = " + secretState); |
| 60 | + System.out.println("Got = " + value); |
| 61 | + System.out.println(); |
| 62 | + } |
| 63 | + |
| 64 | + // Trade the Request Token and Verifier for the Access Token |
| 65 | + System.out.println("Trading the Request Token for an Access Token..."); |
| 66 | + final OAuth2AccessToken accessToken = service.getAccessToken(code); |
| 67 | + System.out.println("Got the Access Token!"); |
| 68 | + System.out.println("(View Access Token contents: " + accessToken |
| 69 | + + ", 'rawResponse'='" + accessToken.getRawResponse() + "')"); |
| 70 | + System.out.println(); |
| 71 | + |
| 72 | + // Now let's go and ask for a protected resource! |
| 73 | + System.out.println("Accessing a protected resource..."); |
| 74 | + final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL, service); |
| 75 | + service.signRequest(accessToken, request); |
| 76 | + final Response response = request.send(); |
| 77 | + System.out.println("Got it! Viewing contents..."); |
| 78 | + System.out.println(); |
| 79 | + System.out.println(response.getCode()); |
| 80 | + System.out.println(response.getBody()); |
| 81 | + |
| 82 | + System.out.println(); |
| 83 | + System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)"); |
| 84 | + } |
| 85 | +} |
0 commit comments