TeamsFx 提供商

使用 Microsoft Teams 应用程序内的 TeamsFx 提供程序提供对 Microsoft Graph 的 Microsoft Graph 工具包组件访问权限。

若要详细了解身份验证提供程序,请参阅 提供程序

入门

  1. 初始化提供程序并登录以获取所需的访问令牌

    1. 在组件内初始化提供程序。

          // Import the providers and credential at the top of the page
          import {Providers} from '@microsoft/mgt-element';
          import {TeamsFxProvider} from '@microsoft/mgt-teamsfx-provider';
          import {TeamsUserCredential, TeamsUserCredentialAuthConfig} from "@microsoft/teamsfx";
          const authConfig: TeamsUserCredentialAuthConfig = {
              clientId: process.env.REACT_APP_CLIENT_ID,
              initiateLoginEndpoint: process.env.REACT_APP_START_LOGIN_PAGE_URL,
          };
          const scope = ["User.Read"];
          const credential = new TeamsUserCredential(authConfig);
          const provider = new TeamsFxProvider(credential, scope);
          Providers.globalProvider = provider;
      
    2. credential.login(scopes)使用 方法获取所需的访问令牌。

          // Put these code in a call-to-action callback function to avoid browser blocking automatically showing up pop-ups. 
          await credential.login(this.scope);
          Providers.globalProvider.setState(ProviderState.SignedIn);
      

    使用 TeamsFx 类

    注意:TeamsFx 类将弃用并从 TeamsFx SDK 的未来版本中删除,建议改用TeamsUserCredential

    1. 在组件内初始化提供程序。

          // Import the providers and credential at the top of the page
          import {Providers} from '@microsoft/mgt-element';
          import {TeamsFxProvider} from '@microsoft/mgt-teamsfx-provider';
          import {TeamsUserCredential} from "@microsoft/teamsfx";
          const scope = ["User.Read"];
          const teamsfx = new TeamsFx();
          const provider = new TeamsFxProvider(teamsfx, scope);
          Providers.globalProvider = provider;
      
    2. teamsfx.login(scopes)使用 方法获取所需的访问令牌。

          // Put these code in a call-to-action callback function to avoid browser blocking automatically showing up pop-ups. 
          await teamsfx.login(this.scope);
          Providers.globalProvider.setState(ProviderState.SignedIn);
      
  2. 现在,可以使用 React 时,可以在 HTML 页面或方法中添加render()任何组件,并且它将使用 TeamsFx 上下文访问 Microsoft Graph。

    <!-- Using HTML -->
    <mgt-person query="me" view="threeLines"></mgt-person>
    
    // Using React
    public render(): void {
      return (
          <div>
            <Person personQuery="me" view={PersonViewType.threelines}></Person>
          </div>
      );
    }
    

有关演示如何初始化 TeamsFx 提供程序的示例,请参阅 联系人导出程序示例

另请参阅