0

我正在使用 PHP 的 fitbit api,我现在试图简单地回显 json 数据,但我收到的 JSON 数据是一个无效的请求错误。到目前为止的工作流程是这样的。我首先转到http://www.plas.nyc/TEST/fitbit.php这是下面的代码,单击 LOGIN 按钮会将我重定向到 fitbit 站点,然后返回到我的站点,并在 url 中附加代码: http ://www.plas.nyc/TEST/fitbit.php?code=cc8462bcde166d20517fc099b8ea9c994738ac59这很好,但坏的部分是在我的 var_dump 中收到的 JSON 是一个错误,它会将其射入 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

    }

    ?>
4

1 回答 1

0

我错过了一个授权标头,它也需要一个 base64 字符串

$auth_header = array("Authorization: Basic " . base64_encode(client_id.":".client_secret));

将此添加到我的标题使我能够在这里解决此问题。

于 2015-12-08T16:19:03.957 回答