大佬教程收集整理的这篇文章主要介绍了Azure AD 发布者的授权令牌请求返回 302,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在作为我网页后端的 Azure Function 中,我按照 page 的指示请求了 Azure AD 发布者的授权令牌。 这是我的 Azure Functions 的代码行:
// Stringfy request body
const postData = queryString.Stringify({
'grant_type': 'clIEnt_credentials','clIEnt_ID': clIEnt_ID,'clIEnt_secret': clIEnt_secret,'resource': resource,});
// Initiate options
var httpAgent = new http.Agent();
httpAgent.maxSockets = 200;
const options = {
hostname: 'login.microsoftonline.com',path: `/${tenantID}/oauth2/token`,method: 'POST',headers: {
'Content-Type': 'application/x-www-form-urlencoded',},agent: httpAgent,}
const tokenReq = http.request(options,(res) => {
console.log(`STATUS: ${res.statusCodE}`);
console.log(`headerS: ${JsON.Stringify(res.headers)}`);
res.setEnCoding('utf-8')
res.on('data',(chunk) => {
console.log(chunk);
body += chunk;
});
res.on('end',() => {
console.log('No more data in response.');
console.log("body:" + body);
context.res = {
status: 200,body: body,};
});
});
tokenReq.on('error',(E) => {
console.log(`problem with request: ${e.messagE}`);
context.res = {
status: 500,body: `problem with request: ${e.messagE}`,}
});
// write data to request body
tokenReq.write(postData);
tokenReq.end();
预期的响应是我需要的访问令牌,但是在本地运行它我得到了 STATUS 302,以及一个包含位置和其他一些参数的标头作为响应。根据我的理解,STATUS 302 指出 URL 临时移动到标题中提供的位置。现在,我不知道我应该做什么,我必须提出的请求应该是 POST 请求,因此重定向不起作用。我还尝试在收到重定向 URL 后发出新请求,但收到一条错误消息:getaddrinfo ENOTFOUND {redirect URL from header}。我在这里做错了什么?
302 错误是由 http
模块引起的,您使用 require('http');
和 http.request(options,(res)....
执行请求,因此显示 302 错误。
建议你使用var request = require('request');
来做请求,下面是我的功能代码供你参考(使用request
模块之前,你需要先运行npm install request
来安装它) :
@H_435_5@module.exports = async function (context,req) { context.log('JavaScript http trigger function processed a request.'); var result = await generatetoken(context); context.res = { body: result }; } function generatetoken(context){ var request = require('request'); var options = { 'method': 'POST','url': 'https://login.microsoftonline.com/<your tenant id>/oauth2/token','headers': { 'Content-Type': 'application/x-www-url-form-urlencoded' },form: { 'client_id': 'xxxxxx','grant_type': 'client_credentials','resource': 'xxxxx','client_secret': 'xxxxx' } }; return new Promise(function(resolve,reject) { request(options,function(err,res) { if (err) { reject(err); } else { context.log(res.body); resolve(res.body); } }) }) }
以上是大佬教程为你收集整理的Azure AD 发布者的授权令牌请求返回 302全部内容,希望文章能够帮你解决Azure AD 发布者的授权令牌请求返回 302所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。