响应中的MS身份验证 [英] MS Authentication in React

查看:40
本文介绍了响应中的MS身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆