我是这个API的新手,我现在正在做的事情如下:
当用户对我的应用进行身份验证时,我可以使用重定向uri上给出的身份验证代码首次访问用户,而uber建议保留/存储凭据,以便将来代表任何用户提出请求,购买我找不到办法用令牌或刷新令牌为用户实例化凭证,有什么建议吗?
我已经尝试过
Credential c = new Credential()
.setRefreshToken("MA.CAESEACXatFo4kWUn-v7mUHYbwkiATEoATIBMQ.SF5nzFJ1dnAfBpcleiSy8i_l159Kfx6fIhOCmOOmaxo.blR8m0ly-A1iC330pfMmLZ_EgnANn6NFzb83LOzZ374")
.setExpiresInSeconds(2222222222L);
但是Credential的构造函数要求一个accesMethod,我似乎没有机会。
答案 0 :(得分:0)
这就是我的PHP答案,
第一件事非常重要:注册用户时,请将所有这些信息存储到数据库中,以便您可以使用收到的密钥代表该用户发出请求!
每次要代表用户发出API请求时,都应使用收到的刷新令牌刷新承载令牌,如下所示:
首先创建一个将控制功能的类(如果不需要,其中一些将不会使用):
class uber
{
private $code = '';
private $access_token = '';
private $uber_uid = '';// this is just the id for the
//specific user that you should store in the database
private $refresh_token = '';
private $expires_in = '';
}
之后,在该类中添加一些功能:每个私有变量的getter和setter,如下所示(这是一个变量的示例,其余的也要使用它们):
/**
* @return mixed
*/
public function getAccessToken()
{
return $this->access_token;
}
/**
* @param mixed $access_token
*/
public function setAccessToken($access_token)
{
$this->access_token = $access_token;
}
现在,例如,如果要使用Webhook(我建议您这样做),这意味着您在uber帐户中为重定向uri设置的链接(例如,当您收到收据时),以便获得该收据刷新承载令牌,如下所示:
function refreshToken($refresh_token)//param is the one you stored in your DB under the name refresh_token
{
$provider = $this->getProvidersCredentialsFromYourDatabaseForTheSpecificUser();
// this should return an array
$client_id = $provider['client_id'];
$client_secret = $provider['client_secret'];
$url = 'https://login.uber.com/oauth/v2/token';
$fields = array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'grant_type' => "refresh_token",
'refresh_token' => $refresh_token
);
$fields_string = '';
foreach ($fields as $key => $value) {
$fields_string .= $key . '=' . $value . '&';
}
$fields_string = rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$result = curl_exec($ch);
$result = json_decode($result,1);
$this->setAccessToken($result['access_token']);
curl_close($ch);
return $result;
}
现在您的令牌已刷新并设置为私有,您可以创建一个函数来检索收据,例如,如下所示:
public function getRidesReceipt($receipt_id){
$url = "https://sandbox-api.uber.com/v1.2/requests/$receipt_id/receipt";// this is the test environment
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $this->access_token));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$result = curl_exec($ch);
return json_decode($result,1);
}
按如下所示在您设置的链接上捕获Webhook:
$json = file_get_contents('php://input'); //catch the data when posted
$data = json_decode($json,1); // decode it into an array so you could easily read it
如果要检查是否为recept_ready帖子,则可以这样操作:
if($data['meta']['status'] == 'ready' && $data['event_type'] == 'requests.receipt_ready'){
//add you code of what to happen with that receipt
}
我将向您展示如何在uber api上对该代码进行测试。
您将需要为此增加2个功能(在每次请求之前记住刷新令牌):
function makeRequest($url, $token){
$fields = array(
'start_latitude' => '39.761492',
'start_longitude' => '-122.423941',
'end_latitude' => '35.775393',
'end_longitude' => '-133.417546',
);
$fields = json_encode($fields);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer '. $token));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($ch);
//the below used for seeing something
echo '<pre>';
print_r(json_decode($result,1));
echo '</pre>';
// the above is just for you to see something(should print print a ride id or whatever you requested), you can comment or delete it
curl_close($ch);
return json_decode($result,1);
}
您可以这样称呼它:
makeRequest($url = 'https://sandbox-api.uber.com/v1.2/requests', $the_bearer_token_you_just_refreshed);
发出上述请求时,从打印的信息中接受代码并调用此函数:
function modifyRideStatus($url, $status, $token){
$fields = array(
'status' => $status,
);
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer '. $token));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if (!$result)
{
echo 'Modified ride to: '. $status;
}
}
这个你可以这样叫
modifyRideStatus($url = 'https://sandbox-api.uber.com/v1.2/sandbox/requests/id_of_the_ride_you_requested_and_is_printed_on_the_screen', 'accepted', $the_bearer_token_you_just_refreshed);
这些是您将要使用的状态(按此顺序依次调用该函数4次): 1)被接受
2)到达
3)in_progress
4)完成
当您调用状态为完整的函数时,UBER会在Webhook上以状态为receive_ready进行POST,如果使用上面的代码,则将捕获该POST。
我希望这会有所帮助!!如果您有任何问题,请告诉我!