2015-05-24 233 views
2

经过10个小时的尝试各种fitbit php库我转向求助于stackoverflow。fitbit php身份验证不起作用

这不起作用:https://github.com/heyitspavel/fitbitphp

使用

$profile = $fitbit->getProfile(); 

与该库的回报

Fatal error: Uncaught exception 'FitBitException' with message 'Your Fitbit request failed. Code: 400' in /var/www/api/fitbitphp.php:324 Stack trace: #0 /var/www/api/addFitbit.php(22): FitBitPHP->getProfile() #1 {main} thrown in /var/www/api/fitbitphp.php on line 324 

这个fitbit网站上的图书馆,好像有很多人有这个问题。

public function getProfile() 
{ 
    $headers = $this->getHeaders(); 

    try { 
     $this->oauth->fetch($this->baseApiUrl . "user/" . $this->userId . "/profile." . $this->responseFormat, null, OAUTH_HTTP_METHOD_GET, $headers); 
    } catch (Exception $E) { 
    } 
    $response = $this->oauth->getLastResponse(); 
    $responseInfo = $this->oauth->getLastResponseInfo(); 
    if (!strcmp($responseInfo['http_code'], '200')) { 
     $response = $this->parseResponse($response); 

     if ($response) 
      return $response; 
     else 
      throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']); 
    } else { 
     throw new FitBitException($responseInfo['http_code'], 'Your Fitbit request failed. Code: ' . $responseInfo['http_code']); 
    } 
} 

这个我试过这里很好,但它不返回用户令牌或会话ID https://github.com/nostra999/fitbit-api

回答

1

也许错过了初始步骤,如在lib README文件描述(https://github.com/heyitspavel/fitbitphp/blob/master/README.md

简单的工作用法是:

<?php 
define('FITBIT_KEY', '777'); // The application key registered 
define('FITBIT_SECRET', '777'); // The application secret registered 

$fitbit = new FitBitPHP(FITBIT_KEY, FITBIT_SECRET); 
$fitbit->initSession('http://localhost:8080/fibit'); // callback URL 
$fitbit->getProfile(); 

同样来自Fitbit API文档: https://wiki.fitbit.com/display/API/API+Response+Format+And+Errors#APIResponseFormatAndErrors-Response

400错误的请求,其中任一端点不存在任何情况下,资源 路径参数是无效的,POST请求参数无效或不 认证头提供。这不包括无效具体 资源ID

如果这没有帮助,请提供您运行的完整代码,不只是

$外形= $ fitbit-> getProfile();