Last active
February 27, 2024 20:32
-
-
Save mickyginger/f676c3befefe744684e5666f7d428287 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