【问题标题】:Laravel OAuth Authorization Header for FitBit APIFitBit API 的 Laravel OAuth 授权标头
【发布时间】:2015-10-07 10:37:38
【问题描述】:

我一直在努力为 Laravel 找到一个合适的解决方案,但无济于事。

有很多库在某一时刻可能已经提供了 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

我尝试使用 guzzle 和其他一些库来发送请求,但它们都不支持 FitBit 所需的格式。

我见过集成了 FitBit API 的网站,因此必须有解决方案。

如果有人成功集成了 FitBit API,请告诉我哪里出错了。

谢谢。

【问题讨论】:

  • 问题在于大多数 REST 客户端使用关联数组构建标头。您可能需要构建自己的 curl 请求来获取您的 access token 以便您可以使用该特定的标头格式,但是,快速查看 API,看起来您应该能够为所有人使用像 guzzle 这样的库其他电话。
  • 我很久没用 CURL 了,它的用法很简单,你能指出我如何使用 CURL 获取访问令牌的正确方向吗?

标签: php laravel oauth oauth-2.0 fitbit


【解决方案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();
}

【讨论】:

  • 非常感谢,它几乎可以工作了,但是我收到了这个错误响应。 imgur.com/tUoM4ov 这很奇怪,因为我可以看到授权类型包含在请求中。有什么想法吗?
  • 是的,Fitbit 的文档似乎有些混乱。他们不希望您在标头中发送请求参数,他们希望您将这些参数附加到请求 url。我将更新我的答案以反映这一点,但如果您不想使用此自定义 curl 方法,您现在应该可以使用像 guzzle 这样的包。
  • 我注意到在再次阅读文档时,我看到参数都希望在 CURL 正文中,所以我更改了您的代码,现在授权代码无效,所以我认为这是进步。现在将尝试您更改后的解决方案。
  • 仍然收到message: "Authorization code invalid: 99179855123fab25cfe58e28c360a21a87f4c6dc" 错误。
  • 对于授权码,文档状态为The code is only valid for 10 minutes,因此如果您在超过 10 分钟前授权您的应用程序,您将需要通过再次执行授权过程来获取新的授权码。
猜你喜欢
  • 1970-01-01
  • 2012-07-20
  • 2012-06-19
  • 2016-07-31
  • 1970-01-01
  • 1970-01-01
  • 2013-04-19
  • 1970-01-01
  • 2020-09-22
相关资源
最近更新 更多