“errorType”:“invalid_client”,“message”:“无效的授权标头格式

时间:2017-08-23 11:19:20

标签: php curl access-token fitbit

我正在尝试使用api从fitbit数据库获取用户详细信息。我不确定我的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);

1 个答案:

答案 0 :(得分:3)

我得到了解决方案,即我们可以从哪里获得访问令牌。这就是我获取需求数据的方式

$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.
}
相关问题