Example 13-30. Making a request with the password grant type
// Routes/web.php in the *consuming application*Route::get('tweeter/password-grant-auth',function(){$http=new GuzzleHttp\Client; // Make call to "Tweeter," our Passport-powered OAuth server$response=$http->post('http://tweeter.test/oauth/token',['form_params'=>['grant_type'=>'password','client_id'=> config('tweeter.id'),'client_secret'=> config('tweeter.secret'),'username'=>'matt@mattstauffer.co','password'=>'my-tweeter-password','scope'=>'',],]);$thisUsersTokens= json_decode((string)$response->getBody(),true); // Do stuff with the tokens});
请注意,用于密码授权类型的id和secret将是passport应用程序的oauth_clients数据库表中与passport授予客户端名称匹配的行中的id和secret。当你运行passport:install你将在数据表中看到默认生成两个客户端:“Laravel Personal Access Client” 和 “Laravel Password Grant Client.”
$php artisan passport:client
Which user ID should the client be assigned to?:
>1
What should we name the client?:
> SpaceBook
Where should we redirect the request after authorization?
[http://tweeter.test/auth/callback]: > http://spacebook.test/tweeter/callback
New client created successfully.
Client ID: 4
Client secret: 5rzqKpeCjIgz3MXpi3tjQ37HBnLLykrgWgmc18uH
Example 13-31. A consumer app redirecting a user to our OAuth server
Example 13-32. The authorization callback route in the sample consuming app
// In SpaceBook's routes/web.php:
Route::get('tweeter/callback', function (Request $request) {
if ($request->has('error')) {
// Handle error condition
}
$http = new GuzzleHttp\Client;
$response = $http->post('http://tweeter.test/oauth/token', [
'form_params' => [
'grant_type' => 'authorization_code',
'client_id' => config('tweeter.id'),
'client_secret' => config('tweeter.secret'),
'redirect_uri' => url('tweeter/callback'),
'code' => $request->code,
],]);
$thisUsersTokens = json_decode((string)$response->getBody(), true);
// Do stuff with the tokens
});
Example 13-33. Defining token refresh times
// AuthServiceProvider's boot() method
public function boot() {
$this->registerPolicies();
Passport::routes();
// How long a token lasts before needing refreshing
Passport::tokensExpireIn(
now()->addDays(15)
);
// How long a refresh token will last before re-auth
Passport::refreshTokensExpireIn(
now()->addDays(30)
);
}
Example 13-34. Requesting a new token using a refresh token
// In SpaceBook's routes/web.php:
Route::get('tweeter/request-refresh', function (Request $request) {
$http = new GuzzleHttp\Client;
$params = [
'grant_type' => 'refresh_token',
'client_id' => config('tweeter.id'),
'client_secret' => config('tweeter.secret'),
'redirect_uri' => url('tweeter/callback'),
'refresh_token' => $theTokenYouSavedEarlier,
'scope' => '',
];
$response = $http->post(
'http://tweeter.test/oauth/token',
['form_params' => $params]
);
$thisUsersTokens = json_decode((string)$response->getBody(), true);
// Do stuff with the tokens
});
$php artisan passport:client --personal
What should we name the personal access client?
[My Application Personal Access Client]:
> My Application Personal Access Client
Personal access client created successfully.
// Creating a token without scopes
$token = $user->createToken('Token Name')->accessToken;
// Creating a token with scopes
$token = $user->createToken('My Token', ['place-orders'])->accessToken;
Example 13-35. Setting jQuery to pass Laravel’s CSRF tokens and the X-Requested-With header with all Ajax requests