程序问答   发布时间:2022-05-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Azure AD 发布者的授权令牌请求返回 302大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Azure AD 发布者的授权令牌请求返回 302?

开发过程中遇到Azure AD 发布者的授权令牌请求返回 302的问题如何解决?下面主要结合日常开发的经验,给出你关于Azure AD 发布者的授权令牌请求返回 302的解决方法建议,希望对你解决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,请注明来意。
标签:302