如何生成用于身份验证的令牌?
How to generate token for authentication?
我需要通过获取 API 请求生成令牌来执行 CRUD 操作。
我使用了 Postman 的代码片段。令牌正在邮递员中生成。但是当我在 React 中生成时。它显示了这个:
[错误] https://i.stack.imgur.com/ahwUp.png
这是我从 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));
有人可以帮我解决这个问题吗?
我已经找到问题的解决方案并愿意分享答案,以便将来对其他用户有所帮助。
为了摆脱No 'Access-Control-Allow-Origin' header
,您需要在package.json 文件中添加"proxy": "https://accounts.accesscontrol.windows.net/",
并从您的JS 文件中的fetch
中删除代理。您的抓取应该看起来像这样:fetch("834fb7b4-624d-4de8-a977-2d46ad979bd9/tokens/OAuth/2", requestOptions)
Note: This is a temporary solution as its for development purpose. In
order to fix it permanently, you need to add the proxy in your server when you deploy it in the real world environment.
谢谢!
编码愉快!
我需要通过获取 API 请求生成令牌来执行 CRUD 操作。 我使用了 Postman 的代码片段。令牌正在邮递员中生成。但是当我在 React 中生成时。它显示了这个: [错误] https://i.stack.imgur.com/ahwUp.png
这是我从 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));
有人可以帮我解决这个问题吗?
我已经找到问题的解决方案并愿意分享答案,以便将来对其他用户有所帮助。
为了摆脱No 'Access-Control-Allow-Origin' header
,您需要在package.json 文件中添加"proxy": "https://accounts.accesscontrol.windows.net/",
并从您的JS 文件中的fetch
中删除代理。您的抓取应该看起来像这样:fetch("834fb7b4-624d-4de8-a977-2d46ad979bd9/tokens/OAuth/2", requestOptions)
Note: This is a temporary solution as its for development purpose. In order to fix it permanently, you need to add the proxy in your server when you deploy it in the real world environment.
谢谢! 编码愉快!