2015-04-04 136 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

相關問題