2015-04-04 53 views
0

我试图使用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 =参数,并根据需要使用它进行另一个调用。

我希望到目前为止我没有失去任何人。提前致谢!

回答

0

我做了什么,而不是使用狂饮来处理来自外部网站,我只是做了其中的一个授权第一步:

控制器:

return redirect::to('https://api.restsite.com/oauth2/authorize?'); 

查看:

<a href="https://api.restsite.com/oauth2/authorize?">Approve App</a> 

我有我的重定向uri /回调返回到我的应用程序,然后使用Guzzle发布和检索必要的令牌,它作为json响应返回,因此不需要任何更多的外部站点/红色irects。

最后,我可以使用实际的资源端点来查询数据。

UPDATE

在请求我已在该过程如何去提供更多的细节:

视图(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); 

} 

重点回顾

  1. 访问使用查看链接的授权最初的端点。
  2. 有一个php返回文件,它是请求的一部分,以获取必要的令牌/访问凭证并存储到数据库,然后返回到路由。
  3. 该路由运行一个控制器函数,该函数现在抓取您之前保存的数据库条目中的数据,按照您现在的希望进行操作。
+0

你能分享你的代码吗?我被困在完全相同的地方。我能够获得返回代码,但是当我尝试执行下一步POST时,我收到了未经授权的用户 – artSir 2016-08-15 18:34:07

+0

@artSir我更新了我的答案以提供更多详细信息。 – dmotors 2016-08-22 22:41:56