curl 在SecurityContext中找不到身份验证对象,提取请求出错

avwztpqn  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(173)

我尝试使用cURL和**fetch(JavaScript)**调用一个API端点,因为它可以很好地与cURL一起工作,而fetch却不能。
下面是cURL请求:

curl -i -L -H 'Accept: application/json' --data 'client_id=[ID]&c
lient_secret=[SECRET]&grant_type=authorization_code&red
irect_uri=[REDIRECT_URI]&code=[CODE]' https://orcid.org/oauth/token

何处获取:

fetch("https://orcid.org/oauth/token", {
        method: "POST",
        headers: {
          Accept: "application/json",
        },
        body: `client_id=[ID]&c
lient_secret=[SECRET]&grant_type=authorization_code&red
irect_uri=[REDIRECT_URI]&code=[CODE]`,
      })
        .then((res) => res.json())
        .then((data) => {
          console.log(data);
        })
        .catch((error) => console.log(error));

错误:

www-authenticate: Bearer realm="orcid", error="unauthorized", error_description="An Authentication object was not found in the SecurityContext"
3zwtqj6y

3zwtqj6y1#

我在PayU上也遇到了类似的问题。一旦我将字符串化的主体更改为URLSearchParams对象,我的问题就消失了。
我不确定它是否对您有帮助,但您可以尝试以下代码:

const headers = {
  "Content-Type": "application/x-www-form-urlencoded",
};

const params = new URLSearchParams();
params.append('client_id', ID);
params.append('client_secret', SECRET);
params.append('grant_type', 'authorization_code');
params.append('redirect_uri', REDIRECT_URI);
params.append('code', CODE);

fetch("https://orcid.org/oauth/token", {
  method: "POST",
  headers,
  body: params,
});

相关问题