我试图使用Laravel和Guzzle的授权代码流访问其余API。Laravel Oauth2客户端授权并重定向与Guzzle
他们指定的要求:
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的$响应它会显示来自其网站的登录页面。
现在,他们的下一个指令是成功登录后会去我的重定向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
}
w ^这可以用来访问另一个端点上的受保护资源。
现在,我被困在Laravel,在Guzzle首次调用“授权”端点后,我回来的$响应不确定该怎么处理,因为我没有被自动重定向任何地方。
所以我暂时没有加入这个返回的观点:
return View::make('welcome')->with('response', $response);
这很好的花花公子(相貌丑陋无CSS,因为从他们的网站实际上没有),但似乎有适当的形式代码时,我看资源。
当前的URL只是我的项目的根:
http://myserver:8080/test/public/
然而,当我尝试登录我重定向到服务器上的我的主要根文件夹:
http://myserver:8080/
我不是确定如何让它至少正确加载重定向URI,以便我可以接受该URI?code =参数,并根据需要使用它进行另一个调用。
我希望到目前为止我没有失去任何人。提前致谢!
你能分享你的代码吗?我被困在完全相同的地方。我能够获得返回代码,但是当我尝试执行下一步POST时,我收到了未经授权的用户 – artSir 2016-08-15 18:34:07
@artSir我更新了我的答案以提供更多详细信息。 – dmotors 2016-08-22 22:41:56