Last active
January 2, 2017 14:29
-
-
Save 3mcd/445d45033e9f1eb7b40a2c44d2bae3ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add script tag to src="https://raw.githubusercontent.com/bettiolo/oauth-signature-js/master/dist/oauth-signature.min.js" | |
// to get oauthSignature function on window (global) | |
function randomString(length, chars) { | |
var result = ''; | |
for (var i = length; i > 0; --i) result += chars[Math.round(Math.random() * (chars.length - 1))]; | |
return result; | |
} | |
function jsonp(url, callback) { | |
var callbackName = 'jsonp_callback_' + Math.round(100000 * Math.random()); | |
window[callbackName] = function(data) { | |
delete window[callbackName]; | |
document.body.removeChild(script); | |
callback(data); | |
}; | |
var script = document.createElement('script'); | |
script.src = url + (url.indexOf('?') >= 0 ? '&' : '?') + 'callback=' + callbackName; | |
document.body.appendChild(script); | |
} | |
function toQueryString(json) { | |
return '?' + | |
Object.keys(json).map(function(key) { | |
return encodeURIComponent(key) + '=' + | |
encodeURIComponent(json[key]); | |
}).join('&'); | |
} | |
function yelpSearch(callback) { | |
var method = 'GET'; | |
var url = 'http://api.yelp.com/v2/search'; | |
var params = { | |
callback: 'angular.callbacks._0', | |
location: 'San+Francisc', | |
oauth_consumer_key: '', // CONSUMER KEY HERE | |
oauth_token: '', // TOKEN HERE | |
oauth_signature_method: "HMAC-SHA1", | |
oauth_timestamp: new Date().getTime(), | |
oauth_nonce: randomString(32, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'), | |
term: 'food' | |
}; | |
var consumerSecret = ''; // CONSUMER SECRET HERE | |
var tokenSecret = ''; // TOKEN SECRET HERE | |
var signature = oauthSignature.generate(method, url, params, consumerSecret, tokenSecret, { encodeSignature: false}); | |
params.oauth_signature = signature; | |
url += toQueryString(params); | |
jsonp(url, callback); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment