[向Outlook API提供访问令牌时出现HTTP 401错误

问题描述 投票:0回答:1

我正在尝试为用户创建文件夹,但api调用尝试一直失败。我的代码能够接收正确的访问令牌,因此我相信be bug将在下面的createFolderTestFunction中。

enter image description here

async function redirectToDashboard() {
  console.log("redirect to dashboard");
  // var response = await requestTokenSilent();
  var response;
  if (!response || !response.status == 200) {
    response = await requestTokenPopup();
  }

  if (response.accessToken) {
    console.log(response);
    createFolderTest(response.accessToken);
    // location.href = hostname;
  } else {
    console.log("Unable to acquire token");
  }
}


function createFolderTest(accessToken) {
  var options = {
    method: "POST",
    headers: {
      Authorization: accessToken,
      "Content-Type": "application/json"
    },
    mode: "cors",
    body: JSON.stringify({
      displayName: "@COOLMONDAY"
    })
  };

  var graphEndpoint = "https://outlook.office.com/api/v2.0/me/Inbox/";

  fetch(graphEndpoint, options)
    .then(resp => {
      console.log(resp);
    })
    .catch(err => {
      console.log(err);
    });
} 
javascript http post microsoft-graph token
1个回答
0
投票

建议您首先在Graph Explorer中使用它。这样可以消除所用语言和访问令牌权限的任何问题。

https://developer.microsoft.com/en-us/graph/graph-explorer/preview

Microsoft Graph端点实际上是https://graph.microsoft.com/,您可以使用Outlook URL,但前进Graph是我们在文档,SDK和工具上的投资。

根据文档https://docs.microsoft.com/en-us/graph/api/user-post-mailfolders?view=graph-rest-1.0&tabs=http

您应该使用,您缺少“邮件文件夹”

POST /me/mailFolders

您还可以使用我们的JavaScript SDK,使用智能感知和强类型对象使此类错误更容易出现。

    const options = {
    authProvider,
};

const client = Client.init(options);

const mailFolder = {
  displayName: "displayName-value"
};

let res = await client.api('/me/mailFolders')
    .post(mailFolder);

https://docs.microsoft.com/en-us/graph/api/user-post-mailfolders?view=graph-rest-1.0&tabs=javascript

© www.soinside.com 2019 - 2024. All rights reserved.