Last active
August 11, 2017 16:05
-
-
Save patrickleet/d32352ab2bb0d033f3b5 to your computer and use it in GitHub Desktop.
Meteor OAuth1Binding REST calls using oauth1 package (Yelp example)
This file contains hidden or 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
<template name='configureYelp'> | |
<div class="container"> | |
<div class="section"> | |
<h1>Yelp API Access Configuration</h1> | |
<p class="lead">Do not change if you don't know what you are doing.</p> | |
<p>{{currentConfig}}</p> | |
{{> quickForm schema=configureYelp doc=this id='configureYelp' type='method' meteormethod='configureYelp' buttonClasses="btn btn-primary" buttonContent="Configure Yelp"}} | |
</div> | |
</div> | |
</template> |
This file contains hidden or 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
Template.configureYelp.helpers({ | |
configureYelp: function() { | |
return Schema.configureYelp; | |
}, | |
currentConfig: function() { | |
Accounts.loginServiceConfiguration.findOne({service: 'yelp'}); | |
} | |
}); |
This file contains hidden or 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
// Using SimpleSchema | |
Schema.configureYelp = new SimpleSchema({ | |
consumerKey: { | |
type: String, | |
label: "Yelp Consumer Key" | |
}, | |
consumerSecret: { | |
type: String, | |
label: "Yelp Consumer Secret" | |
}, | |
accessToken: { | |
type: String, | |
label: "Yelp Access Token" | |
}, | |
accessTokenSecret: { | |
type: String, | |
label: "Yelp Access Token Secret" | |
} | |
}); | |
Meteor.methods({ | |
configureYelp: function(oauth_config) { | |
check(oauth_config, Schema.configureYelp); | |
Accounts.loginServiceConfiguration.remove({ | |
service: "yelp" | |
}); | |
Accounts.loginServiceConfiguration.insert({ | |
service: 'yelp', | |
consumerKey: oauth_config.consumerKey, | |
consumerSecret: oauth_config.consumerSecret, | |
accessToken: oauth_config.accessToken, | |
accessTokenSecret: oauth_config.accessTokenSecret | |
}); | |
} | |
}); |
This file contains hidden or 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
// FILE SHOULD BE ON SERVER ONLY | |
var getYelpOauthBinding = function(url) { | |
var config = Accounts.loginServiceConfiguration.findOne({service: 'yelp'}); | |
if (config) { | |
config.secret = config.consumerSecret; | |
var oauthBinding = new OAuth1Binding(config, url) | |
oauthBinding.accessToken = config.accessToken; | |
oauthBinding.accessTokenSecret = config.accessTokenSecret; | |
return oauthBinding; | |
} else { | |
throw new Meteor.Error(500, 'Yelp Not Configured'); | |
} | |
} | |
Meteor.methods({ | |
searchYelp: function(search, isCategory, latitude, longitude) { | |
this.unblock(); | |
console.log('Yelp search for userId: ' + this.userId + '(search, isCategory, lat, lon) with vals (', search, isCategory, latitude, longitude, ')'); | |
// Add REST resource to base URL | |
var url = yelp_base_url + 'search'; | |
var oauthBinding = getYelpOauthBinding(url); | |
// Build up query | |
var parameters = {}; | |
// Search term or categories query | |
if(isCategory) | |
parameters.category_filter = search; | |
else | |
parameters.term = search; | |
// Set lat, lon location, if available or default location | |
if(longitude && latitude) | |
parameters.ll = latitude + ',' + longitude; | |
else | |
parameters.location = 'New+York'; | |
// Results limited to 5 | |
parameters.limit = 5; | |
// Only return .data because that is how yelp formats its responses | |
return oauthBinding.get(url, parameters).data; | |
}, | |
yelpBusiness: function(id) { | |
this.unblock(); | |
console.log('Yelp business for userId: ' + this.userId + '(id, lat, lon) with vals (', id, ')'); | |
var url = yelp_base_url + 'business/' + id; | |
// Query OAUTH credentials (these are set manually) | |
var oauthBinding = getYelpOauthBinding(url); | |
// Only return .data because that is how yelp formats its responses | |
return oauthBinding.get(url).data; | |
} | |
}); |
This file contains hidden or 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
Meteor.publish('yelpconfig', function() { | |
return Accounts.loginServiceConfiguration.find({service: 'yelp'}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment