This is sequence of my previous post but now with Yahoo.
I have downloaded Scribe example source code to get authenticated with Yahoo. But the default example was not working. After several hours of research, I found out that the author had forgotten to send a request to Yahoo APIs to get a GUID, which is in turn used to get user profile data. I have modified the code to fix that and here is the working code.
package org.scribe.examples;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Scanner;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.scribe.builder.ServiceBuilder;
import org.scribe.builder.api.YahooApi;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.model.Verifier;
import org.scribe.oauth.OAuthService;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class YahooExample {
private static String PROTECTED_RESOURCE_URL =
"http://social.yahooapis.com/v1/user/GUID/profile?format=json";
public static void main(String[] args) {
OAuthService service = new ServiceBuilder()
.provider(YahooApi.class)
.apiKey("dj0yJmk9TXZDWVpNVVdGaVFmJmQ9WVdrOWMweHZXbkZLT
khVbWNHbzlNVEl5TWprd05qUTJNZy0tJnM9Y29uc3VtZXJzZWNyZX
QmeD0wMw--")
.apiSecret("262be559f92a2be20c4c039419018f2b48cdfce9")
.build();
Scanner in = new Scanner(System.in);
System.out.println("=== Yahoo's OAuth Workflow ===");
System.out.println();
System.out.println("Fetching the Request Token...");
Token requestToken = service.getRequestToken();
System.out.println("Got the Request Token!");
System.out.println();
System.out.println("Now go and authorize Scribe here:");
System.out.println(service.getAuthorizationUrl(requestToken));
System.out.println("And paste the verifier here");
System.out.print(">>");
Verifier verifier = new Verifier(in.nextLine());
System.out.println();
System.out.println("Trading the Request Token for an Access Token...");
Token accessToken = service.getAccessToken(requestToken, verifier);
System.out.println("Got the Access Token!");
System.out.println("(if your curious it looks like this: "
+ accessToken + " )");
System.out.println();
System.out.println("Now we're going to access a protected resource...");
OAuthRequest request1 = new OAuthRequest(Verb.GET,
"http://social.yahooapis.com/v1/me/guid?format=xml");
service.signRequest(accessToken, request1);
Response response1 = request1.send();
System.out.println("Got it! Lets see what we found...");
System.out.println();
System.out.println(response1.getCode());
System.out.println(response1.getBody());
PROTECTED_RESOURCE_URL = PROTECTED_RESOURCE_URL.replaceAll("GUID",
parseYahooGUIDResposne(response1.getBody()));
System.out.println("PROTECTED_RESOURCE_URL "
+ PROTECTED_RESOURCE_URL);
System.out.println("Now we're going to access a protected resource...");
OAuthRequest request = new OAuthRequest(Verb.GET,
PROTECTED_RESOURCE_URL);
service.signRequest(accessToken, request);
request.addHeader("realm", "yahooapis.com");
Response response = request.send();
System.out.println("Got it! Lets see what we found...");
System.out.println();
System.out.println(response.getCode());
System.out.println(response.getBody());
System.out.println();
System.out
.println("Thats it man! Go and build something
awesome with Scribe! <img src="http:
images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /> ");
}
private static String parseYahooGUIDResposne(String data) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse(new ByteArrayInputStream
(data.getBytes()));
Element rootElement = dom.getDocumentElement();
return getGUID(rootElement);
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (SAXException se) {
se.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
return null;
}
private static String getGUID(Element guidEl) {
String name = getTextValue(guidEl, "value");
return name;
}
private static String getTextValue(Element ele, String tagName) {
String textVal = null;
NodeList nl = ele.getElementsByTagName(tagName);
if (nl != null && nl.getLength() > 0) {
Element el = (Element) nl.item(0);
textVal = el.getFirstChild().getNodeValue();
}
return textVal;
}
private static int getIntValue(Element ele, String tagName) {
return Integer.parseInt(getTextValue(ele, tagName));
}
}
In my next post, I will provide a sample web app where OAuth is used to authenticate with Yahoo/Google/Facebook.
Enjoy networking.