Microsoft graph api 来自SharePoint的调用图API

Microsoft graph api 来自SharePoint的调用图API,microsoft-graph-api,sharepoint-online,spfx,Microsoft Graph Api,Sharepoint Online,Spfx,我需要从SPFXWebPart调用Graph API 之前,我们使用了以下方法: import { MSGraphClient } from '@microsoft/sp-client-preview'; 但后来我们了解到,MSGraphClient现在在sp客户端预览中已贬值 我检查了Microsoft文档中提到的以下方法 import { MSGraphClient } from '@microsoft/sp-http'; 但它给出了如下错误: 模块“d:/O365/upload one

我需要从SPFXWebPart调用Graph API

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

import { MSGraphClient } from '@microsoft/sp-client-preview';
但后来我们了解到,MSGraphClient现在在sp客户端预览中已贬值

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

import { MSGraphClient } from '@microsoft/sp-http';
但它给出了如下错误:

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

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


现在有没有办法从spfx调用图API

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

Graph+adal+SPFx步骤:

  • 在中创建应用程序。单击清单,然后将“oauth2AllowImplicitFlow”值更改为true
  • 转到设置->所需权限->添加->选择API->Microsoft Graph,选择权限,然后授予权限

  • 构建HelloWorld SPFx项目:

  • 添加并删除IAdalConfig.ts和WebPartAuthenticationContext.js修补程序文件

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

  • 将以下代码添加到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();
    

  • 当然,我们可以在SPFx中使用图形

    Graph+adal+SPFx步骤:

  • 在中创建应用程序。单击清单,然后将“oauth2AllowImplicitFlow”值更改为true
  • 转到设置->所需权限->添加->选择API->Microsoft Graph,选择权限,然后授予权限

  • 构建HelloWorld SPFx项目:

  • 添加并删除IAdalConfig.ts和WebPartAuthenticationContext.js修补程序文件

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

  • 将以下代码添加到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();
    

  • 实际上没有理由在Azure端创建任何应用程序,它是自动的,由SharePoint负责。有关详细信息,请参阅以下文档。我们确实在preview和GA之间稍微更改了API结构,但基本上MSGraphClient的使用保持不变,没有任何手动访问令牌处理的理由


    实际上没有理由在Azure端创建任何应用程序,它是自动的,由SharePoint负责。有关详细信息,请参阅以下文档。我们确实在preview和GA之间稍微更改了API结构,但基本上MSGraphClient的使用保持不变,没有任何手动访问令牌处理的理由


    非常感谢您。我很快就会试试的!非常感谢你。我很快就会试试的!