AadHttpClient连接到Dynamics 365-商业中心

时间:2018-10-10 21:23:26

标签: sharepoint spfx

嗨,我在从Azure AD安全API获取数据时遇到一些问题。我已经在Azure AD中完成了应用程序注册,并且在使用Postman时可以获得预期的响应。我认为这应该意味着我应该能够使用AadhttpClient类获取数据。

我已经按照这里的指南进行操作:https://docs.microsoft.com/en-us/sharepoint/dev/spfx/use-aadhttpclient尝试从我的SPFx WebPart连接,但是当我将其添加到页面时,出现以下错误。

401 Unauthorized Error Image

这是我的.ts文件

import { Version } from '@microsoft/sp-core-library';
import {
  BaseClientSideWebPart,
  IPropertyPaneConfiguration,
  PropertyPaneTextField
} from '@microsoft/sp-webpart-base';
import { escape } from '@microsoft/sp-lodash-subset';

import styles from './BusinessCentralWebPart.module.scss';
import * as strings from 'BusinessCentralWebPartStrings';

import { AadHttpClient, HttpClientResponse } from '@microsoft/sp-http';

export interface IBusinessCentralWebPartProps {
  description: string;
}

export default class BusinessCentralWebPart extends BaseClientSideWebPart<IBusinessCentralWebPartProps> {

 private companiesClient: AadHttpClient;

  protected onInit(): Promise<void> {
    return new Promise<void>((resolve: () => void, reject: (error: any) => void): void => {
      this.context.aadHttpClientFactory
        .getClient('50d56f05-1ab4-4395-j0hn-09f5b8e7101b')
        .then((client: AadHttpClient): void => {
          this.companiesClient = client;
          resolve();
        }, err => reject(err));
    });
  }

  public render(): void {
    this.context.statusRenderer.displayLoadingIndicator(this.domElement, 'Companies');

    this.companiesClient
      .get('https://api.businesscentral.dynamics.com/v1.0/api/beta/companies', AadHttpClient.configurations.v1)
      .then((res: HttpClientResponse): Promise<any> => {
        return res.json();
      })
      .then((companies: any): void => {
        this.context.statusRenderer.clearLoadingIndicator(this.domElement);
        this.domElement.innerHTML = `
          <div class="${ styles.companies}">
            <div class="${ styles.container}">
              <div class="${ styles.row}">
                <div class="${ styles.column}">
                  <span class="${ styles.title}">Orders</span>
                  <p class="${ styles.description}">
                    <ul>
                      ${companies.map(o => `<li>${companies.DisplayName}</li>`).join('')}
                    </ul>
                  </p>
                  <a href="https://aka.ms/spfx" class="${ styles.button}">
                    <span class="${ styles.label}">Learn more</span>
                  </a>
                </div>
              </div>
            </div>
          </div>`;
      }, (err: any): void => {
        this.context.statusRenderer.renderError(this.domElement, err);
      });
  }

  protected get dataVersion(): Version {
    return Version.parse('1.0');
  }

  protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
    return {
      pages: [
        {
          header: {
            description: strings.PropertyPaneDescription
          },
          groups: [
            {
              groupName: strings.BasicGroupName,
              groupFields: [
                PropertyPaneTextField('description', {
                  label: strings.DescriptionFieldLabel
                })
              ]
            }
          ]
        }
      ]
    };
  }
}

这是我的package-solution.json文件。

{
  "$schema": "https://developer.microsoft.com/json-schemas/spfx-build/package-solution.schema.json",
  "solution": {
    "name": "businesscentral-client-side-solution",
    "id": "5a784d64-85bf-4d5e-9c34-18e1037ee0f3",
    "version": "1.0.0.0",
    "includeClientSideAssets": true,
    "skipFeatureDeployment": true,
    "webApiPermissionRequests": [
     {
       "resource": "Business Central",
       "scope": "user_impersonation"
     }
   ]
  },
  "paths": {
    "zippedPackage": "solution/businesscentral.sppkg"
  }
}

我对此并不陌生,所以请客气。但是,如果有人能向我正确地指出我要去哪里的地方,那太好了。

谢谢...

约翰

0 个答案:

没有答案