【问题标题】:Protect an API by using OAuth 2.0 with Azure Active Directory and API Management通过将 OAuth 2.0 与 Azure Active Directory 和 API 管理结合使用来保护 API
【发布时间】:2019-09-06 15:49:46
【问题描述】:

我想通过将 OAuth 2.0 与 Azure Active Directory 和 API 管理结合使用来保护 amy API。 我在 API 管理中添加了我的 API,我正在关注这篇文章 https://docs.microsoft.com/en-in/azure/api-management/api-management-howto-protect-backend-with-aad。 该文档使用 Azure 开发人员控制台作为示例应用程序客户端来调用 API 端点,但是我如何创建自己的应用程序来生成身份验证代码,并且我可以使用该应用程序调用我的 API。 我尝试使用示例应用程序,但是我是新手,所以我不确定如何进一步。那么你能帮我提供任何python或节点基础代码吗? 谢谢

【问题讨论】:

  • 只有在您尝试过某些事情并遇到特定问题时,我们才能提供帮助。

标签: python node.js oauth-2.0 azure-active-directory azure-api-management


【解决方案1】:

不知道你卡在哪里了。如果您是 Python 中使用 Azure Active Directory 的 OAuth 2.0 的初学者,您可以先深入研究代码示例:Python authentication samples for Microsoft Graph,它展示了如何使用 Microsoft ADAL for Python 进行身份验证。请注意sample_adal.py文件。

@APP.route('/login/authorized')
def authorized():
    """Handler for the application's Redirect Uri."""
    code = flask.request.args['code']
    auth_state = flask.request.args['state']
    if auth_state != SESSION.auth_state:
        raise Exception('state returned to redirect URL does not match!')
    auth_context = adal.AuthenticationContext(config.AUTHORITY_URL, api_version=None)
    token_response = auth_context.acquire_token_with_authorization_code(
        code, config.REDIRECT_URI, config.RESOURCE, config.CLIENT_ID, config.CLIENT_SECRET)
    SESSION.headers.update({'Authorization': f"Bearer {token_response['accessToken']}",
                            'User-Agent': 'adal-sample',
                            'Accept': 'application/json',
                            'Content-Type': 'application/json',
                            'SdkVersion': 'sample-python-adal',
                            'return-client-request-id': 'true'})
    return flask.redirect('/graphcall')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-29
    • 2016-08-14
    • 2020-07-22
    • 2020-03-13
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多