|
| 1 | +package com.github.scribejava.apis.examples; |
| 2 | + |
| 3 | +import com.github.scribejava.apis.YahooApi20; |
| 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.Scanner; |
| 10 | +import java.util.concurrent.ExecutionException; |
| 11 | + |
| 12 | +/** |
| 13 | + * Flow as documented at https://developer.yahoo.com/oauth2/guide/flows_authcode |
| 14 | + * <ol> |
| 15 | + * <li>Create an application at https://developer.yahoo.com/apps/create/</li> |
| 16 | + * <li>Make sure the application has the API permission where your protected resource resides (Profiles, Fantasy Sports, etc)</li> |
| 17 | + * <li>get Client ID and Client Secret after registering your app</li> |
| 18 | + * </ol> |
| 19 | + */ |
| 20 | +public class Yahoo20Example { |
| 21 | + |
| 22 | + // Update PROTECTED_RESOURCE_URL to a secure URL only you could reach = your profile, or private fantasy sports |
| 23 | + private static final String PROTECTED_RESOURCE_URL |
| 24 | + = "https://baseball.fantasysports.yahoo.com/b1/ ADD TEAM HERE!!!!"; |
| 25 | + |
| 26 | + private Yahoo20Example() { |
| 27 | + } |
| 28 | + |
| 29 | + public static void main(String... args) throws IOException, InterruptedException, ExecutionException { |
| 30 | + |
| 31 | + // Add your personal information here |
| 32 | + final String clientId = "ADD CLIENT ID HERE!!!!"; |
| 33 | + final String clientSecret = "ADD SECRET HERE!!!!"; |
| 34 | + |
| 35 | + final OAuth20Service service = new ServiceBuilder(clientId) |
| 36 | + .apiSecret(clientSecret) |
| 37 | + .callback("oob") |
| 38 | + .build(YahooApi20.instance()); |
| 39 | + final Scanner in = new Scanner(System.in); |
| 40 | + |
| 41 | + System.out.println("=== Yahoo's OAuth Workflow ==="); |
| 42 | + System.out.println(); |
| 43 | + |
| 44 | + // Obtain the Request Token |
| 45 | + System.out.println("Fetching the Request Token..."); |
| 46 | + System.out.println("Got the Request Token!"); |
| 47 | + System.out.println(); |
| 48 | + |
| 49 | + System.out.println("Now go and authorize ScribeJava here:"); |
| 50 | + System.out.println(service.getAuthorizationUrl()); |
| 51 | + System.out.println("And paste the verifier here"); |
| 52 | + System.out.print(">>"); |
| 53 | + final String oauthVerifier = in.nextLine(); |
| 54 | + System.out.println(); |
| 55 | + |
| 56 | + // Trade the Request Token and Verfier for the Access Token |
| 57 | + System.out.println("Trading the Request Token for an Access Token..."); |
| 58 | + final OAuth2AccessToken accessToken = service.getAccessToken(oauthVerifier); |
| 59 | + System.out.println("Got the Access Token!"); |
| 60 | + System.out.println("(The raw response looks like this: " + accessToken.getRawResponse() + "')"); |
| 61 | + System.out.println(); |
| 62 | + |
| 63 | + // Now let's go and ask for a protected resource! |
| 64 | + System.out.println("Now we're going to access a protected resource..."); |
| 65 | + final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL); |
| 66 | + service.signRequest(accessToken, request); |
| 67 | + final Response response = service.execute(request); |
| 68 | + System.out.println("Got it! Lets see what we found..."); |
| 69 | + System.out.println(); |
| 70 | + System.out.println(response.getCode()); |
| 71 | + System.out.println(response.getBody()); |
| 72 | + |
| 73 | + System.out.println(); |
| 74 | + System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)"); |
| 75 | + |
| 76 | + } |
| 77 | +} |
0 commit comments