首页 > 解决方案 > 从 Wordpress wp_remote_post 调用 Laravel Passport 时,身份验证失败,但与 Postman 一起使用

问题描述

我正在重新询问和更新这个问题,并在评论偏离轨道时删除我的旧问题。

第一次使用Laravel Passport,我花了几个小时试图弄清楚这一点。我正在从Wordpress站点调用 api 端点。两者都是昨天全新安装的最新版本PassportLaravel 5.7.19

我的 API 使用Postman, 和Passport身份验证工作得很好。

我这样调用Laravel PassportAPI Wordpress

$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', 'Api\AuthController@userChannel');

});

通话失败401wp_remote_post()使用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)

标签: phpwordpresslaravellaravel-passport

解决方案


正如 Travis 提到的,我上面的代码没有任何问题。我的控制器出现逻辑问题。上面的代码可以正常工作。


推荐阅读