![](/img/trans.png)
[英]Laravel 5.6 Passport oAuth2 can't make request with Guzzle
[英]Laravel Oauth2 client authorizing and redirecting with Guzzle
我正在嘗試使用帶有 Laravel 和 Guzzle 的授權代碼流來訪問休息 API。
他們指定了要求:
GET https://api.restsite.com/oauth2/authorize ?
client_id = [application id] &
response_type = code &
scope = orders inventory &
redirect_uri = [redirect uri]
在 Laravel 中,我是這樣實現的:
// Create a client
$client = new Client();
$request = $client->createRequest(
'GET', 'https://api.restsite.com/oauth2/authorize',[
'query' => [
'client_id' => 'myclientid',
'response_type' => 'code',
'scope' => 'inventory',
'redirect_uri' => 'https://myownsite/uri.php',
],
]
);
// Send the request
$response = $client->send($request);
如果我 print_r $response 它將顯示來自他們網站的登錄頁面。
現在,他們的下一條指令是在成功登錄后,它將轉到我的重定向 uri,如下所示:
https://[redirect uri]?code=[authorization code]
有了這個授權碼,我現在可以按照他們的指示撥打另一個電話:
POST https://api.restsite.com/oauth2/token ?
grant_type = authorization_code &
code = [authorization code] &
redirect_uri = [redirect uri]
最后,如果一切順利,JSON 響應應該如下所示:
{
"access_token": [access token],
"token_type": "bearer",
"expires_in": 3600
}
我可以使用它來訪問另一個端點的受保護資源。
現在我被困在 Laravel 中,在 Guzzle 第一次調用“授權”端點后,返回的 $response 我不確定如何處理它,因為我沒有被自動重定向到任何地方。
所以我暫時做的是添加了這個返回視圖:
return View::make('welcome')->with('response', $response);
這很好(看起來很丑,沒有css,因為實際上不是來自他們的網站)但是當我查看源代碼時似乎有正確的表單代碼。
當前 URL 只是我的項目根目錄:
http://myserver:8080/test/public/
但是,在我嘗試登錄后,我被重定向到服務器的主根文件夾:
http://myserver:8080/
我不確定如何讓它至少正確加載重定向 URI,以便我可以使用該 URI ?code= 參數並根據需要使用它進行另一個調用。
我希望到目前為止我沒有失去任何人。 提前致謝!
我所做的不是使用 Guzzle 來處理來自外部站點的授權初始步驟,而是執行以下操作之一:
控制器:
return redirect::to('https://api.restsite.com/oauth2/authorize?');
看法:
<a href="https://api.restsite.com/oauth2/authorize?">Approve App</a>
我讓我的重定向 uri/callback 返回到我的應用程序,然后使用 Guzzle 發布和檢索必要的令牌,這些令牌作為 json 響應返回,因此不再需要任何外部站點/重定向。
最后,我可以使用實際的資源端點來查詢數據。
更新
根據要求,我提供了有關該過程如何進行的更多詳細信息:
查看(authtoken.blade.php):
<a href="https://api.restsite.com/oauth2/authorize?client_id=XXX&response_type=code&scope=inventory&redirect_uri=https://myownsite.com/return_uri.php&access_type=offline">Request New Token</a>
return_uri.php ( http://myownsite.com/ )
<?php
ob_start();
$url = 'http://myserver:8080/test/public/authtoken/get';
date_default_timezone_set('America/Los_Angeles');
$authcode = $_GET['code'];
echo 'Authorization code: ' . $authcode;
sleep(15);
$servername = "xx.xxx.xxx.xx";
$username = "user";
$password = "pass";
$dbname = "ca";
$now = date("Y-m-d H:i:s");
if ($authcode) {
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO credentials (id, auth, authDate)
VALUES ('1','$authcode', '$now') ON DUPLICATE KEY UPDATE auth = values(auth), authDate = values(authDate) ";
if ($conn->query($sql) === TRUE) {
echo "Auth code created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
//echo '<br><br><a href="http://myserver:8080/test/public/authtoken/get">go back</a>';
while (ob_get_status())
{
ob_end_clean();
}
header( "Location: $url" );
}
?>
控制器
public function authtokenget()
{
$creds = Credentials::find(1);
$auth = $creds->auth;
$client = new Client();
$client->setDefaultOption('verify', false);
$data = 'grant_type=authorization_code&code=$auth&redirect_uri=https://myownsite.com/return_uri.php';
$data_string = json_encode($data);
$datlen = strlen($data_string);
$request = $client->createRequest(
'POST', 'https://api.restsite.com/oauth2/token',[
'headers' => [
'content-type' => 'application/x-www-form-urlencoded',
'content-length' => $datlen,
],
'body' => $data_string,
'auth' => ['xxxxx=', 'xxxxx'],
]
);
$response = $client->send($request);
}
回顧
幾天前我遇到了同樣的問題。 然后我再次閱讀文檔,我發現有一個可選參數“state”可以接受字符串,所以我決定將我的路由名稱放在 state 變量中,並用它重定向回之前的路由。 但我認為這不是正確的方式。 所以我也在尋找更好的方法。 感謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.