如何在 curl 调用中访问 Uber API OAuth 2.0 不记名令牌
How to access the Uber API OAuth 2.0 bearer token in curl calls
我正在从 Uber API 获取访问令牌,它作为以下代码的 json 输出返回。我面临的问题是我只需要存储在变量中的访问令牌,我需要将其传递给我的其他 API 调用。
我的代码:
<?php
$fields_string = '';
$fields = array(
'client_secret' => 'xxxxxxxxxxx',
'client_id' => 'xxxxxxxxxx',
'grant_type' => 'authorization_code',
'redirect_uri' => "xxxxxxxxx",
'code' => $_GET['code']
);
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, "https://login.uber.com/oauth/v2/token");
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
$responseData = json_decode($result , TRUE);
$access_token = $responseData["access_token"];
echo "Access Token :" . $access_token ;
curl_close($ch);
?>
输出:
{"last_authenticated":0,"access_token":"QW.eyJ2FD6546754GGF6IktmczloQWo2VDJpb1gwWHVjUWVTdEE9PSIsImV4cGlyZXNfYXQiOjE0OTc5NDgwMTksInBpcGVsaW5lX2tleV9pZCI6Ik1RPT0iLCJwaXBlbGluZV9pZCI6MX0.v95QyTqyK0PPEpG1MlBtYq8-hYU44TX#GFGFGFGEUuFI","expires_in":2592000,"token_type":"Bearer","scope":"history history_lite places profile ride_widgets","refresh_token":"#$HGHG.CAESEBhoHMvuTUNUu3QXwd9hJ_kiATEoATIBMQ.aqfiFhkCA21MCOmhf92kqj_pIlMNcyKHMQ-2fImMdWI.w_s3tB3xnxpq826UXUtUsmuneDh1bvcEfdfdvC5JXU"}Access Token :
我可以看到在 json 响应中生成了访问令牌,但我无法将其存储在变量中。
我需要在其他 curl 调用中传递访问令牌,如下所示:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.uber.com/v1.2/history");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization', 'OAuth '+$atoken));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$curl_response = curl_exec($ch);
print_r($curl_response);
curl_close($ch);
你错过了这个,你需要添加这个 CURLOPT_RETURNTRANSFER
选项,如果你不应用这个,那么你的 $result
将导致 null。
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
所以你的整个代码会像这样
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, "https://login.uber.com/oauth/v2/token");
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);//add this to your code.
$result = curl_exec($ch);
$responseData = json_decode($result , TRUE);
$access_token = $responseData["access_token"];
echo "Access Token :" . $access_token ;
curl_close($ch);
我正在从 Uber API 获取访问令牌,它作为以下代码的 json 输出返回。我面临的问题是我只需要存储在变量中的访问令牌,我需要将其传递给我的其他 API 调用。
我的代码:
<?php
$fields_string = '';
$fields = array(
'client_secret' => 'xxxxxxxxxxx',
'client_id' => 'xxxxxxxxxx',
'grant_type' => 'authorization_code',
'redirect_uri' => "xxxxxxxxx",
'code' => $_GET['code']
);
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, "https://login.uber.com/oauth/v2/token");
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
$responseData = json_decode($result , TRUE);
$access_token = $responseData["access_token"];
echo "Access Token :" . $access_token ;
curl_close($ch);
?>
输出:
{"last_authenticated":0,"access_token":"QW.eyJ2FD6546754GGF6IktmczloQWo2VDJpb1gwWHVjUWVTdEE9PSIsImV4cGlyZXNfYXQiOjE0OTc5NDgwMTksInBpcGVsaW5lX2tleV9pZCI6Ik1RPT0iLCJwaXBlbGluZV9pZCI6MX0.v95QyTqyK0PPEpG1MlBtYq8-hYU44TX#GFGFGFGEUuFI","expires_in":2592000,"token_type":"Bearer","scope":"history history_lite places profile ride_widgets","refresh_token":"#$HGHG.CAESEBhoHMvuTUNUu3QXwd9hJ_kiATEoATIBMQ.aqfiFhkCA21MCOmhf92kqj_pIlMNcyKHMQ-2fImMdWI.w_s3tB3xnxpq826UXUtUsmuneDh1bvcEfdfdvC5JXU"}Access Token :
我可以看到在 json 响应中生成了访问令牌,但我无法将其存储在变量中。
我需要在其他 curl 调用中传递访问令牌,如下所示:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.uber.com/v1.2/history");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization', 'OAuth '+$atoken));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$curl_response = curl_exec($ch);
print_r($curl_response);
curl_close($ch);
你错过了这个,你需要添加这个 CURLOPT_RETURNTRANSFER
选项,如果你不应用这个,那么你的 $result
将导致 null。
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
所以你的整个代码会像这样
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, "https://login.uber.com/oauth/v2/token");
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);//add this to your code.
$result = curl_exec($ch);
$responseData = json_decode($result , TRUE);
$access_token = $responseData["access_token"];
echo "Access Token :" . $access_token ;
curl_close($ch);