'How to generate token for authentication?

I need to generate a token to do the CRUD operations by fetching the API requests. I have used the code snippet from the Postman. The token is getting generated in the Postman. But when I am generating in React. It is showing this : [Error] https://i.stack.imgur.com/ahwUp.png

This is the code snippet that I copied from Postman :

var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/x-www-form-urlencoded");
myHeaders.append("Cookie", "esctx=AQABAAAAAAD--DLA3VO7QrddgJg7WevrQx7IG43UK7gipYHXtqZImLB1jfBLK4PTkZlgLq3BvpTizt3xt8EZQrpUJGa0hTnSdpRf-AenJvnGNABiv2cWYWSyJj9QNm-vWalRGHuDZ6Km_DaX_5CQHUa4H8U-osEGCM48buOyj0G819e1NoxuvoOD6fZvMI5nnDWZyjNa1mogAA; fpc=An1vbDtRI8BAiCLlUBBGpFXf9_srAQAAAA6uptkOAAAA; stsservicecookie=estsfd; x-ms-gateway-slice=estsfd");

var formdata = new FormData();
formdata.append("grant_type", "client_credentials");
formdata.append("client_id", "a01d4a9b-fda6-46e4-9174-661a6673acdc@834fb7b4-624d-4de8-a977-2d46ad979bd9");
formdata.append("client_secret", "oczmqK3VSCylwZtJba9fwTEpRYpKykge3AmZVCHLmUc=");
formdata.append("resource", "00000003-0000-0ff1-ce00-000000000000/cooponline.sharepoint.com@834fb7b4-624d-4de8-a977-2d46ad979bd9");

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: formdata,
  redirect: 'follow'
};

fetch("https://accounts.accesscontrol.windows.net/834fb7b4-624d-4de8-a977-2d46ad979bd9/tokens/OAuth/2", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

Can someone please help me with the solution?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source