【发布时间】:2015-06-09 02:52:53
【问题描述】:
我正在尝试使用带有 Laravel 和 Guzzle 的授权代码流来访问 rest 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= 参数并根据需要使用它进行另一个调用。
我希望到目前为止我没有失去任何人。提前致谢!
【问题讨论】:
标签: php laravel oauth-2.0 laravel-5 guzzle