Pleaseeee! Help needed ASAP


#1

I am working on a fitness app using flutter framework using dart lang for my employer. I am attempting to use Fatsecret API for food and recipe database.

I am new to flutter and API’s in general (previously as a junior android developer, only worked with firebase). Now I am stuck at generating OAuth signature for fatsecret API.

Fatsecret [documentation] for signature generation.

I am trying to produce oauth_signature to use Fatsecret API, but getting an invalid signature error.

To create oauth_signature value the Documentation says:
> Use the HMAC-SHA1 signature algorithm as defined by the [RFC2104] to sign the request where text is the Signature Base String and key is the concatenated values of the Consumer Secret and Access Secret separated by a ‘&’ character (show ‘&’ even if Access Secret is empty as some methods do not require an Access Token).

> The calculated digest octet string, first base64-encoded per [RFC2045], then escaped using the [RFC3986] percent-encoding (%xx) mechanism is the oauth_signature.

Following code is what i have come up with to generate the signature,

String _generateSignature(String method, String url, Map<String, String> params) {
// sort the parameters
var sortedParams = SplayTreeMap.from(params);

// Concatenate the sortedParams with '&'
String concatenatedParams = sortedParams.keys.map((key) {
  return '$key=${sortedParams[key]}';
}).join('&');

// encode the sorted and concatenated params string
var encodedParams = Uri.encodeComponent(concatenatedParams);

var encodedUrl = Uri.encodeComponent(url);

String baseString = '$method&$encodedUrl&$encodedParams';
print(baseString);

String signingKey = '${Uri.encodeComponent(SHARED_SECRET)}&';

var hmac = Hmac(sha1, signingKey.codeUnits);

return base64Encode(hmac.convert(baseString.codeUnits).bytes);
}

When I make API call, it returns:

11-12 09:52:45.924 15525-15612/com.example.delete I/flutter: {error: {code: 8, message: Invalid signature: oauth_signature 'y81+JIzX/P+xNqOCYLgbrMtDV2I='}}

Please help me, guys…I am stuck at this for weeks!