Skip to content

Commit cc4b6f0

Browse files
Bill Tarrkullfar
authored andcommitted
Add OAuth2.0 for Yahoo APIs with example.
1 parent 6f0d0ab commit cc4b6f0

2 files changed

Lines changed: 113 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.github.scribejava.apis;
2+
3+
import com.github.scribejava.apis.openid.OpenIdJsonTokenExtractor;
4+
import com.github.scribejava.core.builder.api.DefaultApi20;
5+
import com.github.scribejava.core.extractors.TokenExtractor;
6+
import com.github.scribejava.core.model.OAuth2AccessToken;
7+
8+
public class YahooApi20 extends DefaultApi20 {
9+
10+
protected YahooApi20() {
11+
}
12+
13+
private static class InstanceHolder {
14+
private static final YahooApi20 INSTANCE = new YahooApi20();
15+
}
16+
17+
public static YahooApi20 instance() {
18+
return InstanceHolder.INSTANCE;
19+
}
20+
21+
@Override
22+
public String getAccessTokenEndpoint() {
23+
return "https://api.login.yahoo.com/oauth2/get_token";
24+
}
25+
26+
@Override
27+
protected String getAuthorizationBaseUrl() {
28+
return "https://api.login.yahoo.com/oauth2/request_auth";
29+
}
30+
31+
@Override
32+
public TokenExtractor<OAuth2AccessToken> getAccessTokenExtractor() {
33+
return OpenIdJsonTokenExtractor.instance();
34+
}
35+
36+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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

Comments
 (0)