& quot; errorType & quot;: & quot; invalid_client & quot ;, & quot; message & quot;: & quot; неверный формат заголовка авторизации

Я пытаюсь получить информацию о пользователе из базы данных FitBit с помощью API. Я не уверен, каким должен быть мой access_token. Я попытался установить секретный ключ fitbit как access_token, но он выдает ту же ошибку. Заранее спасибо.

function generateRandomString($length = 32) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}

$_SESSION['access_token'] = generateRandomString();
$oauth_profile_header = ["Authorization: Bearer " . $_SESSION['access_token']];
$url = "https://api.fitbit.com/1/user/XXXXXX/profile.json";

$cu = curl_init($url);
curl_setopt($cu, CURLOPT_HTTPHEADER, $oauth_profile_header);
curl_setopt($cu, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cu, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($cu);
curl_close($cu);

2

Решение

Я получил решение, то есть откуда мы можем получить токен доступа. Вот как я получил необходимые данные

$client_id = 'XXXXXX';//Get it from fitbit app dashboard
$user_id = 'HHHHHH';//Get it from fitbit user profile page
echo "<a href='https://www.fitbit.com/oauth2/authorize?response_type=code&client_id=".$client_id."&redirect_uri=http://localhost/fitbitphp-master/&scope=weight'>Get User Weight Data</a>";
$client_Secret_key = 'e2cd10210bf8416064e84a2fc8230716';
$app_url = 'http://localhost/fitbitphp-master/';
$get_authorize_code_url ="https://www.fitbit.com/oauth2/authorize?response_type=code&client_id=228QD4&redirect_uri=http://localhost/fitbitphp-master/&scope=weight";
$get_authorize_code = curl_init($get_authorize_code_url);
curl_setopt($get_authorize_code, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($get_authorize_code, CURLOPT_SSL_VERIFYPEER, false);
$result_authorize_code = curl_exec($get_authorize_code);
curl_close($get_authorize_code);

if(isset($_GET['code'])){
$code= $_GET['code'];//It is authorisation code
//CURL Operation to get Access Token
$_SESSION['token'] = base64_encode($client_id.":".$client_Secret_key);//Create a temporary token
$oauth_get_access_tokan_header = ["Authorization: Basic " . $_SESSION['token']];//set temporary token in header
$get_access_tokan_url = "https://api.fitbit.com/oauth2/token?client_id=".$client_id."&grant_type=authorization_code&redirect_uri=".$app_url."&code=".$code;
$get_access_tokan = curl_init($get_access_tokan_url);
curl_setopt($get_access_tokan, CURLOPT_HTTPHEADER, $oauth_get_access_tokan_header);
curl_setopt($get_access_tokan, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($get_access_tokan, CURLOPT_POST, 1);//It should be post call
curl_setopt($get_access_tokan, CURLOPT_SSL_VERIFYPEER, false);
$result_access_tokan = curl_exec($get_access_tokan);
curl_close($get_access_tokan);
$obj_result_access_tokan = json_decode($result_access_tokan);
$array_access_tokan = (array)$obj_result_access_tokan;
$access_token = $array_access_tokan['access_token'];//output is access token

/*
*CURL operation to get fitbit data
*/

$get_fitbit_data_url =   "https://api.fitbit.com/1/user/$user_id/body/log/weight/date/2017-08-23/1m.json";
$oauth_get_fitbit_data_header = ["Authorization: Bearer " . $access_token];//set access token in header
$fitbit_data = curl_init($get_fitbit_data_url);
curl_setopt($fitbit_data, CURLOPT_HTTPHEADER, $oauth_get_fitbit_data_header);
curl_setopt($fitbit_data, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($fitbit_data, CURLOPT_SSL_VERIFYPEER, false);
$result_fitbit_data = curl_exec($fitbit_data);
curl_close($fitbit_data);
print_r($result_fitbit_data);//Required User Data.
}
3

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]