Laravel 5.5 + Laravel Passport 4.0 で API サーバーを作り、Password Grant で保護しています。
クライアントからこのサーバーの情報を取得しようと思ったのですが、「401 Unauthorized」エラーになってしまいます。
php
1<?php 2$http = new GuzzleHttp\Client; 3 4$response = $http->post('http://example.com/oauth/token' [ 5 'form_params' => [ 6 'grant_type' => 'password', 7 'client_id' => 4, 8 'client_secret' => 'xxxxxxxx', 9 'username' => 'admin@example.com', 10 'password' => 'password', 11 'scope' => '', 12 ], 13]); 14 15$response = json_decode((string) $response->getBody(), true); 16 17$response = $http->request('GET', 'http://example.com/v2/posts', [ 18 'headers' => [ 19 'Accept' => 'application/json', 20 'Authorization' => 'Bearer ' . $response['access_token'] 21 ], 22]); 23 24var_dump(json_decode((string) $response->getBody(), true));
アクセストークンの取得自体はうまくいっているようで、$response には token_type や access_token などの配列が格納されていました。
ルーティングの保護は 'middleware' => 'auth:api'
で行っています。
http://example.com/v2/posts にブラウザから直接アクセスするとログイン画面(auth/login)にリダイレクトされます。ミドルウェアを解除した場合は JSON が出力されます。
ほぼマニュアル通りに実装しているのですがどこに原因があるのでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。