> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ridergy.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 获取访问令牌

> 将客户端凭证兑换为短期 Bearer 令牌（3600秒）。

接受以下方式传递凭证：
- `Authorization: Basic base64(client_id:client_secret)` 并在
  请求体中包含 `grant_type=client_credentials`（RFC 6749）
- JSON 请求体，包含 `client_id`、`client_secret` 和 `grant_type`




## OpenAPI

````yaml api-reference/energy-management/openapi.zh.yaml POST /oauth/token
openapi: 3.1.0
info:
  title: RiDERgy Energy Management API
  version: '1.0'
  license:
    name: Proprietary
    identifier: LicenseRef-Proprietary
  description: |
    对位置、单个充电桩、组织以及跨位置集群的充电功率限制进行程序化控制。

    ## 身份验证
    除 `GET /health` 和 `POST /oauth/token` 外，所有接口都需要通过
    OAuth 2.0 client_credentials 流程获取的 Bearer 令牌：

    1. 在 RiDERgy 控制台中创建 OAuth 客户端凭证。
    2. 通过 `POST /oauth/token` 将其兑换为短期访问令牌。
    3. 在每个请求中以 `Authorization: Bearer <token>` 传递令牌。

    令牌在 3600 秒后过期。您的租户信息嵌入在令牌中，
    无法被伪造。一个租户的凭证无法访问另一个租户。

    您的租户必须已启用 Energy Management API 功能，否则
    请求会返回 `403 FEATURE_NOT_ENABLED` 错误。请联系 RiDERgy 以启用该功能。

    ## Scopes
    令牌携带控制可访问接口的 scopes：
    `locations:read`、`locations:write`、`clusters:read`、`clusters:write`、
    `chargers:write`、`org:write`。向需要令牌不具备的 scope 的接口
    发送请求将返回 `403 INSUFFICIENT_SCOPE` 错误。

    ## 优先级模型
    位置和充电桩的限制通过**带优先级时间窗口的配置文件（profile）**进行控制：

    - 优先级范围为 **0（最低）到 10（最高）**，与 OCPP 协议栈风格一致。
    - 一个目标对象（位置或充电桩连接器）最多可以拥有 11 个配置文件，每个
      优先级一个，每个配置文件都有自己的 `[startTime, endTime)` 时间窗口。
    - 在任意时刻，**生效限制**为当前处于激活状态的配置文件中
      优先级最高者的 `limitKw`。
    - 如果没有配置文件处于激活状态，位置会回退到其 `permanentLimitKw`。
    - 在某个目标对象的某个优先级上已存在配置文件时，再次提交该优先级的配置文件
      将**替换**原有配置文件。
    - 配置文件会在其 `startTime`/`endTime` 自动应用并撤销。配置文件创建后
      无需进行额外的 API 调用。配置文件记录永远不会被删除，因此历史记录
      始终可查询。

    ## 错误
    所有错误均采用以下结构：

    ```json
    {
      "error": {
        "code": "LOCATION_NOT_FOUND",
        "message": "Location 123 was not found for the provided tenant",
        "requestId": "b1f6e6b0-..."
      }
    }
    ```
servers:
  - url: https://api.ridergy.com/v1
    description: Production
security:
  - BearerAuth: []
tags:
  - name: Auth
    description: 用于 client_credentials 授权的 OAuth 2.0 令牌端点。
  - name: Health
    description: 无需身份验证的服务健康检查。
  - name: Locations
    description: 读取并设置位置级别的功率限制和优先级配置文件。
  - name: Chargers
    description: 在指定时间窗口内将单个充电桩连接器锁定为固定功率限制。
  - name: Organizations
    description: 租户范围内的总量限额配置文件（仅记录，尚未启用强制执行）。
  - name: Clusters
    description: 将多个位置归入一个共享 kW 上限的集群，并对其应用调度计划。
paths:
  /oauth/token:
    post:
      tags:
        - Auth
      summary: Obtain an access token
      description: |
        将客户端凭证兑换为短期 Bearer 令牌（3600秒）。

        接受以下方式传递凭证：
        - `Authorization: Basic base64(client_id:client_secret)` 并在
          请求体中包含 `grant_type=client_credentials`（RFC 6749）
        - JSON 请求体，包含 `client_id`、`client_secret` 和 `grant_type`
      operationId: createToken
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - grant_type
              properties:
                grant_type:
                  type: string
                  enum:
                    - client_credentials
                client_id:
                  type: string
                  description: 未使用 Basic auth 请求头时必填。
                  example: emcl_a1b2c3d4e5f67890a1b2c3d4e5f67890
                client_secret:
                  type: string
                  description: 未使用 Basic auth 请求头时必填。
                  example: emcs_...
            examples:
              jsonBody:
                value:
                  grant_type: client_credentials
                  client_id: emcl_a1b2c3d4e5f67890a1b2c3d4e5f67890
                  client_secret: emcs_a1b2c3d4...
      responses:
        '200':
          description: 访问令牌已签发
          content:
            application/json:
              schema:
                type: object
                properties:
                  access_token:
                    type: string
                    description: JWT Bearer 令牌
                    example: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
                  token_type:
                    type: string
                    example: Bearer
                  expires_in:
                    type: integer
                    description: 令牌有效期（秒）
                    example: 3600
                  scope:
                    type: string
                    description: 以空格分隔的已授权 scopes 列表
                    example: >-
                      locations:read locations:write clusters:read
                      clusters:write chargers:write org:write
        '400':
          description: 无效请求（缺少 grant_type 或不支持的授权类型）
          content:
            application/json:
              schema:
                type: object
                properties:
                  error:
                    type: string
                    example: unsupported_grant_type
                  error_description:
                    type: string
                    example: Only client_credentials grant type is supported
        '401':
          description: 客户端凭证无效
          content:
            application/json:
              schema:
                type: object
                properties:
                  error:
                    type: string
                    example: invalid_client
                  error_description:
                    type: string
                    example: Invalid client credentials
      security: []
components:
  securitySchemes:
    BearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
      description: |
        通过 `POST /oauth/token` 使用 client_credentials 授权获取的 OAuth 2.0 访问令牌。
        租户和 scopes 嵌入在 JWT 中。无需额外的请求头。

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.