如何在 Guzzle 中设置授权 HTTP header?

How to set Authorization HTTP header in Guzzle?

我想使用 Publons 的 API。他们提到 "Any other requests should include a token in the Authorization HTTP header and prefixed with 'Token'. 所以,我尝试在 headers 中设置 Token。这是我的代码


$client = new Client([
            'base_uri' => "https://publons.com/api/v2/academic/",
            'headers' => [
                'Token' => "MyToken"
            ]
        ]);

结果是{"detail":"Authentication credentials were not provided."}。我确保令牌是正确的,因为我已经在 Talend API Tester 上尝试过它并且可以正常工作。谁能帮我?哪里错了?

只是猜测,他们的文档中没有太多内容,但我想它看起来像这样:

$client = new Client([
            'base_uri' => "https://publons.com/api/v2/academic/",
            'headers' => [
                'Authorization' => "Token{Your api token here}"
            ]
]);

他们想要授权 header 和以“令牌”为前缀的令牌...

另外this问题会给你更多关于如何处理你的案子的想法。