Skip to content

Instantly share code, notes, and snippets.

@nomoney4me
Created July 2, 2017 05:28
Show Gist options
  • Save nomoney4me/e0b547b810a4fd410649af2741a05135 to your computer and use it in GitHub Desktop.
Save nomoney4me/e0b547b810a4fd410649af2741a05135 to your computer and use it in GitHub Desktop.
require('dotenv').config()
var express = require('express')
, app = express()
, bodyParser = require('body-parser')
, fetch = require('node-fetch')
, cookieParser = require('cookie-parser')
, Promise = require('bluebird')
const {URLSearchParams} = require('url');
app.use(cookieParser())
function getToken() {
const params = new URLSearchParams({
'grant_type': 'client_credentials',
'client_id': process.env.CLIENTID,
'client_secret': process.env.CLIENTSECRET
})
return fetch('https://api.yelp.com/oauth2/token', {
method:'POST',
headers: {
'Content-Type':'application/x-www-form-urlencoded'
},
body: params
}).then(res => res.json())
}
app.get('/search', (req, res) => {
Promise.try(() => {
if(res.cookie.Token) {
return res.cookie.Token
}else {
return getToken()
}
}).then((token) => {
var search_params = new URLSearchParams({
'term':'food',
'location':'',
'latitude':process.env.LATITUDE, //using a sample location (will be replaced with user's current location)
'longitude':process.env.LONGITUDE //using a sample location (will be replaced with user's current location)
})
return fetch('https://api.yelp.com/v3/businesses/search', {
method:'get',
headers: {
'Authorization': 'Bearer '+token.access_token
//'Content-Type':'application/x-www-form-urlencoded'
},
body: search_params
}).then(res => res.json())
}).then((data) => {
console.log(data)
})
})
app.listen(4001)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment