John Davidson

Garmin wellness-api - Invalid OAuth signature - PHP

0 comments
Message:


i have problem with generate oAuth signature to Garmin Wellness API.


    $oauth_nonce = 'OhJBVADBAC8';
$oauth_timestamp = '1614947339';
$date_from = 1607089161;
$date_to = 1614865161;

$url = 'https://apis.garmin.com/wellness-api/rest/backfill/activities?summaryStartTimeInSeconds=' . $date_from . '&summaryEndTimeInSeconds=' . $date_to;

$base_string = 'GET&' . rawurlencode($url) . '&' . rawurlencode('oauth_consumer_key=' . $oauth_consumer_key . '&oauth_nonce=' . $oauth_nonce . '&oauth_signature_method=' . $oauth_signature_method . '&oauth_timestamp=' . $oauth_timestamp . '&oauth_token=' . $oauth_token . '&oauth_version=' . $oauth_version . '&summaryStartTimeInSeconds=' . $date_from . '&summaryEndTimeInSeconds=' . $date_to);

$oauth_signature = hash_hmac("SHA1", $base_string, $oauth_consumer_secret, false);
$oauth_signature = rawurlencode(base64_encode(pack('H*', $oauth_signature)));

$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: OAuth oauth_consumer_key="' . $oauth_consumer_key . '",oauth_token="' . $oauth_token . '",oauth_signature_method="' . $oauth_signature_method . '",oauth_timestamp="' . $oauth_timestamp . '",oauth_nonce="' . $oauth_nonce . '",oauth_version="' . $oauth_version . '",oauth_signature="' . $oauth_signature . '"'
),
));

$response = curl_exec($curl);
var_dump($response);
curl_close($curl);

API returns "Invalid OAuth signature". When I put signature generated by Pastman, everything i s all right, so problem is in my way of generating signature.


Can you indicate my wrong?



My Answer:
It looks like there may be an issue with how you are constructing the base string for generating the OAuth signature.

One potential issue is that you are using `&` instead of `&` in the base string. The `&` should be replaced with `&` in the base string construction.

Additionally, make sure that the values of `$oauth_consumer_key`, `$oauth_signature_method`, `$oauth_token`, `$oauth_version`, and `$oauth_consumer_secret` are correctly set and match the values provided by Garmin Wellness API.

You can also try using a library like `oauth-php` or `oauth2-client-php` to handle the OAuth signature generation, as they provide a more robust and standardized way of generating OAuth signatures.

Lastly, double-check the parameters being passed in the URL and the OAuth header to ensure they match the required format and are correctly encoded.

By addressing these potential issues, you should be able to generate a valid OAuth signature for the Garmin Wellness API.

Rate this post

5 of 5 based on 5141 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us