我是这个api的新手,我现在正在经历的是以下内容:
当用户验证我的应用程序时,我可以第一次使用重定向uri上给出的auth代码访问用户,uber建议保留/存储凭据以代表任何用户在将来发出请求,我可以找到无法使用令牌或刷新令牌为用户实例化凭据,任何建议?
我已经尝试过类似的东西
Credential c = new Credential()
.setRefreshToken("MA.CAESEACXatFo4kWUn-v7mUHYbwkiATEoATIBMQ.SF5nzFJ1dnAfBpcleiSy8i_l159Kfx6fIhOCmOOmaxo.blR8m0ly-A1iC330pfMmLZ_EgnANn6NFzb83LOzZ374")
.setExpiresInSeconds(2222222222L);
但是Credential的构造函数要求一个accessMethod,我似乎无法抓住它。
所以这里是我的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上对此代码进行测试。
你需要为此做两个以上的功能(记住在每次请求之前刷新令牌):
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上使用状态receipt_ready进行POST,如果您使用上面的代码,您将捕获它。
我希望这有帮助!!如果您有任何疑问,请告诉我!