YUI 3.x Home -

YUI Library Examples: YQL Query: YQL 2-Legged oAuth

YQL Query: YQL 2-Legged oAuth

Loading data from YQL..

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:

  1. 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.

  1. 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.

  1. YUI({ gallery: 'gallery-2010.08.11-20-39' }).use('node', 'yql', 'gallery-oauth', function(Y) {
  2.  
  3. //Using with YQL oAuth Gallery module
  4. Y.oAuth.ready(function() {
  5.  
  6. Y.YQL('select title,abstract,url,source from search.news where query="endangered species"', function(r) {
  7. if (r.query.results) {
  8. var list = Y.Node.create('<ul></ul>');
  9. Y.each(r.query.results.result, function(v) {
  10. list.append(Y.Lang.sub('<li><a href="{url}" target="_blank" title="{abstract}">{title}</a> (<cite>{source}</cite>)</li>', v));
  11. });
  12. Y.one('#res').set('innerHTML', '<h3>News Headlines</h3>').append(list);
  13. }
  14. }, {
  15. key: 'xxx', //get key and secret from https://developer.apps.yahoo.com/projects
  16. secret: 'xxx', //get key and secret from https://developer.apps.yahoo.com/projects
  17. base: '://query.yahooapis.com/v1/yql?'
  18. });
  19.  
  20. });
  21.  
  22. });
  23.  
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?'
        });
 
    });
 
});
 

Copyright © 2011 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings