首页 文章

Laravel API调用失败 . PostMan使用Basic Auth覆盖Authorization标头

提问于
浏览
0

您好我正试图从Postman调用API .
使用的框架是:Laravel

该网站具有浏览器身份验证(使用.htacess)
并且API具有用户身份验证(Laravel Passport)(用户必须登录)

现在,

我需要调用一个API并包含两个 Headers ,即

  • 我添加了使用username / pwd作为浏览器凭据的Basic Auth

基本身份验证:用户名/密码

  • 在 Headers 中我添加了

授权:持票人oauthtoken

(其中oauthtoken是从API调用https://servername/auth和Basic Auth:username / pwd中获取的密钥)

现在,当我发送API调用时,

授权 Headers 更改为:

授权:基本some_key

因此我得到了未经授权的回复 .
有什么方法可以将浏览器信用卡和用户身份验证标头一起发送吗?

2 回答

  • 0

    验证后检索的OAuth令牌与Postman for Basic Authentication附加的令牌不同 .

    如果您希望在请求中使用OAuth令牌,我建议您查看Postman变量并将该令牌存储在那里 .

    Here is an article that helped me build my Postman collection using JWT

    但是那里的一些剧本已经过时了 .

    我首先创建一个环境(本地),在其中添加一个键来存储值为空的令牌(oauth_token) .

    在Postman中的auth endpoints 中,您可以选中Tests选项卡,在该选项卡中可以放置脚本来更新oauth标记:

    pm.test("Logged in successfully", function () {
        var jsonData = pm.response.json();
        pm.environment.set('oauth_token', jsonData.token);
    });
    

    在此之后,您可以将值为{}的授权承载添加到使用OAuth令牌的 endpoints .

    如果你想进一步了解Postman变量,这是一个很棒的帖子variables

    回顾一下:

    • 将用户名和密码发送到auth endpoints

    • 将oauth令牌存储在从步骤1返回的Postman变量中

    • 使用{}表示法将新令牌用作邮递员变量

  • -1

    我建议使用Guzzle Library

    $http = new \GuzzleHttp\Client;
            $response = $http->request('POST', 'http://quotible.web3box.com/oauth/token', [
                'headers' => [
                    //you headers here
                ],
                'form_params'=>[
                    //your browser based parameters here
                ]
            ]);
    

相关问题