找到你要的答案

Q:Cannot Get Token from Uber API with Valid Access Key

Q:无法获得令牌从Uber API与有效的访问键

I've been trying for the past few days to integrate my app with uber, but for some reason during the oauth2 authentication I can't get uber to give me a valid token. I can get an access code but when using curl, I can't seem to get an access token, no matter how I arrange my script. Here's what I have:

<?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;

?>

I've double checked my secret and id, both are correct. I can see each time I'm going to get the access code it's something unique, I can see it echoed out on the authorization page I'm redirecting to, but no matter what I keep getting the response as:

{"error": "access_denied"}

我一直努力在过去的几天里将我的应用程序与尤伯杯,但出于某种原因,在oauth2认证我不能让Uber给我一个有效的令牌。我可以得到一个访问代码,但当使用卷曲,我似乎无法得到一个访问令牌,无论我如何安排我的脚本。这就是我所拥有的:

<?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;

?>

我已经检查了我的秘密和ID,两者都是正确的。我可以看到每次我去访问代码是独一无二的,我可以看到它回荡在我的授权页面重定向,但无论我怎么保持响应:

{"error": "access_denied"}
answer1: 回答1:

This error happens because:

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.

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.

这个错误发生是因为:

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.

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.

answer2: 回答2:

Works fine for me. Make sure the Content-Type header is correct. Some services will expect that. (e.g. Content-Type=x-www-form-urlencoded)

Also, can yo share how you are requesting the code? (e.g. which scope are you using when sending the Authorization request, etc).

我的作品罚款。确保内容类型标头正确。一些服务将期望。(例如内容类型=窗体-urlencoded)

此外,您可以分享您如何请求代码?(在发送授权请求时,您使用的范围是什么)。

answer3: 回答3:

I've also encountered this problem and it looks like I found the reason of it.

Try adding redirect_uri (the same as in app settings) to your request, so your params code should look like this:

$param = array(
'client_secret' => 'MY_SECRET',
'client_id' => 'MY_ID',
'redirect_uri' => 'MY_REDIRECT_URI',
'grant_type' => 'authorization_code',
'code' => "{$_GET['code']}"
);

That helped me, but I hope Uber developers will make this error more specific

我也遇到了这个问题,看起来我找到了它的原因。

尝试添加redirect_uri(在应用程序设置一样)你的要求,那么你的参数代码看起来应该是这样的:

$param = array(
'client_secret' => 'MY_SECRET',
'client_id' => 'MY_ID',
'redirect_uri' => 'MY_REDIRECT_URI',
'grant_type' => 'authorization_code',
'code' => "{$_GET['code']}"
);

帮助我,但是我希望Uber开发商会让这个错误更具体

answer4: 回答4:

It worked for me perfectly. My problem was that I had a difference between the Url Redirect on Dashboard app and The parameter that i send to the request. I fixed it and everything works fine

它完美地为我工作。我的问题是,我有一个区别的URL重定向仪表板应用程序和我发送到请求的参数。我修好了,一切都很好

php  curl  oauth-2.0  uber-api