【问题标题】:I am getting error while trying to get profile details from Fitbit using api. What is the reason?尝试使用 api 从 Fitbit 获取个人资料详细信息时出现错误。是什么原因?
【发布时间】:2012-09-28 09:16:47
【问题描述】:

错误是,

stdClass Object
(
    [errors] => Array
        (
            [0] => stdClass Object
                (
                    [errorType] => oauth
                    [fieldName] => n/a
                    [message] => No Authorization header provided in the request. Each call to Fitbit API should be OAuth signed
                )

        )

)

我正在使用 Oauth 库。我已经拿到了用户令牌。

http://api.fitbit.com/1/user/23Q/profile.json?oauth_consumer_key=425e1234b8823e26485aa6&oauth_nonce=31f991c9b3e068c14adceddd9b862c0a&oauth_signature=R5oi4dHA6ztIIpdKheahYOy%2FeMQ%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1348821623&oauth_token=3405b4918d5d96a7b78885a98&oauth_version=1.0

我试过了……

$url = http://api.fitbit.com/1/user/23Q2SP/profile.json;

$header = 数组();
$header[] = "授权:OAuth 3405b496578885a98";

$header[] = "Accept: ";

$header[] = "Cache-Control: no-cache";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en_US";
$header[] = "Pragma: no-cache";   
$this->http_info = array();
$ci = curl_init();
/* Curl settings */
curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ci, CURLOPT_HTTPHEADER, $header);
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
curl_setopt($ci, CURLOPT_HEADER, FALSE);

curl_setopt($ci, CURLOPT_URL, $url);
$response = curl_exec($ci);

但同样的错误 {"errors":[{"errorType":"oauth","fieldName":"n/a","message":"请求中未提供授权标头。对 Fitbit API 的每次调用都应经过 OAuth 签名"}] }

【问题讨论】:

    标签: php oauth http-headers fitbit


    【解决方案1】:

    我没有使用 Fitbit API,但我读到 here 是,Fitbit 只允许通过授权标头进行 GET 请求,因此您可能需要设置类似的标头

    "Authorization: OAuth <ACCESS_TOKEN>" http://www.fitbit.com
    

    查看post,了解如何通过 CURL 设置标头,希望对您有所帮助。

    【讨论】:

    • 我用访问令牌试了一下。但是返回了同样的错误。我的示例请求是, GET /1/user/-/activities/date/2010-04-02.json HTTP/1.1 主机:api.fitbit.com 授权:OAuth oauth_consumer_key="fitbit-example-客户端应用程序”,组oauth_token = “8d3221fb072f31b5ef1b3bcfc5d8a27a”,oauth_signature_method = “HMAC-SHA1”,oauth_timestamp = “1270248088”,oauth_nonce = “515379974”,oauth_signature = “Gf5NUq1Pvg3DrtxHJyVaMXq4Foo%3D” oauth_version = “1.0” 我怎么能在添加此我标题?
    • 我猜你只是像Authorization: OAuth 8d3221fb072f31b5ef1b3bcfc5d8a27a一样传递oauth_token
    【解决方案2】:

    试试这个:

    req_url = 'http://api.fitbit.com/oauth/request_token';
    $authurl = 'http://api.fitbit.com/oauth/access_token';
    $acc_url = 'http://api.fitbit.com/oauth/authorize';
    
    $conskey = 'Dev key';
    $conssec = 'Dev secret';
    
    $token = 'User token';
    $secret = 'User secret';
    
    
    
    try {
    
        $oauth = new OAuth($conskey,$conssec,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_AUTHORIZATION);
        $oauth->enableDebug();
    
    
    
         $oauth->setToken($token,$secret);
    
    
          $oauth->fetch("http://api.fitbit.com/1/user/-/activities/date/2013-12-06.json");
        $response_info = $oauth->getLastResponseInfo();
    
      print_r($oauth->getLastResponse());
    
      } catch(OAuthException $E) {
      print_r($E);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-10
      • 2017-12-28
      • 2013-07-02
      • 2011-01-13
      • 1970-01-01
      • 2017-02-09
      • 1970-01-01
      • 2022-08-21
      相关资源
      最近更新 更多