2015-10-07 75 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主體中,因此我更改了代碼,現在授權代碼無效,因此我認爲這是進步。現在嘗試你的改變的解決方案。 –