使用PHP cURL的Fitbit oAuth-请求令牌返回空白屏幕
我试图使用FitbitAPI为“请求令牌”方法发出一个cURL请求。问题是它只显示一个空白屏幕。然而,这个过程假设给我一个“访问令牌”,我可以使用它生成一个登录URL来验证用户身份并授予对我的应用程序的访问权 我知道有oAuth php类用于此,但我的服务器不支持oAuth,所以只能使用cURL。请帮忙!非常感谢使用PHP cURL的Fitbit oAuth-请求令牌返回空白屏幕,php,curl,oauth,fitbit,Php,Curl,Oauth,Fitbit,我试图使用FitbitAPI为“请求令牌”方法发出一个cURL请求。问题是它只显示一个空白屏幕。然而,这个过程假设给我一个“访问令牌”,我可以使用它生成一个登录URL来验证用户身份并授予对我的应用程序的访问权 我知道有oAuth php类用于此,但我的服务器不支持oAuth,所以只能使用cURL。请帮忙!非常感谢 <?php define('FITBIT_KEY', '<consumer key from fitbit website>'); define('FITBIT_SE
<?php
define('FITBIT_KEY', '<consumer key from fitbit website>');
define('FITBIT_SECRET', '<consumer secret from fitbit website>');
function buildBaseString($baseURI, $method, $params)
{
$r = array();
ksort($params);
foreach($params as $key=>$value){
$r[] = "$key=" . rawurlencode($value);
}
return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r)); //return complete base string
}
function buildAuthorizationHeader($oauth)
{
$r = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key=>$value)
$values[] = "$key=\"" . rawurlencode($value) . "\"";
$r .= implode(', ', $values);
return $r;
}
$url = "http://api.fitbit.com/oauth/request_token";
$oauth = array( 'oauth_consumer_key' => FITBIT_KEY,
'oauth_consumer_secret' => FITBIT_SECRET,
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => time(),
'oauth_version' => '1.0',
'oauth_callback' => 'http://h1.servy.net/zerocuisine/index.php');
$base_info = buildBaseString($url, 'GET', $oauth);
$composite_key = rawurlencode(FITBIT_SECRET) . '&';
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
$fitbit_data = json_decode($json);
echo '<pre>'.print_r($fitbit_data,true).'</pre>'; ?>
这里有两个问题会导致出现空白屏幕。您在$url中使用了http而不是httpsfitbit令牌请求需要https。您还在一个非json数组的字符串上运行json_decode。$json中的数据是http正文响应中的字符串 下面是我的详细介绍,以使其完全工作 而不是:
$url = "https://api.fitbit.com/oauth/request_token";
使用:
同时删除:
echo '<pre>'.print_r($json,true).'</pre>'; ?>
更改:
echo '<pre>'. $json .'</pre>'; ?>
echo'。打印($json,true)。“”;?>
致:
echo“”$json。“”;?>
这将为您带来以下成功响应:
oauth_token=93bf6ded129fcfa2b687ce6e625477af和oauth_token_secret=c9cc547ca6dd484f73763e23b0987121和oauth_callback_confirm=true
显示到浏览器中
另外,请确保将其重新制作为Oauth 2.0。OAuth 1.0a支持将于2016年4月12日从Fitbit Web API中删除
echo '<pre>'.print_r($json,true).'</pre>'; ?>
echo '<pre>'. $json .'</pre>'; ?>
<pre>oauth_token=93bf6ded129fcfa2b687ce6e625477af&oauth_token_secret=c9cc547ca6dd484f73763e23b0987121&oauth_callback_confirmed=true</pre>