【问题标题】:Microsoft Graph Drive searchMicrosoft Graph 驱动器搜索
【发布时间】:2017-05-16 21:02:53
【问题描述】:

我正在尝试修改 node.js 快速入门示例以生成用户的 excel 文档列表,但我遇到了问题。

此查询在 Graph Explorer 中有效(返回非空结果):

https://graph.microsoft.com/v1.0/me/drive/root/search(q='.xlsx')?select=name,id,webUrl

我尝试了几种方法在 graphHelpers.js 中重现它。 此查询返回空结果:

function getExcelDocs(accessToken, callback) {
  // Get list of excel docs in the user's onedrive
  request
   .get("https://graph.microsoft.com/v1.0/me/drive/root/search(q='.xlsx')?select=name,id,webUrl")
   .set('Authorization', 'Bearer ' + accessToken)
   .end((err, res) => {
     // Returns 200 OK and the search result in the body. 
     callback(err, res);
   });
}

如果我用驱动器子查询替换搜索 URL,我会返回一组非空文档。

.get('https://graph.microsoft.com/v1.0/me/drive/root/children')

我如何格式化查询 url 有问题吗?

【问题讨论】:

  • 您是否尝试过对 URL 进行编码(即 encodeURIComponent() )?
  • 您建议对 URL 的哪一部分进行编码?显然,如果我对冒号和斜杠进行编码,get 请求将失败。我会尝试编码括号和单引号。
  • 不幸的是,这返回了错误的请求结果。
  • 我不熟悉这个graphHelpers.js 库,但听起来它可能无法正确处理搜索查询。您有指向 graphHelpers.js 项目的链接吗?不看代码本身就很难判断。

标签: microsoft-graph-api


【解决方案1】:

URI 中的括号导致了这里的问题。这些括号中的内容代表请求的body。处理此请求的底层库 (SuperAgent) 似乎不喜欢它们直接进入 URI。

我相信您可以通过单独提供 URL 组件来解决此问题:

request
  .get("https://graph.microsoft.com/v1.0/me/drive/root/search", "q='.xlsx'")
  .query({ select: 'name,id,webUrl'})
  .set('Authorization', 'Bearer ' + accessToken)
  .end((err, res) => {
      // Returns 200 OK and the search result in the body. 
      callback(err, res);
  });

或者,这也可能有效:

request
  .get("https://graph.microsoft.com/v1.0/me/drive/root/search")
  .query({ select: 'name,id,webUrl'})
  .send("q='.xlsx'")
  .set('Authorization', 'Bearer ' + accessToken)
  .end((err, res) => {
      // Returns 200 OK and the search result in the body. 
      callback(err, res);
  });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    相关资源
    最近更新 更多