zoukankan      html  css  js  c++  java
  • 【Azure API 管理】解决调用REST API操作APIM(API Management)需要认证问题(Authentication failed, The 'Authorization' header is missing)

    问题描述

    在通过REST API的方式来管理APIM资源,需要调用Azure提供的management接口。而这所有的接口,都是需要有Token并且还需要正确的Token。如若不然,就会获取到如下的错误:

    {
        "error": {
            "code": "AuthenticationFailed",
            "message": "Authentication failed. The 'Authorization' header is missing."
        }
    }

    OR 

    {
        "error": {
            "code": "AuthenticationFailed",
            "message": "Authentication failed."
        }
    }

    如在官方对API调用的介绍中,都是需要设置 Authorization 。

    缺少Token和Token错误的截图(使用Postman测试调用Get APIM API List的接口:GET https://management.chinacloudapi.cn/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis?api-version=2019-12-01)

    本文就从 快速获取Token通过AAD Authorization URL获取Token 两种方式进行介绍。

    问题解决

    方式一: 快速的从Azure APIM门户中获取Token

    PS: 这个方式适用于验证Azure Management的调用方式,或一次性调用等场景。使用的Token权限能力由当前登录Azure门户的用户的权限所决定

    1)登录Azure门户,打开API Management服务并选中其中需要操作的APIM对象。

    2)点击F12,打开浏览器的开发者模式,进入Networking选项卡

    3)刷新页面,在所有请求列表中搜索“ management.chinacloudapi.cn ”,筛选目标请求

    4)在请求的 Request Body 中发现 Authorization 值。

    演示动画:

     

    方式二: 通过AAD Authorization URL获取Token

    PS:此种方式为正确的调用方式,通过发送POST请求获取Token。可以在Coding中长期使用。由在AAD:Azure Active Directory中所使用的服务主体权限所决定。

    1)登录Azure 门户,进入AAD页面,选中“ App Registrations ”,然后点击“ + New registration ”按钮

    2)输入新注册应用的名称 “ apimDevOpsUser ”,点击“ Register ”按钮。注册成功后,保存下“Tenant”和“Application”值。

    3)设置应用客户端密码。选择 “ Certificates & secrets ” 目录,点击“ + New client secret ”按钮,根据提示生产新密钥,然后复制出来保存(PS: 此处的密钥只有第一次创建时可见,此后全是*号代替,所以务必保存下来)

    4)回到APIM资源的权限设定页面(IAM), 为新的注册应用赋予 Contributor 权限。(可根据具体需要赋予权限)

    6)在Postman中使用POST方式调用接口:https://login.chinacloudapi.cn/{{TENANT}}/oauth2/v2.0/token

    Request Type:
    POST
    
    Request URL:
    https://login.chinacloudapi.cn/{{TENANT}}/oauth2/v2.0/token
    
    Request Body:
    tenant:{{TENANT}}
    client_id:{{CLIENT}}
    scope:https://management.chinacloudapi.cn/.default
    grant_type:client_credentials
    client_secret:{{SECRETS}}

    PS: scope是非常重要的一个参数,如APIM资源的API的URL为"https://management.chinacloudapi.cn/subscriptions/{...",所以正确的SCOPE是“ https://management.chinacloudapi.cn/.default 

    7) 发送请求,获取正确的Token。

    演示动画:

     

    获取Token后的正确调用截图:

    GET https://management.chinacloudapi.cn/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis?api-version=2019-12-01
    
    Authorization: Bearer ....................

    参考资料

    APIM REST API Document : Apis - Get -- https://docs.microsoft.com/en-us/rest/api/apimanagement/2019-12-01/apis/get

    当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!

  • 相关阅读:
    Python的历史
    python excel
    excel xdr wdr
    sql 常用命令
    selenium 配置firefox
    SQL 一直恢复状态解决方法
    sqlserver 学习
    ITCHAT用法
    健身卡属性,以及业务规则,
    安装REDIS
  • 原文地址:https://www.cnblogs.com/lulight/p/14978216.html
Copyright © 2011-2022 走看看