通过 Oauth 请求 Zoom API 的访问令牌 - 错误“缺少授权类型”

Fre*_*ckG 3 json oauth axios zoom-sdk

我正在尝试通过 Oauth 从 Zoom api 接收访问令牌。无论我尝试以哪种形式发送正文,“Content-Type”:“application/json”或“Content-Type:application/x-www-form-urlencoded”,它总是会出现错误:{reason:“Missing grant type” ,错误:'无效请求'}。

var options = {
  method: "POST",
  url: "https://zoom.us/oauth/token",
  body: JSON.stringify({
    grant_type: "authorization_code",
    code: process.env.AUTH_CODE,
  }),
  redirect_uri: "https://zoom.us",
};

var header = {
  headers: {
    Authorization:
      "Basic " +
      Buffer.from(process.env.ID + ":" + process.env.SECRET).toString("base64"),
  },
  "Content-Type": "application/json",
};

var tokCall = () =>
  axios
    .post("https://zoom.us/oauth/token", options, header)
    .then((response) => {
      console.log(response);
    })
    .catch((error) => {
      console.log(error.response);
    });

tokCall();

Run Code Online (Sandbox Code Playgroud)

我相当确定答案在于 Oauth 接收数据的数据类型,或者它在哪里/是否接收正文。如有任何建议,我们将不胜感激。

Koj*_*udu 6

抛出错误是因为当您Request Access Token Zoom API希望将数据作为查询参数(您可能称为查询字符串)找到时,您将数据作为发布请求的正文发送。

参考

https://marketplace.zoom.us/docs/guides/auth/oauth#local-test

链接中的页面图像,突出显示查询参数的使用和 API 调用的内容类型要求

改变

var options = {
  method: "POST",
  url: "https://zoom.us/oauth/token",
  body: JSON.stringify({
    grant_type: "authorization_code",
    code: process.env.AUTH_CODE,
  }),
  redirect_uri: "https://zoom.us",
};
Run Code Online (Sandbox Code Playgroud)

var options = {
      method: "POST",
      url: "https://zoom.us/oauth/token",
      params: {
        grant_type: "authorization_code",
        code: process.env.AUTH_CODE,
        redirect_uri: "<must match redirect uri used during the app setup on zoom>"
      },
    };
Run Code Online (Sandbox Code Playgroud)

Content-Type头应设置为,application/x-www-form-urlencoded因为这是 Zoom API 本身的要求。

顺便说一句,axios要求您将请求的主体字段/对象命名为数据,而且也不需要JSON.stringify()方法,因为axios会在幕后为您完成此操作