Php 无法从fitbit oauth2 api检索JSON数据
我正在用PHP使用FitbitAPI,我试图简单地回显json数据,但我收到的json数据是无效的请求错误。到目前为止,工作流程是这样的。 我首先转到下面的代码,单击下面的“登录”按钮可将我重定向到fitbit站点,然后返回到我的站点,并在url中附加代码: 这很好,但不好的是,在我的var_dump中接收到的JSON是一个错误,它将此错误发送到DOM中,而不是我请求的正确数据:Php 无法从fitbit oauth2 api检索JSON数据,php,json,curl,get,fitbit,Php,Json,Curl,Get,Fitbit,我正在用PHP使用FitbitAPI,我试图简单地回显json数据,但我收到的json数据是无效的请求错误。到目前为止,工作流程是这样的。 我首先转到下面的代码,单击下面的“登录”按钮可将我重定向到fitbit站点,然后返回到我的站点,并在url中附加代码: 这很好,但不好的是,在我的var_dump中接收到的JSON是一个错误,它将此错误发送到DOM中,而不是我请求的正确数据: string(210) "{"errors":[{"errorType":"invalid_request","m
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
}
?>
我缺少一个授权标头,该标头也需要base64字符串
$auth_header = array("Authorization: Basic " . base64_encode(client_id.":".client_secret));
将此添加到我的标题使我能够在此处解决此问题