【问题标题】:Call Graph API from SharePoint从 SharePoint 调用图形 API
【发布时间】:2019-04-28 02:04:04
【问题描述】:

我需要从 spfx webpart 调用 Graph API。

之前我们使用了以下方法:

import { MSGraphClient } from '@microsoft/sp-client-preview';

但后来我们知道 MSGraphClient 现在在 sp-client-preview 中已被贬值。

我检查了 Microsoft 文档中也提到的以下方法。

import { MSGraphClient } from '@microsoft/sp-http';

但是报错如下:

模块“d:/O365/upload-onedrive/node_modules/@microsoft/sp-http/dist/index-internal”没有导出成员“MSGraphClient”

我们现在使用的 SPFx 版本是 1.6

现在有什么方法可以从 spfx 调用 Graph API 吗?

【问题讨论】:

    标签: microsoft-graph-api sharepoint-online spfx


    【解决方案1】:

    当然我们可以在 SPFx 中使用 Graph。

    图表+adal+SPFx步骤:

    1. Azure portal 中创建一个应用程序。单击清单,然后将“oauth2AllowImplicitFlow”值更改为 true
    2. 转到设置->必需权限->添加->选择一个API->Microsoft Graph,选择权限,然后授予权限。 p>

    3. 构建 HelloWorld SPFx 项目:https://docs.microsoft.com/en-us/sharepoint/dev/spfx/web-parts/get-started/build-a-hello-world-web-part

    4. 添加 IAdalConfig.ts 和 WebPartAuthenticationContext.js 补丁文件

      提示:如果 node_modules/@types 文件夹中没有 adal 模块,最好使用命令手动安装模块:npm install @types/adal@1.0.29

    5. 将以下代码添加到 render()

        // Make an AJAX request to the Graph API and print the response as JSON.
        var getToken;
      
        var getCurrentUser = function (access_token) {
          var xhr = new XMLHttpRequest();
          xhr.open('GET', 'https://graph.microsoft.com/v1.0/me', true);
          xhr.setRequestHeader('Authorization', 'Bearer ' + access_token);
      
          xhr.onreadystatechange = function () {
      
            if (xhr.readyState === 4 && xhr.status === 200) {
      
              // Do something with the response
      
              getToken=JSON.stringify(JSON.parse(xhr.responseText), null, '  ');
              console.log('get Graph APi information=='+getToken);
            } else {
              // TODO: Do something with the error (or non-200 responses)
            //  console.log(' error');
            }
          };
          xhr.send();
      

    【讨论】:

    • 非常感谢。我很快就会试试看!
    【解决方案2】:

    实际上没有理由在 Azure 端创建任何应用程序,这一切都是由 SharePoint 自动完成的。有关详细信息,请参阅以下文档。我们确实在预览版和 GA 之间稍微更改了 API 结构,但基本与 MSGraphClient 的使用保持一致,并且没有任何手动访问令牌处理的理由。

    https://docs.microsoft.com/en-us/sharepoint/dev/spfx/use-msgraph

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-16
      • 2021-11-05
      • 2020-05-23
      • 2017-02-27
      • 2018-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多