导入和发布第一个 APIImport and publish your first API

本教程介绍如何导入 http://conferenceapi.azurewebsites.cn?format=json 中的“OpenAPI 规范”后端 API。This tutorial shows how to import an "OpenAPI specification" backend API residing at http://conferenceapi.azurewebsites.cn?format=json. 此后端 API 由 Microsoft 提供并托管在 Azure 上。This backend API is provided by Microsoft and hosted on Azure.

后端 API 导入到 API 管理 (APIM) 之后,APIM API 即成为后端 API 的外观。Once the backend API is imported into API Management (APIM), the APIM API becomes a facade for the backend API. 在导入后端 API 时,源 API 和 APIM API 均相同。At the time you import the backend API, both the source API and the APIM API are identical. 通过 APIM,无需触摸后端 API 即可根据需要自定义外观。APIM enables you to customize the facade according to your needs without touching the backend API. 有关详细信息,请参阅转换和保护 APIFor more information, see Transform and protect your API.

本教程介绍如何执行下列操作:In this tutorial, you learn how to:

  • 导入第一个 APIImport your first API
  • 在 Azure 门户中测试 APITest the API in the Azure portal
  • 在开发人员门户中测试 APITest the API in the Developer portal

新的 API

先决条件Prerequisites

转到你的 API 管理实例Go to your API Management instance

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 选择“所有服务”。Select All services.

  3. 在搜索框中输入 apiIn the search box, enter api.

  4. 在搜索结果中,选择“API 管理服务”。In the search results, select API Management services.

  5. 选择自己的 API 管理服务实例。Select your API Management service instance.

Tip

若要将 API 管理添加到 Azure 门户中的收藏夹,请选择星形。To add API Management to your favorites in the Azure portal, select the star.

API 管理图标The API Management icon ((API 管理图标)现在显示在门户的左侧菜单中。) now appears in the left menu in the portal.

导入和发布后端 API Import and publish a backend API

本部分演示如何导入和发布 OpenAPI 规范后端 API。This section shows how to import and publish an OpenAPI specification backend API.

  1. 在“API 管理”下面选择“API”。Select APIs from under API MANAGEMENT.

  2. 从该列表中选择“OpenAPI 规范”,然后在弹出窗口中单击“完整”。Select OpenAPI specification from the list and click Full in the pop-up.

    创建 API

    可在创建时设置 API 或稍后转到“设置”选项卡进行设置。带有红色星号的字段是必填的。You can set the API values during creation or later by going to the Settings tab. The red star next to a field indicates that the field is required.

    使用下表中的值创建第一个 API。Use the values from the table below to create your first API.

    设置Setting Value 说明Description
    OpenAPI 规范OpenAPI Specification https://conferenceapi.azurewebsites.net?format=json 引用实现 API 的服务。References the service implementing the API. API 管理将请求转发到此地址。API management forwards requests to this address.
    显示名称Display name 演示会议 APIDemo Conference API 如果在输入服务 URL 后按 Tab 键,APIM 将根据 json 中的内容填充此字段。If you press tab after entering the service URL, APIM will fill out this field based on what is in the json.
    此名称显示在开发人员门户中。This name is displayed in the Developer portal.
    名称Name demo-conference-apidemo-conference-api 提供 API 的唯一名称。Provides a unique name for the API.
    如果在输入服务 URL 后按 Tab 键,APIM 将根据 json 中的内容填充此字段。If you press tab after entering the service URL, APIM will fill out this field based on what is in the json.
    说明Description 提供 API 的可选说明。Provide an optional description of the API. 如果在输入服务 URL 后按 Tab 键,APIM 将根据 json 中的内容填充此字段。If you press tab after entering the service URL, APIM will fill out this field based on what is in the json.
    URL 方案URL scheme HTTPSHTTPS 确定可用于访问 API 的协议。Determines which protocols can be used to access the API.
    API URL 后缀API URL suffix 会议conference 此后缀附加到 API 管理服务的基础 URL。The suffix is appended to the base URL for the API management service. API 管理通过其后缀区分 API,因此后缀对于给定发布者的每个 API 必须唯一。API Management distinguishes APIs by their suffix and therefore the suffix must be unique for every API for a given publisher.
    产品Products 不受限制Unlimited 产品是一个或多个 API 的关联。Products are associations of one or more APIs. 可在一个产品中包含多个 API,并通过开发人员门户将其提供给开发人员。You can include a number of APIs into a Product and offer them to developers through the developer portal.
    通过将 API 关联到某个产品(在本示例中为“无限制”)来发布该 API。You publish the API by associating the API with a product (in this example, Unlimited). 若要将此新 API 添加到产品,请键入产品名称(也可以稍后通过“设置”页执行此操作)。To add this new API to a product, type the product name (you can also do it later from the Settings page). 可以多次重复此步骤,将此 API 添加到多个产品。This step can be repeated multiple times to add the API to multiple products.
    开发人员必须先订阅产品才能访问 API。To get access to the API, developers must first subscribe to a product. 订阅时,他们会得到一个订阅密钥,此密钥对该产品中的任何 API 都有效。When they subscribe, they get a subscription key that is good for any API in that product.
    如果你创建了 APIM 实例,那么你已是管理员,因此订阅了每个产品。If you created the APIM instance, you are an administrator already, so you are subscribed to every product.
    默认情况下,每个 API 管理实例附带两个示例产品:初学者无限By default, each API Management instance comes with two sample products: Starter and Unlimited.
    标记Tags 用于组织 API 的标记。Tags for organizing APIs. 标记可用于搜索、分组或筛选。Tags can be used for searching, grouping, or filtering.
    对此 API 进行版本控制?Version this API? 有关版本控制的详细信息,请参阅发布 API 的多个版本For more information about versioning, see Publish multiple versions of your API

    Note

    若要发布 API,必须将其与某个产品相关联。To publish the API, you must associate it with a product. 可以从“设置”页执行此操作。You can do it from the Settings page.

  3. 选择“创建” 。Select Create.

Tip

如果在导入自己的 API 定义时遇到问题,请查看已知问题和限制的列表If you are experiencing problems with import of your own API definition, see the list of known issues and restrictions.

在 Azure 门户中测试新的 APIM APITest the new APIM API in the Azure portal

测试 API 映射

可直接从 Azure 门户调用操作,这样可以方便地查看和测试 API 的操作。Operations can be called directly from the Azure portal, which provides a convenient way to view and test the operations of an API.

  1. 从“API”选项卡选择在上一步骤中创建的 API。Select the API you created in the previous step (from the APIs tab).

  2. 按“测试”选项卡。Press the Test tab.

  3. 单击“GetSpeakers”。Click on GetSpeakers. 该页显示查询参数(在此示例中无)和标头的字段。The page displays fields for query parameters, in this case none, and headers. 其中一个标头是“Ocp-Apim-Subscription-Key”,适用于和此 API 关联的产品订阅密钥。One of the headers is "Ocp-Apim-Subscription-Key", for the subscription key of the product that is associated with this API. 将自动填充该密钥。The key is filled in automatically.

  4. 按“发送”。Press Send.

    后端以“200 正常”和某些数据做出响应。Backend responds with 200 OK and some data.

从开发人员门户调用操作 Call an operation from the developer portal

此外,也可以从开发人员门户调用操作来测试 API。Operations can also be called from the Developer portal to test APIs.

  1. 导航到开发人员门户Navigate to the Developer portal.

    开发人员门户

  2. 选择 API,依次单击“演示会议 API”和 GetSpeakersSelect APIS, click on Demo Conference API and then GetSpeakers.

    该页显示查询参数(在此示例中无)和标头的字段。The page displays fields for query parameters, in this case none, and headers. 其中一个标头是“Ocp-Apim-Subscription-Key”,适用于和此 API 关联的产品订阅密钥。One of the headers is "Ocp-Apim-Subscription-Key", for the subscription key of the product that is associated with this API. 如果创建了 APIM 实例,那么你已是管理员,因此会自动填充该密钥。If you created the APIM instance, you are an administrator already, so the key is filled in automatically.

  3. 按“试用”。Press Try it.

  4. 按“发送”。Press Send.

    调用操作后,开发人员门户将显示响应。After an operation is invoked, the developer portal shows the responses.

后续步骤 Next steps

在本教程中,你已学习了如何执行以下操作:In this tutorial, you learned how to:

  • 导入第一个 APIImport your first API
  • 在 Azure 门户中测试 APITest the API in the Azure portal
  • 在开发人员门户中测试 APITest the API in the Developer portal

转到下一教程:Advance to the next tutorial: