API login from Android application using laravel 5.3 passport

For two days I dig google, but I can not find the initial thread for my problem, now I'm not ok. Please help me in some direction / howTo

I have a web application created using laravel 5.3, I installed the passport as described here . if I go /home, it shows great.

Homepage

Now I have to make an Android app from which

  • An existing web application user can log in.
  • get the list of tasks of this user TaskModel (ons_tasks(id, title, description))

only related routes

in web.php 
Auth::routes();

in api.php 

Route::get('/user', function (Request $request) {
    return $request->user();
})->middleware('auth:api');


Route::group(['middleware' => ['auth:api']], function () {

    Route::get('/test', function (Request $request) {
        return response()->json(['name' => 'test']);
    });

    Route::get('/task/list', function (Request $request) {        
        $list = \App\Model\TaskModel::all();
        return response()->json($list);
    });

});

:, /login , TokenMismatchException, Android- ? Auth::routes() api? , , , .

-,

/api/test, /home !!!

.

+4
1

API Passport

Client Password Grant Client, . Passport.

, :

 php artisan passport:client --password

, auth: api.

, /oauth/token route (, , PHP, , Java):

$http = new GuzzleHttp\Client;

$response = $http->post('http://your-app.com/oauth/token', [
    'form_params' => [
        'grant_type' => 'password',
        'client_id' => '<client id returned from the artisan command above>',
    '    client_secret' => '<secret returned from artisan command above>',
        'username' => 'taylor@laravel.com',
        'password' => 'my-password',
        'scope' => '',
    ],
]);

return json_decode((string) $response->getBody(), true);

, client_secret client_id, , , username password .

, access_token refresh_token . access_token - , auth:api. api, Authorization: Bearer <your accessToken> Accept: application/json

, "" :

$response = $client->request('GET', '/api/test', [
    'headers' => [
        'Accept' => 'application/json',
        'Authorization' => 'Bearer '. <accessToken from /oauth/token call>,
    ],
]);

, JSON .

/api/test ?

auth:api. , , , .

, .

+3

Source: https://habr.com/ru/post/1671878/


All Articles