Menu

Usage

Chester (Carlos D. Nascimento) Vanessa Sabino
There is a newer version of this page. You can find it here.

Using the library is pretty straighforward:

Registering your application on Yahoo!

The first thing to do is to register an oAuth application with Yahoo. It's free, and they will give you a consumer key and consumer secret for each of your applications.

Geting an Api object

All your interaction with the librar is done by this object, so you need to get one before doing any of the operations below. Use some code like this:

Api api = ApiFactory.getApiInstance(
    "your yahoo consumer key",
    "your yahoo consumer secret",
    "YOURAPPBASEURL/simpleyqlcallback/",
    false, null);

Replace

YOURAPPURL

with your application's base URL (ex.:

http://mytomcatserver.com/myjar/

)

Asking Authorization

Before you can call Yahoo APIs on behalf of an user, he must authorize you. It means that on an user's first acces, you should redirect him to Yahoo by issuing the following call (under a servlet or JSP):

    api.askAuthorization(request, response, "RETURNURL");

This call will redirect him, so it should be the last command on the servlet. After he gets authorized, he will be sent back to the

RETURNURL

(which must be on the domain that you registered your key on). This URL will receive an

authdata

string parameter. Store it on your database alongside your user's data.

Calling the API

  • Whenever you need to call the Y! api in behalf of an user, call:
    QueryResult qr = api.query("SOME YQL", authdata);
    

Here,

authdata

is the string you stored on the database on the previous step, and

SOME YQL

with your query (e.g.:

select * from social.profile where guid=me

). The

QueryResult

object's

getText()

method will return the result of the call. There you go - but there is a last catch:

Updating your Token

Unfortunately, Yahoo expires the access token (part of the

authdata

) every hour. In theory, you don't need to worry with that, because the library will refresh the token and retry the call automatically, but you should store the new token on the database whenever it changes (the

QueryRsult

also contains the new authdata).

So after issuing a query, you should have something in the lines of:

    if (!qr.getAuthData().equals(authdata)) {
        // replace the authdata on the database with qr.getAuthData()
        ..
    }

That's it!