Привет! У меня возникли проблемы с получением данных из API Azure AD Secured.Я выполнил регистрацию приложения в Azure AD, и я могу получить ожидаемый ответ при использовании Postman.Что, как мне кажется, должно означать, что я должен иметь возможность получать данные с помощью класса AadhttpClient.
Я следовал приведенному здесь руководству: https://docs.microsoft.com/en-us/sharepoint/dev/spfx/use-aadhttpclientчтобы попытаться подключиться через мой SPFx WebPart, но когда я добавляю его на страницу, я получаю следующую ошибку.
401 Изображение неавторизованной ошибки
Это мой файл .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"
}
}
Я новичок в этом, поэтому будьте добры.Но если кто-нибудь может ударить меня в правильном направлении относительно того, где я иду не так, это было бы замечательно.
Спасибо ...
Джон