Hi @Abbas Aleid
Do you want to implement authentication in code, thus avoiding the authentication window?
You need to use the HttpClient class to send a POST request to Microsoft Access Control Service (ACS) to obtain an access token. You can use the following code sample:
Here is link for your refernce:
https://learn.microsoft.com/en-us/graph/auth-v2-user?tabs=http
// Define the endpoint and parameters for the ACS request
const acsEndpoint = 'https://accounts.accesscontrol.windows.net/tokens/OAuth/2';
const acsParams = {
grant_type: 'client_credentials',
client_id: '<your-client-id>@<your-tenant-id>',
client_secret: '<your-client-secret>',
resource: '<your-resource-id>@<your-tenant-id>'
};
// Create a HttpClient object
const httpClient = new HttpClient(this.props.context.serviceScope);
// Send a POST request to the ACS endpoint with the parameters
const acsResponse = await httpClient.post(acsEndpoint, HttpClient.configurations.v1, {
body: JSON.stringify(acsParams),
headers: new Headers({
'Content-Type': 'application/x-www-form-urlencoded'
})
});
// Parse the response as JSON and get the access token
const acsResponseJson = await acsResponse.json();
const accessToken = acsResponseJson.access_token;
You can use the access token to call the SharePoint REST API
_checkModernWorkspaceStatus = async (): Promise<{ data: any, responseOk: boolean }> => {
const { item, absoluteUrl, context } = this.props;
try {
const response = await context.spHttpClient.get(
`${absoluteUrl}/modern/${item.OrgCode}/_api/web/effectiveBasePermissions`,
SPHttpClient.configurations.v1,
{
headers: [
['accept', 'application/json;odata.metadata=none'],
['Authorization', 'Bearer ${accessToken}']
]
}
);
return response.json();
catch (error) { console.log('Error: ', error); } }
For rest api authentication issues, please refer to this links:
(The above code is for reference only. For specific situations, you need to modify the code according to your scenario)
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best Regards
Cheng Feng