• Azure OAuth2 PostMan 授权代码


    //config varibales
    const tokenLifeTime = 59;//minute
    pm.environment.set('tenantDomain', 'dev.onmicrosoft.com') // Directories + subscriptions
    pm.environment.set('client_credentials', 'client_credentials') // type
    pm.environment.set('clientId', 'de1d0d77-') //
    pm.environment.set('clientSecret', 'fUA=') //
    pm.environment.set('resource', 'http://dev.com/cloudservices/system/test1/agent') //ResourcesId
    
    //check bearerToken.
    var oldToken = pm.environment.get('bearerToken');
    var bearerTime = pm.environment.get('bearerTime');//last update bearerToken time 
    var noToken = oldToken == undefined || oldToken == null || oldToken.trim() == '';
    var expired = bearerTime == undefined || (new Date(bearerTime).addMinutes(tokenLifeTime) - new Date()) < 0;
    if (noToken || expired) {
        console.log('BearerToken has expired.')
        pm.sendRequest(
            {
                url: 'https://login.microsoftonline.com/' + pm.environment.get('tenantDomain') + '/oauth2/token',
                method: 'POST',
                header: 'Content-Type: application/x-www-form-urlencoded',
                body: {
                    mode: 'urlencoded',
                    urlencoded: [
                        { key: 'grant_type', value: 'client_credentials', disabled: false },
                        { key: 'client_id', value: pm.environment.get('clientId'), disabled: false, },
                        { key: 'client_secret', value: pm.environment.get('clientSecret'), disabled: false, },
                        //{ key: 'scope', value: pm.environment.get('scope'), disabled: false },
                        { key: 'resource', value: pm.environment.get('resource'), disabled: false },
                    ],
                },
            },
            function (err, res) {
                //pm.globals.set('bearerToken', res.json().access_token)
                pm.environment.set('bearerToken', res.json().access_token)
                pm.environment.set('bearerTime', new Date())
                console.log(pm.environment.get('bearerToken'))
            },
        )
    } else {
        console.log('BearerToken is exist and life is ok.')
    }

    将上述代码置于左建点击某一Collection的Pre-request Script 后,再选中AuthorizationTab页修改为:

  • 相关阅读:
    好的软件架构设计(转)
    利用ManualResetEvent来来控制异步调用的打印的线程的暂停和恢复(转)
    Lucene.Net+盘古分词器(详细介绍)(转)
    如何让 height:100%; 起作用---父级元素必须设定高度
    前端问题汇总
    JS基础_强制类型转换-Number
    CSS实现垂直居中的常用方法
    vue项目上线步骤
    一篇文章全面了解运维监控知识体系
    vue+axios 前端实现登录拦截(路由拦截、http拦截)
  • 原文地址:https://www.cnblogs.com/atwind/p/15774087.html
Copyright © 2020-2023  润新知