java - 使用凭据对用户进行身份验证
问题描述
我是这个 api 的新手,我现在正在经历的是以下内容:
当用户对我的应用程序进行身份验证时,我可以使用重定向 uri 上给出的身份验证代码第一次毫无问题地访问用户,优步建议保留/存储凭据以在将来代表任何用户发出请求,我可以找到无法使用令牌或刷新令牌为用户实例化凭据,有什么建议吗?
我已经尝试过类似的东西
Credential c = new Credential()
.setRefreshToken("MA.CAESEACXatFo4kWUn-v7mUHYbwkiATEoATIBMQ.SF5nzFJ1dnAfBpcleiSy8i_l159Kfx6fIhOCmOOmaxo.blR8m0ly-A1iC330pfMmLZ_EgnANn6NFzb83LOzZ374")
.setExpiresInSeconds(2222222222L);
但是 Credential 的构造函数要求一个我似乎无法获得的访问方法。
解决方案
所以这是我的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)进行中
4)完成
当您调用具有已完成状态的函数时,UBER 将在您的 webhook 上使用状态 receive_ready 进行 POST,如果您使用我上面的代码,您将捕获它。
我希望这有帮助!!如果您有任何问题,请告诉我!
推荐阅读
- javascript - 为什么'用 insertBefore 注入避免 appendChild 错误'?
- python-3.x - 空路径与这些 URL 中的任何一个都不匹配
- linux - 如何将大块行存储在shell中的变量中
- python - 在 python,pandas,openpyxl 中拆分或文本到列标题单元格
- css - 我的线性渐变的“高度”如何在 Safari 中随着时间的推移而减小?
- python - 提取特征重要性系数/分数
- css - 如何在没有任何内联css的情况下更改离子图标的颜色?
- javascript - React,Express - 从数据库中获取数据返回对象数组,但我无法访问数组中的任何对象
- razor - 是否可以从 DNNSharp 的 Action Form 或 Action Grid 获取内容或令牌并将其显示在 2sxc 模板中?
- c++ - 带有 enqueue_event 的 OpenCL 2.x clk_event_t 给出了令人惊讶的错误