无法从 fitbit oauth2 api 检索 JSON 数据
Unable to retrieve JSON data from fitbit oauth2 api
我正在使用带 PHP 的 fitbit api,我现在试图简单地回显 json 数据,但 JSON 数据是接收是一个无效的请求错误。到目前为止的工作流程是这样的。
我首先转到 http://www.plas.nyc/TEST/fitbit.php,这是下面的代码,点击登录按钮将我重定向到 fitbit 网站,然后 returns 返回我的网站,代码附加在 url 中:
http://www.plas.nyc/TEST/fitbit.php?code=cc8462bcde166d20517fc099b8ea9c994738ac59 这很好,但不好的部分是 JSON 在我的 var_dump 中收到是一个错误,它将此错误发送到 DOM 而不是我请求的正确数据:
string(210) "{"errors":[{"errorType":"invalid_request","message":"Authorization header required. Visit https://dev.fitbit.com/docs/oauth2 for more information on the Fitbit Web API authorization process."}],"success":false}"
我现在不确定哪里出了问题?下面是为此编写的代码。
<?php
/*--------------------- Fitbit API Keys ---------------------*/
// CONSTANTS
define("user_id",'X2222X'); // renamed to X2222X for this post
define("client_id",'X1111X'); //renamed to X1111X for this post.
define("response_type",'code');
define("scope", 'activity nutrition profile settings sleep social weight');
define("redirect_uri", 'http://www.plas.nyc/TEST/fitbit.php');
if($_GET['code']){
echo '<h1>success</h1>';
//loggedin
$code = $_GET['code'];
$url = "https://api.fitbit.com/oauth2/token";
$access_token_setttings = array(
'code' => $code,
'grant_type' => "authorization_code",
'client_id' => client_id,
'redirect_uri' => redirect_uri
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $access_token_setttings);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);// look into it more
$result = curl_exec($curl);
curl_close();
$results = json_decode($result, true);
var_dump($result);
} else { ?>
<!DOCTYPE html>
<html>
<head>
<title>FITBIT API -|- PLAS.NYC</title>
</head>
<body>
<a href="https://www.fitbit.com/oauth2/authorize?response_type=<?php echo response_type; ?>&client_id=<?php echo client_id; ?>&scope=<?php echo scope; ?>">LOGIN</a>
</body>
</html>
<?php
}
?>
我缺少授权 header,它还需要一个 base64 字符串
$auth_header = array("Authorization: Basic " . base64_encode(client_id.":".client_secret));
将此添加到我的 header 使我能够在这里解决这个问题。
我正在使用带 PHP 的 fitbit api,我现在试图简单地回显 json 数据,但 JSON 数据是接收是一个无效的请求错误。到目前为止的工作流程是这样的。 我首先转到 http://www.plas.nyc/TEST/fitbit.php,这是下面的代码,点击登录按钮将我重定向到 fitbit 网站,然后 returns 返回我的网站,代码附加在 url 中: http://www.plas.nyc/TEST/fitbit.php?code=cc8462bcde166d20517fc099b8ea9c994738ac59 这很好,但不好的部分是 JSON 在我的 var_dump 中收到是一个错误,它将此错误发送到 DOM 而不是我请求的正确数据:
string(210) "{"errors":[{"errorType":"invalid_request","message":"Authorization header required. Visit https://dev.fitbit.com/docs/oauth2 for more information on the Fitbit Web API authorization process."}],"success":false}"
我现在不确定哪里出了问题?下面是为此编写的代码。
<?php
/*--------------------- Fitbit API Keys ---------------------*/
// CONSTANTS
define("user_id",'X2222X'); // renamed to X2222X for this post
define("client_id",'X1111X'); //renamed to X1111X for this post.
define("response_type",'code');
define("scope", 'activity nutrition profile settings sleep social weight');
define("redirect_uri", 'http://www.plas.nyc/TEST/fitbit.php');
if($_GET['code']){
echo '<h1>success</h1>';
//loggedin
$code = $_GET['code'];
$url = "https://api.fitbit.com/oauth2/token";
$access_token_setttings = array(
'code' => $code,
'grant_type' => "authorization_code",
'client_id' => client_id,
'redirect_uri' => redirect_uri
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $access_token_setttings);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);// look into it more
$result = curl_exec($curl);
curl_close();
$results = json_decode($result, true);
var_dump($result);
} else { ?>
<!DOCTYPE html>
<html>
<head>
<title>FITBIT API -|- PLAS.NYC</title>
</head>
<body>
<a href="https://www.fitbit.com/oauth2/authorize?response_type=<?php echo response_type; ?>&client_id=<?php echo client_id; ?>&scope=<?php echo scope; ?>">LOGIN</a>
</body>
</html>
<?php
}
?>
我缺少授权 header,它还需要一个 base64 字符串
$auth_header = array("Authorization: Basic " . base64_encode(client_id.":".client_secret));
将此添加到我的 header 使我能够在这里解决这个问题。