YQL Query: YQL 2-Legged oAuth
This example pairs the YUI Gallery oAuth module with YQL Query to make an authenticated YQL request to Yahoo! Search's news search Web service..
Setting Up the Query
The easiest way to build a YQL query is by visiting the YQL Console. In this example we will be using the search.news
table. The YQL
statement that we are using looks like this:
SELECT title,abstract,url,source FROM search.news WHERE query="endangered species"
SELECT title,abstract,url,source FROM search.news WHERE query="endangered species"
Setting Up the YUI Instance
Now we need to create our YUI instance and tell it to load the yql
, node
and gallery-oauth
modules.
YUI().use('node', 'yql', 'gallery-oauth')
YUI().use('node', 'yql', 'gallery-oauth')
Making the Query
Now that we have a YUI instance with the yql
and gallery-oauth
modules, we need to ensure that the oAuth external libraries have been loaded.
In the code snippet below, you will notice that the YQL call is now wrapped in a call to Y.oAuth.ready
. This allows the oAuth module to load the required files and get them ready to be used.
Once that is loaded, you are free to make your YQL call by supplying the query, callback, key and secret.
Note: The key and secret in the code snippet below is invalid an should not be used. To get your very own YQL app key please visit the My Projects page on YDN.
YUI({ gallery: 'gallery-2010.08.11-20-39' }).use('node', 'yql', 'gallery-oauth', function(Y) { //Using with YQL oAuth Gallery module Y.oAuth.ready(function() { Y.YQL('select title,abstract,url,source from search.news where query="endangered species"', function(r) { if (r.query.results) { var list = Y.Node.create('<ul></ul>'); Y.each(r.query.results.result, function(v) { list.append(Y.Lang.sub('<li><a href="{url}" target="_blank" title="{abstract}">{title}</a> (<cite>{source}</cite>)</li>', v)); }); Y.one('#res').set('innerHTML', '<h3>News Headlines</h3>').append(list); } }, { key: 'xxx', //get key and secret from https://developer.apps.yahoo.com/projects secret: 'xxx', //get key and secret from https://developer.apps.yahoo.com/projects base: '://query.yahooapis.com/v1/yql?' }); }); });
YUI({ gallery: 'gallery-2010.08.11-20-39' }).use('node', 'yql', 'gallery-oauth', function(Y) { //Using with YQL oAuth Gallery module Y.oAuth.ready(function() { Y.YQL('select title,abstract,url,source from search.news where query="endangered species"', function(r) { if (r.query.results) { var list = Y.Node.create('<ul></ul>'); Y.each(r.query.results.result, function(v) { list.append(Y.Lang.sub('<li><a href="{url}" target="_blank" title="{abstract}">{title}</a> (<cite>{source}</cite>)</li>', v)); }); Y.one('#res').set('innerHTML', '<h3>News Headlines</h3>').append(list); } }, { key: 'xxx', //get key and secret from https://developer.apps.yahoo.com/projects secret: 'xxx', //get key and secret from https://developer.apps.yahoo.com/projects base: '://query.yahooapis.com/v1/yql?' }); }); });