Yahoo oAuth using Scribe

It’s very common in this Cloud age to have a need to connect different applications in the internet. This can form a complete solution without reinventing the wheels.  How to you get access to end user’s data if such need arises? oAuth is probably what you will encounter these days for getting data access permissions from end users. That’s the reason why I was playing with Yahoo’s oAuth this weekend to get myself familiar with oAuth.

I was trying to get the end user’s contact information from his/her Yahoo account.  Scribe seemed to be quite easy to work with, and I had already used it a bit with Spring Social a few weeks back.  It is indeed a nice library, however, there were still several things I had to iron out to get it working with what I was trying to do.

The follow are the steps I went through,

  1. Register a standard Web-based project https://developer.apps.yahoo.com/projects with read access to Contacts.
  2. Add a html file with Yahoo’s provided name to my domain’s public_html folder (this is similar to Google’s process when you register a domain for your applications)
  3. Download scribe-java (git clone git://github.com/fernandezpablo85/scribe-java.git)
  4. Build scribe with Maven (mvn clean test), so I can use its YahooExample in test folder.
  5. Run Maven java:exec to test YahooExample
    mvn exec:java -Dexec.mainClass=org.scribe.examples.YahooExample -Dexec.classpathScope=test
    

Yahoo uses GUID for many of their rest API’s, so I modified YahooExample to extract GUID from the raw response, and then get list of contacts for the give user who had granted the permission.

package org.scribe.examples;

import java.util.Scanner;

import org.scribe.builder.*;
import org.scribe.builder.api.*;
import org.scribe.model.*;
import org.scribe.oauth.*;

public class YahooExample
{
  private static final String PROTECTED_RESOURCE_URL = "http://social.yahooapis.com/v1/user/%1s/contacts?format=json";

  public static void main(String[] args)
  {
    OAuthService service = new ServiceBuilder()
                                .provider(YahooApi.class)
                                .apiKey("dj0yJmk9UGFSbG00QlNTbVdtJmQ9WVdrOWNYaGhSMnhXTTJNbWNHbzlOekl6T1RjMk56WXkmcz1jb25zdW1lcnNlY3JldCZ4PWIx")
                                .apiSecret("f9f58f737954ff3889f17011a3afb86944870aca")
                                .build();
    Scanner in = new Scanner(System.in);

    System.out.println("=== Yahoo's OAuth Workflow by Barry===");
    System.out.println();

    // Obtain the Request Token
    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 open your browser to the following URL, and grant access when prompted." +
            "You will get a response page with a verification code since there's no callback specified. " +
            "Copy the verification code.");
    System.out.println(service.getAuthorizationUrl(requestToken));
    System.out.println("Paste the verification code here");
    System.out.print(">>");
    Verifier verifier = new Verifier(in.nextLine());
    System.out.println();

    // Trade the Request Token and Verfier for the Access Token
    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();

    // Now let's go and ask for a protected resource!
    System.out.println("Now we're going to access a protected resource...");
    OAuthRequest request = new OAuthRequest(Verb.GET,
            String.format(PROTECTED_RESOURCE_URL, getYahooGuid(accessToken.getRawResponse())));
    service.signRequest(accessToken, request);
    Response response = request.send();
    System.out.println("Got it! Lets see what we found...");
    System.out.println();
    System.out.printf("response code: %1s \n", response.getCode());
    System.out.println(response.getBody());

    System.out.println();
    System.out.println("Thats it man! Go and build something awesome with Scribe!");

  }

private static final String YAHOO_GUID = "xoauth_yahoo_guid";
private static final int GUID_LENGTH = 26;
private static String getYahooGuid(String response) {
    String yahoo_guid = null;
    int yahoo_guid_location = response.indexOf(YAHOO_GUID);
    if ( yahoo_guid_location > 0) {
        yahoo_guid = response.substring(yahoo_guid_location + YAHOO_GUID.length() + 1,
                yahoo_guid_location + YAHOO_GUID.length() + GUID_LENGTH + 1);
    }
    return yahoo_guid;
}
}

You will get a screen similar to the following when you run this class using mvn exec:java.

This entry was posted in Cloud and tagged , . Bookmark the permalink.

One Response to Yahoo oAuth using Scribe

  1. Pingback: Alexander6

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>