Unable to retrieve JSON data from fitbit oauth2 api

谁说我不能喝 提交于 2019-12-23 04:33:27

问题


I'm working with the fitbit api with PHP, i trying to simply echo the json data at the moment but the JSON data i'm receiving is an invalid request error. So far the work flow is this. I first goto http://www.plas.nyc/TEST/fitbit.php which is the code below which upon clicking the LOGIN button redirects me to the fitbit site which then returns back to my site with code appended in the url: http://www.plas.nyc/TEST/fitbit.php?code=cc8462bcde166d20517fc099b8ea9c994738ac59 which is great but the BAD PART is the JSON being received in my var_dump is an error which shoots out this into the DOM instead of the proper data i requested:

 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}"

I'm unsure at the moment what could be wrong? Below is the code written to do this.

<?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:


I was missing an Authorization header which also required a base64 string

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

Adding this to my header enabled me to solve this problem here.



来源:https://stackoverflow.com/questions/33747765/unable-to-retrieve-json-data-from-fitbit-oauth2-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!