-
-
Save imcodetolive/1ae1410905d9e243f22f010b5c140005 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
const crypto = require('crypto'); | |
const queryString = require('query-string'); | |
const urlencode = require('urlencode'); | |
const rp = require('request-promise'); | |
const method = 'GET'; | |
const url = 'http://platform.fatsecret.com/rest/server.api'; | |
const params = { | |
method: 'foods.search', | |
format: 'json', | |
search_expression: req.query.search, | |
oauth_signature_method: 'HMAC-SHA1', | |
oauth_consumer_key: process.env.FATSECRET_CLIENT_KEY, | |
oauth_nonce: (new Date()).getTime().toString(32), | |
oauth_timestamp: (new Date()).getTime(), | |
oauth_version: '1.0' | |
}; | |
const base = `${method}&${urlencode(url)}&${urlencode(queryString.stringify(params))}`; | |
const key = `${process.env.FATSECRET_CLIENT_SECRET}&`; | |
const hash = crypto.createHmac('sha1', key).update(base).digest('base64'); | |
Object.assign(params, { oauth_signature: hash }); | |
rp({ | |
method, | |
url, | |
qs: params, | |
json: true | |
}) | |
.then(response => console.log(response)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment