无法从fitbit oauth2 api检索JSON数据

时间:2015-11-17 01:43:14

标签: php json curl get fitbit

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

    }

    ?>

1 个答案:

答案 0 :(得分:0)

我错过了一个Authorization标头,它还需要一个base64字符串

      <Spinner
            android:layout_width="342dp"
            android:layout_height="48dp"
            android:background="@color/light_gray"
            android:layout_marginLeft="40dp" 
            android:id="@+id/spinner" />

将此添加到我的标题可以让我在这里解决这个问题。