响应中的MS身份验证

本教程将介绍响应中的MS身份验证的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。

响应中的MS身份验证 教程 第1张

问题描述

我正在使用MS Teams应用程序,该应用程序通过Graph-API将新文件上载到连接的Teams OneDrive,但我无法对我的应用程序进行身份验证。

我找到的关于这个主题的文档根本没有提到RESPECT,因此我一直无法使身份验证工作(因为我对Java脚本和RESPECT非常陌生,需要一个明确的示例)。如果我可以简单地在某个地方找到有效的授权令牌,那么我只需对其进行硬编码,它就会奏效。

否则,我怎么获得用户/应用身份验证以通过Graph-API将我的文件上载到OneDrive?

以下是我的代码:

import React from 'react';
import './App.css';
import * as microsoftTeams from "@microsoft/teams-js";

class Tab extends React.Component {
  constructor(props){
 super(props)
 this.state = {
context: {}
 }
  }

  componentDidMount() {
 var myHeaders = new Headers();
 myHeaders.append("Content", "text/plain");
 myHeaders.append("Content-Type", "text/plain");
 myHeaders.append("Authorization", "Bearer {token}");

 var raw = "This works";

 var requestOptions = {
method: 'PUT',
headers: myHeaders,
body: raw,
redirect: 'follow'
 }

 fetch("https://graph.microsoft.com/v1.0/sites/OpenSesameTest/Shared%20Documents/General/FileB.txt:/", requestOptions)
 .then(response => response.text())
 .then(result => console.log(result))
 .catch(error => console.log('error', error));
  }
  render() {return (  <form>
 <div>
<label>Select file to upload</label>
<input type="file"></input>
 </div>
 <button type="submit">Upload</button>
  </form>);
 }
  }

export default Tab;

除了给我提供身份验证错误外,我的获取似乎工作正常。

推荐答案

按照步骤here操作。

首先,您需要在您的Azure Active Directory中注册应用程序并生成客户端机密。然后,您可以在对https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token的POST请求中使用应用ID和客户端密码来获取授权令牌。

然后您可以在代码中使用此内标识来代替{token}

如果您尚未注册沙盒Office 365租户,可以通过Microsoft 365 Developer Program注册以进行测试。

好了关于响应中的MS身份验证的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。