簡體   English   中英

使用訪問令牌通過 Microsoft Graph API 與 OneDrive 交互

[英]Interact with OneDrive through Microsoft Graph API using Access Token

我是 Microsoft Graph API 的新手,我正在嘗試通過 NodeJS API 訪問 One Drive。 我已完成以下操作以從 Graph API 獲取訪問令牌,以便訪問我的 One Drive,而無需用戶每次想要訪問我的驅動器上的內容時都登錄。

const postData = {
  client_id: 'xxxxxxxxxxxxxxxxxxx',
  scope: 'https://graph.microsoft.com/.default',
  client_secret: 'xxxxxxxxxxxxxxxxxxx',
  grant_type: 'client_credentials'
};

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

// Get  access token and assign it to the token variable
axios
  .post('https://login.microsoftonline.com/xxxxxxxxxxxxxxxxxxx/oauth2/v2.0/token', qs.stringify(postData))
  .then(response => {
    token = response.data.access_token;
  }).catch(error => {
    console.log(error);
  });

我的 API 已成功獲取令牌。 但是,當我嘗試使用此令牌和文檔中提供的端點訪問我的 One Drive 時出現錯誤。 代碼如下:

const AuthStr = 'Bearer ' + token;

axios.get('https://graph.microsoft.com/v1.0/drive/root/children', { headers: { Authorization: AuthStr } }).then(response => {
    console.log(response);
}).catch((error) => {
    console.log(error);
});

我究竟做錯了什么?

您只需要使用如下網址:

https://graph.microsoft.com/v1.0/users/{userid}/drive/root/children

在 url 中指定用戶 ID,因為 client_credentials 授權流的訪問令牌不包含用戶身份。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM