无法从fitbit oauth2 api检索JSON数据 [英] Unable to retrieve JSON data from fitbit oauth2 api

查看:373
本文介绍了无法从fitbit oauth2 api检索JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用fitbit api与PHP,我试图简单回应json数据,但我收到的JSON数据是一个无效的请求错误。到目前为止,工作流程是这样的。
我首先goto http://www.plas.nyc/TEST/fitbit.php ,它是下面的代码,点击LOGIN按钮将我重定向到fitbit网站,然后返回到我的网站,并附加在url中的代码:
http://www.plas.nyc/TEST/fitbit.php?code=cc8462bcde166d20517fc099b8ea9c994738ac59 这很棒,但BAD PART是在我的var_dump中收到的JSON是一个错误,它将这个进入DOM,而不是我请求的正确数据:

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

    }

    ?>


推荐答案

我缺少一个授权头, string

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.

这篇关于无法从fitbit oauth2 api检索JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆