调用 Web API 的 Web 应用:调用 Web API

现在你已有令牌,可以调用受保护的 Web API 了。 通常从 Web 应用的控制器或页面调用下游 API。

调用受保护的 Web API

调用受保护的 Web API 取决于所选的语言和框架:

成功检索令牌后,代码使用请求包来查询 API 终结点并检索 JSON 结果。

@app.route("/call_downstream_api")
def call_downstream_api():
    token = auth.get_token_for_user(app_config.SCOPE)
    if "error" in token:
        return redirect(url_for("login"))
    # Use access token to call downstream api
    api_result = requests.get(
        app_config.ENDPOINT,
        headers={'Authorization': 'Bearer ' + token['access_token']},
        timeout=30,
    ).json()
    return render_template('display.html', result=api_result)

后续步骤

  • 通过在以下多部分教程系列中生成可用户登录的 ASP.NET Core Web 应用来了解详细信息

  • 浏览 Microsoft 标识平台 Web 应用示例