

# 教程：创建 REST API ConnectionType 和连接
<a name="rest-api-example"></a>

**连接到 Foo REST API**

 我们将为 Foo REST API 创建 AWS Glue REST API 连接类型和相应的 AWS Glue 连接。此 API 具有下面的属性（可从 REST API 文档中检索）。
+  **实例 URL**：https://foo.cloud.com/rest/v1。
+  **身份验证类型**：OAuth2（客户端凭证）。
+  **REST 方法**：GET。
+  **分页类型**：在请求的查询参数中放置“limit”和“offset”属性的偏移量。
+ **支持的实体**：
  +  **Bar**：相对路径 [/bar.json]。
  +  **Baz**：相对路径 [/baz.json]。

 获得所有详细信息后，我们就可以开始创建与 Foo REST API 的 AWS Glue 连接了。

**要创建 REST API 连接，请执行下列操作**：

1.  使用 AWS API、CLI 或 SDK 调用 RegisterConnectionType API 来在 AWS Glue 中创建 REST API 连接类型。这将在 AWS Glue 中创建新的 ConnectionType 资源。

   ```
   {
       "ConnectionType": "REST-FOO-CONNECTOR",
       "IntegrationType": "REST",
       "Description": "AWS Glue Connection Type for the FOO REST API",
       "ConnectionProperties": {
           "Url": {
               "Name": "Url",
               "Required": true,
               "DefaultValue": "https://foo.cloud.com/rest/v1",
               "PropertyType": "USER_INPUT"
           }
       },
       "ConnectorAuthenticationConfiguration": {
           "AuthenticationTypes": ["OAUTH2"],
           "OAuth2Properties": {
               "OAuth2GrantType": "CLIENT_CREDENTIALS"
           }
       },
       "RestConfiguration": {
           "GlobalSourceConfiguration": {
           "RequestMethod": "GET",
           "ResponseConfiguration": {
               "ResultPath": "$.result",
               "ErrorPath": "$.error.message"
           },
           "PaginationConfiguration": {
               "OffsetConfiguration": {
                   "OffsetParameter": {
                       "Key": "offset",
                       "PropertyLocation": "QUERY_PARAM"
                   },
                   "LimitParameter": {
                       "Key": "limit",
                       "PropertyLocation": "QUERY_PARAM",
                       "DefaultValue": "50"
                   }
               }
           }
       },
       "ValidationEndpointConfiguration": {
           "RequestMethod": "GET",
           "RequestPath": "/bar.json?offset=1&limit=10"
       },
       "EntityConfigurations": {
           "bar": {
               "SourceConfiguration": {
                   "RequestMethod": "GET",
                   "RequestPath": "/bar.json",
                   "ResponseConfiguration": {
                       "ResultPath": "$.result",
                       "ErrorPath": "$.error.message"
                   }
               },
               "Schema": {
                   "name": {
                       "Name": "name",
                       "FieldDataType": "STRING"
                   },
                   "description": {
                       "Name": "description",
                       "FieldDataType": "STRING"
                   },
                   "id": {
                       "Name": "id",
                       "FieldDataType": "STRING"
                   },
                   "status": {
                       "Name": "status",
                       "FieldDataType": "STRING"
                   }
               }
           }
       }
   }
   }
   ```

1.  在 AWS Secrets Manager 中，创建密钥。密钥应包含关联应用程序的使用者密钥，且以 `USER_MANAGED_CLIENT_APPLICATION_CLIENT_SECRET` 为键。
**注意**  
您必须在 AWS Glue 中为每个连接创建密钥

1.  使用 AWS API、CLI 或 SDK 调用 CreateConnection API 来创建 AWS Glue 连接。

   1.  将步骤 1 中的 REST 连接类型名称引用为“ConnectionType”。

   1.  提供 InstanceUrl 以及在 AWS Glue ConnectionType 注册过程中定义的任何其他 ConnectionProperties。

   1.  从配置的身份验证类型中进行选择。REST API Foo 使用授权类型为 ClientCredentials 的 OAuth2。

   1.  提供 **SecretArn** 和配置的其他 **AuthenticationProperties**。例如，我们已将 `OAUTH2` 配置为 AuthenticationType，因此我们将在 CreateConnectionInput 中设置“OAuth2Properties”。这将需要“OAuth2GrantType”、“TokenUrl”和“OAuth2ClientApplication”等属性。

1.  发出 CreateConnection 请求，这将创建 AWS Glue 连接。

   ```
   {
       "ConnectionInput": {
           "Name": "ConnectionFooREST",
           "ConnectionType": "REST-FOO-CONNECTOR",
           "ConnectionProperties": {},
           "ValidateCredentials": true,
           "AuthenticationConfiguration": {
               "AuthenticationType": "OAUTH2",
               "SecretArn": "arn:aws:secretsmanager:<region>:<accountId>:secret:<secretId>",
               "OAuth2Properties": {
                   "OAuth2GrantType": "CLIENT_CREDENTIALS",
                   "TokenUrl": "https://foo.cloud.com/oauth/token",
                   "OAuth2ClientApplication": {
                       "UserManagedClientApplicationClientId": "your-managed-client-id"
                   }
               }
           }
       }
   }
   ```