我在名爲ConfigurePassport
的遷移中創建授權客戶端,並設置我希望應用程序使用的密鑰。您不需要每個用戶的客戶端。
public function up()
{
/*
* This command will create the encryption keys needed to generate secure access tokens.
* In addition, the command will create "personal access" and "password grant"
* clients which will be used to generate access tokens
*/
Artisan::call('passport:install', array('-n' => true));
// Set Password Grant Client secret to known key
DB::table('oauth_clients')->where('password_client', 1)->update(
['secret' => env('GRANT_CLIENT_SECRET', 'dfhsdfhbtg545fdf45yedh5f5blahblah')]
);
}
上述遷移運行人員命令passport:install
按照安裝客戶端的文檔。 https://laravel.com/docs/master/passport#password-grant-tokens
現在,您的移動應用可以請求這樣的令牌:唯一的每用戶參數是用戶名和密碼。
您可以在password_client爲true的oauth_clients表中找到客戶端ID。它可能會是2.
$http->post('http://your-app.com/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => 2,
'client_secret' => 'dfhsdfhbtg545fdf45yedh5f5blahblah',
'username' => '[email protected]',
'password' => 'my-password',
'scope' => '',
],
]);
謝謝。只是爲了讓您知道我要構建應用和API,因此圖片中沒有第三方網站,如臉譜網。因此,您知道爲了獲得訪問令牌,客戶端ID和客戶端密鑰是必需的,那麼我應該創建它們並將它們放在移動應用程序代碼中並將它們發送到API請求中?在那種情況下,無論設備和用戶如何,這些憑證都是相同的。 –
您不能將它們放在移動應用程序上,移動應用程序上的任何代碼都可以被攔截並且可以直接讀取。任何人都可以反編譯你的應用程序,並閱讀你的client_id和祕密。 檢查,出https://laravel.com/docs/5.4/passport#consuming-your-api-with-javascript它通過您的一些需求。 –