我正在重新询问和更新这个问题,并在评论偏离轨道时删除我的旧问题。
第一次使用Laravel Passport
,我花了几个小时试图解决这个问题。我正在从Wordpress
站点调用 api 端点。Passport
和 Laravel 5.7.19
都是昨天全新安装的最新版本。
我的 API 使用 Postman
工作正常,具有Passport
身份验证。
我从Wordpress
调用Laravel Passport
API,如下所示:
$args = array(
'method' => 'POST',
'timeout' => '45',
'redirection' => '5',
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(
'Accept' => 'application/json',
'X-Requested-With' => 'XMLHttpRequest',
'Authorization' => 'Bearer dd4b28b53ea...',
),
'body' => array(
'email' => $email,
'channel_url' => $channel_url,
'api_key' => $api_key,
),
'cookies' => array()
);
// PING API
$response = wp_remote_post( $login, $args );
如果我的路由在auth:api
组内,如下所示:
Route::group([
'middleware' => 'auth:api'
], function() {
Route::post('my/route', 'ApiAuthController@userChannel');
});
它失败,wp_remote_post()
调用401
。有了 Postman
+ Bearer token
它就可以工作了。
在auth:api
组之外的路线适用于Wordpress
和Postman
,我可以记录/查看令牌:
[2019-02-11 05:23:32] local.INFo: Bearer dd4b28b53ea...
我尝试按照许多帖子的建议更改.htaccess
文件和php artisan optimize:clear
Passport::withoutCookieSerialization();
AppServiceProvider
,但没有成功。
Laravel
这边的例外是:
The resource owner or authorization server denied the request. {"exception":"[object] (League\OAuth2\Server\Exception\OAuthServerException(code: 9): The resource owner or authorization server denied the request. at /home/vagrant/src/my-app/vendor/league/oauth2-server/src/Exception/OAuthServerException.php:215, InvalidArgumentException(code: 0): The JWT string must have two dots at /home/vagrant/src/my-app/vendor/lcobucci/jwt/src/Parser.php:95)
特拉维斯提到的,我上面的代码没有任何问题。我的控制器中存在逻辑问题。上面的代码正常工作。