2015-10-07 77 views
1

我一直在努力寻找适合Laravel的体面解决方案,但无济于事。适用于FitBit API的Laravel OAuth授权标头

有许多库在那里,有一点可能已经提供了一个Laravel-FitBit API OAuth集成,但尝试了超过15个不同的,而他们都没有工作,我卡住了。

阅读FitBit Documentation我看到,一旦您收到令牌,您必须将授权代码与访问令牌交换。要做到这一点,你需要发送授权标题是这样的:

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 

我一直在使用狂饮和其他几个库发送请求尝试,但没有人支持FitBit需要的格式。

我见过FitBit API集成的网站,所以必须有解决方案。

如果有人设法整合FitBit API,请让我知道我哪里出错了。

谢谢。

+0

的问题是,大多数REST客户建立关联数组头。您可能需要构建自己的curl请求以获取您的“访问令牌”,以便您可以使用该特定的头格式,但是,快速查看API,看起来应该能够使用类似guzzle的库所有其他呼叫。 –

+0

我很久没有使用CURL了,它的使用非常简单,您是否可以将我指向正确的方向,以及如何使用CURL获取访问令牌? –

回答

1

我没有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(); 
} 
+0

非常感谢,它几乎可以正常工作,但是我收到了这个错误响应。 http://imgur.com/tUoM4ov这很奇怪,因为我可以看到授权类型包含在请求中。有任何想法吗? –

+0

是的,似乎Fitbit有一些轻微混淆的文档。他们不希望您在头中发送请求参数,他们希望您将这些参数附加到请求url。我将更新我的答案以反映这一点,但是如果您不想使用此自定义卷曲方法,则现在应该可以使用一个类似guzzle的包。 –

+0

我注意到,在另一次重新阅读文档时,我看到参数都希望在CURL主体中,因此我更改了代码,现在授权代码无效,因此我认为这是进步。现在尝试你的改变的解决方案。 –