PHP: oauth_signature resulting in "Invalid signature"

229 views
Skip to first unread message

Beckah Sheeler

unread,
Feb 28, 2017, 4:54:51 PM2/28/17
to FatSecret Platform API
I have tried multiple examples including the following and some of the questions in this group with no luck:
http://stackoverflow.com/questions/15892957/fatsecret-api-food-get-method-invalid-signature
https://github.com/EugeneHoran/Android-FatSecret-REST-API

The script I running is the following (based off of one of the answers):

<?php
$consumer_key
= "***********";
$secret_key
= "**********";
//Signature Base String
//<HTTP Method>&<Request URL>&<Normalized Parameters>
$base
= rawurlencode("GET")."&";
$base
.= "http%3A%2F%2Fplatform.fatsecret.com%2Frest%2Fserver.
api&"
;
//sort params by abc....necessary to build a correct unique signature
$params
= "method=foods.search&";
$params
.= "oauth_consumer_key=$consumer_key&"; // ur consumer key
$params
.= "oauth_nonce=123&";
$params
.= "oauth_signature_method=HMAC-SHA1&";
$params
.= "oauth_timestamp=".time()."&";
$params
.= "oauth_version=1.0&";
$params
.= "search_expression=".urlencode($_GET['pasVar']);
$params2
= rawurlencode($params);
$base
.= $params2;
//encrypt it!
$sig
= base64_encode(hash_hmac('sha1', $base, "$secret_key&",
true)); // replace xxx with Consumer Secret
//now get the search results and write them down
$url
= "http://platform.fatsecret.com/rest/server.api?".
$params
."&oauth_signature=".rawurlencode($sig);
//$food_feed = file_get_contents($url);
list
($output,$error,$info) = loadFoods($url);
echo
'<pre>';
if($error == 0){
   
if($info['http_code'] == '200')
        echo $output
;
   
else
       
die('Status INFO : '.$info['http_code']);
}
else
   
die('Status ERROR : '.$error);
function loadFoods($url)
{
       
// create curl resource
        $ch
= curl_init();
       
// set url
        curl_setopt
($ch, CURLOPT_URL, $url);
       
//return the transfer as a string
        curl_setopt
($ch, CURLOPT_RETURNTRANSFER, 1);
       
// $output contains the output string
        $output
= curl_exec($ch);
        $error
= curl_error($ch);
        $info
= curl_getinfo($ch);
       
// close curl resource to free up system resources
        curl_close
($ch);
       
return array($output,$error,$info);
}
?>

Which repeatedly results in 
Invalid signature: oauth_signature '***************'

What am I doing wrong in my code that makes it consistently generate a wrong signature?


John Rocha

unread,
Aug 31, 2022, 10:07:57 AM8/31/22
to FatSecret Platform API
Did you get it to work? im getting the same issue.
Reply all
Reply to author
Forward
0 new messages