Laravel FitBit API 的 OAuth 授权 Header
Laravel OAuth Authorization Header for FitBit API
几天来我一直在努力为 Laravel 找到合适的解决方案,但无济于事。
有许多图书馆曾一度可能致力于提供 Laravel - FitBit API OAuth 集成,但是在尝试了超过 15 个不同的图书馆和 none 之后工作我卡住了。
阅读 FitBit Documentation 我发现一旦您收到令牌,您必须将授权代码与访问令牌交换。为此,您需要像这样发送授权 header:
POST https://api.fitbit.com/oauth2/token
Authorization: Basic Y2xpZW50X2lkOmNsaWVudCBzZWNyZXQ=
Content-Type: application/x-www-form-urlencoded
client_id=22942C&grant_type=authorization_code&redirect_uri=http%3A%2F%2Fexample.com%2Fcallback&code=1234567890
我曾尝试使用 guzzle 和其他一些库来发送请求,但其中 none 支持 FitBit 要求的格式。
我见过集成了 FitBit API 的网站,因此必须有解决方案。
如果有人成功集成了 FitBit API,请告诉我哪里出错了。
谢谢。
我没有 fitbit 账户,所以我无法测试它,它可能需要一些调整,但我会从类似的东西开始:
class FitbitConnection{
public function getToken($request_url, $client_id, $client_secret, $code, $redirect_uri){
// base64 encode the client_id and client_secret
$auth = base64_encode("{$client_id}:{$client_secret}");
// urlencode the redirect_url
$redirect_uri = urlencode($redirect_uri);
$request_url .= "?client_id={$client_id}&grant_type=authorization_code&redirect_uri={$redirect_uri}&code={$code}";
// Set the headers
$headers = [
"Authorization: Basic {$auth}",
"Content-Type: application/x-www-form-urlencoded",
];
// Initiate curl session
$ch = curl_init();
// Set headers
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Options (see: http://php.net/manual/en/function.curl-setopt.php)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_POST, 1);
// Execute the curl request and get the response
$response = curl_exec($ch);
// Throw an exception if there was an error with curl
if($response === false){
throw new Exception(curl_error($ch), curl_errno($ch));
}
// Get the body of the response
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$responseBody = substr($response, $header_size);
// Close curl session
curl_close($ch);
// Return response body
return $responseBody;
}
}
你应该注意到我已经注释掉了
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
如果您在本地主机上遇到 SSL 证书问题,您可以重新启用此选项,但不应在生产中使用它。
然后您可以执行以下操作:
try{
$fitbitConnection = new FitbitConnection();
$token_response = $fitbitConnection->getToken("https://api.fitbit.com/oauth2/token","22942C","client_secret","1234567890","http://www.example.com");
echo $token_response;
}catch(Exception $e){
// curl error
echo $e->getMessage();
}
几天来我一直在努力为 Laravel 找到合适的解决方案,但无济于事。
有许多图书馆曾一度可能致力于提供 Laravel - FitBit API OAuth 集成,但是在尝试了超过 15 个不同的图书馆和 none 之后工作我卡住了。
阅读 FitBit Documentation 我发现一旦您收到令牌,您必须将授权代码与访问令牌交换。为此,您需要像这样发送授权 header:
POST https://api.fitbit.com/oauth2/token
Authorization: Basic Y2xpZW50X2lkOmNsaWVudCBzZWNyZXQ=
Content-Type: application/x-www-form-urlencoded
client_id=22942C&grant_type=authorization_code&redirect_uri=http%3A%2F%2Fexample.com%2Fcallback&code=1234567890
我曾尝试使用 guzzle 和其他一些库来发送请求,但其中 none 支持 FitBit 要求的格式。
我见过集成了 FitBit API 的网站,因此必须有解决方案。
如果有人成功集成了 FitBit API,请告诉我哪里出错了。
谢谢。
我没有 fitbit 账户,所以我无法测试它,它可能需要一些调整,但我会从类似的东西开始:
class FitbitConnection{
public function getToken($request_url, $client_id, $client_secret, $code, $redirect_uri){
// base64 encode the client_id and client_secret
$auth = base64_encode("{$client_id}:{$client_secret}");
// urlencode the redirect_url
$redirect_uri = urlencode($redirect_uri);
$request_url .= "?client_id={$client_id}&grant_type=authorization_code&redirect_uri={$redirect_uri}&code={$code}";
// Set the headers
$headers = [
"Authorization: Basic {$auth}",
"Content-Type: application/x-www-form-urlencoded",
];
// Initiate curl session
$ch = curl_init();
// Set headers
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Options (see: http://php.net/manual/en/function.curl-setopt.php)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_POST, 1);
// Execute the curl request and get the response
$response = curl_exec($ch);
// Throw an exception if there was an error with curl
if($response === false){
throw new Exception(curl_error($ch), curl_errno($ch));
}
// Get the body of the response
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$responseBody = substr($response, $header_size);
// Close curl session
curl_close($ch);
// Return response body
return $responseBody;
}
}
你应该注意到我已经注释掉了
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
如果您在本地主机上遇到 SSL 证书问题,您可以重新启用此选项,但不应在生产中使用它。
然后您可以执行以下操作:
try{
$fitbitConnection = new FitbitConnection();
$token_response = $fitbitConnection->getToken("https://api.fitbit.com/oauth2/token","22942C","client_secret","1234567890","http://www.example.com");
echo $token_response;
}catch(Exception $e){
// curl error
echo $e->getMessage();
}