【问题标题】:How Can I get responses from api request using brearer token in laravel?如何在 laravel 中使用不记名令牌从 api 请求中获取响应?
【发布时间】:2021-12-09 07:38:08
【问题描述】:

问题描述:

  • 我有一个berer api。我只需要使用控制器传递请求 实际上发出请求并接收响应。 (注: 不使用邮递员)

我的 API 根是这样的:http://localhost/cloud/website/api/v1/

在 api.php 中

        Route::group(['prefix' => 'v1'], function() {
            Route::group(['middleware' => 'auth:sanctum'], function() {
                 Route::post('entries/sync-info', [EntrySyncInfoController::class, 'index']);
    }
 }

我正在尝试从 API 获得响应,但不断收到 500 SERVER ERROR。

    $token  ='24|cbG3w1ONkxxeUBhLCGwFjOSLk......';
    
    $response = $client->request('GET', 'http://localhost/cloud/website/api/v1/entries?perPage=50', [
    'headers' => [
        'Authorization' => 'Bearer '.$token,
        'Accept' => 'application/json',
    ],
]);

return $response->body();

但是当我向 Postman 提出请求时,它给了我完美的回应。

postman request's image is here

当我直接在浏览器上点击这个网址时,它会显示同样的错误。

我也关注this 问题。它返回 null。

我该如何解决这个问题?如何使用不记名令牌获得响应?

【问题讨论】:

    标签: laravel api token bearer-token


    【解决方案1】:
    $response = $client->get('http://localhost/cloud/website/api/v1/entries?perPage=50', [
        'headers' => [
            'Authorization' => 'Bearer '.$token,
            'Accept' => 'application/json',
        ],
    ]);
    

    参考:看到这个post, 也可以看看this

    【讨论】:

    • 感谢@moinul-robin 兄弟的参考。您的参考资料和this link 帮助我解决了这个问题。
    猜你喜欢
    • 2021-03-11
    • 2020-10-18
    • 2021-12-23
    • 2015-10-11
    • 2017-10-29
    • 1970-01-01
    • 2018-04-19
    • 2017-03-20
    • 2021-08-14
    相关资源
    最近更新 更多