过去几天我一直在尝试将我的应用程序与 uber 集成,但由于某些原因,在 oauth2 身份验证期间,我无法让 uber 给我一个有效的 token 。我可以获得访问代码,但是使用curl 时,无论我如何安排脚本,我似乎都无法获得访问 token 。这是我所拥有的:
<?php
echo $_GET['code']."<br>";
$token = curl_init();
$param = array(
'client_secret' => 'MY_SECRET',
'client_id' => 'MY_ID',
'grant_type' => 'authorization_code',
'code' => "{$_GET['code']}"
);
$postData = '';
foreach($param as $k => $v)
{
$postData .= $k . '='.urlencode($v).'&';
}
$postData = rtrim($postData, '&');
curl_setopt($token, CURLOPT_URL, 'https://login.uber.com/oauth/token');
curl_setopt($token, CURLOPT_HEADER, true);
curl_setopt($token, CURLOPT_RETURNTRANSFER, true);
curl_setopt($token, CURLOPT_POST, true);
curl_setopt($token, CURLOPT_POSTFIELDS, $postData);
$returned_token = curl_exec($token);
curl_close($token);
echo $returned_token;
?>
我已经仔细检查了我的 secret 和 ID,两者都是正确的。我可以看到每次我要获取访问代码时它都是独一无二的,我可以看到它在我重定向到的授权页面上回显,但无论我如何不断收到响应:
{"error": "access_denied"}
最佳答案
发生此错误的原因是:
- 您没有将redirect_uri参数发送到GET https://login.uber.com/oauth/v2/authorize端点,并且您也没有在 Uber Developers Dashboard 中设置重定向 URI用于您在 API 请求中使用的客户端应用程序。
Redirect URL
These URLs will be used during OAuth Authentication.
If no redirect URI is included with your request, the default URL will be used.
- 您发送的重定向网址无效(URI 的基础与 Uber 开发者控制面板中设置的重定向网址不同),
作为 GET https://login.uber.com/oauth/v2/authorize 的文档说:
redirect_uri (optional)
The URI we will redirect back to after an authorization by the resource owner.
The base of the URI must match the redirect_uri used during the registration of your application.
关于php - 无法使用有效访问 key 从 Uber API 获取 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30429399/