1

我正在使用以下 php 脚本来访问 Fitbit API。问题是当我在本地主机上运行脚本时它工作正常,但是当我在远程服务器上运行它时,它给出了以下异常:

    OAuthException Object ( [message:protected] => making the request failed (dunno why)
    [string:Exception:private] => [code:protected] => -1 [file:protected] => 
    /net/www/example.com/htdocs/exampleFitbit.php [line:protected] => 41 
    [trace:Exception:private] => Array ( [0] => Array ( [file] => 
   /net/www/example.com/htdocs/exampleFitbit.php [line] => 41 [function] => getRequestToken
   [class] => OAuth [type] => -> [args] => Array ( [0] => http://api.fitbit.com/oauth/request_token [1] 
   => http://www.example.com/exampleFitbit.php ) ) ) [previous:Exception:private] => 
   [lastResponse] => [debugInfo] => ) 

剧本:

 <?php

        $baseUrl = 'http://api.fitbit.com';
        $req_url = $baseUrl . '/oauth/request_token';
        $authurl = $baseUrl . '/oauth/authorize';
        $acc_url = $baseUrl . '/oauth/access_token';
        $conskey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
        $conssec = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
        $apiCall = "http://api.fitbit.com/1/user/-/profile.xml";

        session_start();

        if ( !isset($_GET['oauth_token']) && $_SESSION['state']==1 
             $_SESSION['state'] = 0;

        try 
        {

            $oauth = new               OAuth($conskey,$conssec,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_AUTHORIZATION);


            $oauth->enableDebug();

            if ( $_SESSION['state'] == 0 ) 
            {

                $request_token_info = $oauth->getRequestToken($req_url,    'http://example.com/exampleFitbit.php');

                $_SESSION['secret'] = $request_token_info['oauth_token_secret'];
                $_SESSION['state'] = 1;

                header('Location: '.$authurl.'?oauth_token='.$request_token_info['oauth_token']);
                exit;
            } 
            else if ( $_SESSION['state']==1 ) 
            {

                $oauth->setToken($_GET['oauth_token'],$_SESSION['secret']);
                $access_token_info = $oauth->getAccessToken($acc_url);

                $_SESSION['state'] = 2;
                $_SESSION['token'] = $access_token_info['oauth_token'];
                $_SESSION['secret'] = $access_token_info['oauth_token_secret'];
            } 


            $oauth->setToken($_SESSION['token'],$_SESSION['secret']);


            $oauth->fetch($apiCall);

            $response = $oauth->getLastResponse();

            $xml = simplexml_load_string($response);
            print_r($xml);
        } 
        catch( OAuthException $E ) 
        {
            print_r($E);
        }
    ?>

如果有任何资源/教程/帮助/提示,​​请分享。

4

1 回答 1

0

您可以尝试在远程主机上使用 pcap/wireshark 来捕获网络流量,但我怀疑如果您不是 root,这将不起作用。

因此,您唯一的解决方案是向 fitbit 支持寻求帮助。

除此之外,您可以尝试通过 wget 或 curl 简单地获取 profile.xml 文件,看看是否可行。如果不是,则可能是防火墙问题。

于 2013-11-16T08:10:21.947 回答