0 php curl oauth-2.0 fitbit php-curl
我正在尝试使用 php 和 cURL 向 Fitbit oauth 2.0 api 发出请求。我可以获得我的授权代码,但无法设法将代码交换为令牌。Fitbit api 文档说(https://dev.fitbit.com/docs/oauth2/#access-token-request)我需要发布代码、客户端 ID、重定向 uri 并将授予类型设置为“authorization_code”。
但是,我在打印响应时不断收到错误消息。
"errorType":"unsupported_grant_type","message":"不支持授权 grant_type。有关 Fitbit Web API 授权流程的更多信息,请访问https://dev.fitbit.com/docs/oauth2。"}],"成功":false}
对于我的生活,我无法弄清楚我在下面的代码做错了什么。有什么建议?
$code = $_GET['code'];
$url = 'https://api.fitbit.com/oauth2/token';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
'code=' . $code . '&' .
'client_id=' . $oauth2_client_id . '&' .
'redirect_uri=' . $oauth2_redirect . '&' .
'grant_type=authorization_code'
)
);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: Basic '. base64_encode($oauth2_client_id.':'.$oauth2_secret),
'Content-Type: application/x-www-form-urlencoded'
));
$response = curl_exec($curl);
print_r($response);
Run Code Online (Sandbox Code Playgroud)
您将 POST 参数连接成一个字符串,然后将其包含在一个数组中,但它们应该单独显示;这可以通过以下方式完成:
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query(array(
'code' => $code,
'client_id' => $oauth2_client_id,
'redirect_uri' => $oauth2_redirect,
'grant_type' => 'authorization_code'
)));
Run Code Online (Sandbox Code Playgroud)
请参阅:CURLOPT_POSTFIELDS 的 curl POST 格式
| 归档时间: |
|
| 查看次数: |
2726 次 |
| 最近记录: |