Yelp API Google App Script OAuth

I am trying to use Google Apps Script to query Yelp Search Api and put the results in a spreadsheet. I'm having trouble calling to screech using this example as a model:

var consumerKey = "... register your app with Twitter ..."; var consumerSecret = "... register your app with Twitter ..."); var oauthConfig = UrlFetchApp.addOAuthService("twitter"); oauthConfig.setAccessTokenUrl("http://api.twitter.com/oauth/access_token"); oauthConfig.setRequestTokenUrl("http://api.twitter.com/oauth/request_token"); oauthConfig.setAuthorizationUrl("http://api.twitter.com/oauth/authorize"); oauthConfig.setConsumerKey(consumerKey); oauthConfig.setConsumerSecret(consumerSecret); // "twitter" value must match the argument to "addOAuthService" above. var options = { "oAuthServiceName" : "twitter", "oAuthUseToken" : "always" }; var url = "http://api.twitter.com/1/statuses/user_timeline.json"; var response = UrlFetchApp.fetch(url, options); var tweets = JSON.parse(response.getContentText()); // Handle tweets 

https://developers.google.com/apps-script/class_oauthconfig

This class has only methods for setting access token URLs that Yelp does not seem to provide. They directly provide a token and a secret token. I assumed that they would be installed as a consumer key and secret, but I did not find a way.

+4
source share
1 answer

The Yelp API uses oAuth1.0a to authorize and identify the calling API, not the end user that the application can use. This is not like a Twitter script where you should let your users log in. Therefore, you do not need access token URLs or other data. You can create all the necessary tokens to get started. Here's how your API console should look like once everything is configured (I messed up my keys by default) -

Yelp API Console

Now you need to make server-side API calls using UrlFetchApp and not use jQuery AJAX APIs, as this Yelp API does not seem to allow the use of CORS and JSONP in the HtmlService. Otherwise, you will get errors, as shown below in the console -

Chrome console

Finally, here is a sample code to get you started. I based them on my JavaScript sample -

  var auth = { consumerKey: "YOURKEY", consumerSecret: "YOURSECRET", accessToken: "YOURTOKEN", accessTokenSecret: "YOURTOKENSECRET", }; var terms = 'food'; var near = 'San+Francisco'; var accessor = { consumerSecret: auth.consumerSecret, tokenSecret: auth.accessTokenSecret }; var parameters = []; parameters.push(['term', terms]); parameters.push(['location', near]); parameters.push(['oauth_consumer_key', auth.consumerKey]); parameters.push(['oauth_consumer_secret', auth.consumerSecret]); parameters.push(['oauth_token', auth.accessToken]); var message = { 'action': 'http://api.yelp.com/v2/search', 'method': 'GET', 'parameters': parameters }; OAuth.setTimestampAndNonce(message); OAuth.SignatureMethod.sign(message, accessor); var parameterMap = OAuth.getParameterMap(message.parameters); parameterMap.oauth_signature = OAuth.percentEncode(parameterMap.oauth_signature) var url = OAuth.addToURL(message.action,parameterMap); var response = UrlFetchApp.fetch(url).getContentText(); var responseObject = Utilities.jsonParse(response); //have my JSON object, do whatever we want here, like add to spreadsheets 

I also added a couple GS script files with oAuth JS and SHA1 JS code contents from the provided links (just copy the insert into the new files in the script editor). However, if you consider yourself adventurous, you can also use the Utilities APIs to manually sign and encode the necessary oAuth parameters.

Hope this helps. I was able to get Yelp answers with all the samples provided.

+7
source

Source: https://habr.com/ru/post/1443123/


All Articles