FastComments.com

FastComments Java SDK


这是 FastComments 的官方 Java SDK。

FastComments API 的官方 Java SDK

仓库

在 GitHub 上查看


安装 Internal Link

Maven

将 Repsy 仓库添加到您项目的 POM:

<repositories>
    <repository>
        <id>repsy</id>
        <name>FastComments Maven Repository on Repsy</name>
        <url>https://repo.repsy.io/mvn/winrid/fastcomments</url>
    </repository>
</repositories>

然后添加您需要的依赖:

<dependencies>
    <!-- API Client -->
    <dependency>
        <groupId>com.fastcomments</groupId>
        <artifactId>client</artifactId>
        <version>0.0.2</version>
    </dependency>

    <!-- Core Library (includes SSO) -->
    <dependency>
        <groupId>com.fastcomments</groupId>
        <artifactId>core</artifactId>
        <version>0.0.2</version>
    </dependency>

    <!-- PubSub Library (for live events) -->
    <dependency>
        <groupId>com.fastcomments</groupId>
        <artifactId>pubsub</artifactId>
        <version>0.0.2</version>
    </dependency>
</dependencies>

Gradle

将 Repsy 仓库添加到您的 build.gradle 文件:

repositories {
    mavenCentral()
    maven {
        url "https://repo.repsy.io/mvn/winrid/fastcomments"
    }
}

dependencies {
    // API Client
    implementation "com.fastcomments:client:0.0.2"

    // Core Library (includes SSO)
    implementation "com.fastcomments:core:0.0.2"

    // PubSub Library (for live events)
    implementation "com.fastcomments:pubsub:0.0.2"
}

库内容

该库包含三个模块:生成的 API 客户端、包含手工编写实用工具以简化与 API 交互的核心 Java 库,以及用于订阅变更推送的 pubsub 模块。

公共 API 与 受保护 API

对于 API 客户端,有两个类:DefaultApiPublicApiDefaultApi 包含需要使用您的 API 密钥的方法,而 PublicApi 包含可直接从浏览器/移动设备等在无身份验证情况下调用的 API。

快速开始 Internal Link

使用已认证的 API(DefaultApi)

重要: 在进行需要认证的请求之前,您必须在 ApiClient 上设置您的 API 密钥。如果不这样,请求将返回 401 错误。

import com.fastcomments.invoker.ApiClient;
import com.fastcomments.invoker.ApiException;
import com.fastcomments.api.DefaultApi;
import com.fastcomments.model.*;

public class Example {
    public static void main(String[] args) {
        // 创建并配置 API 客户端
        ApiClient apiClient = new ApiClient();

        // 必需:设置您的 API 密钥(可从 FastComments 仪表板获取)
        apiClient.setApiKey("YOUR_API_KEY_HERE");

        // 使用已配置的客户端创建 API 实例
        DefaultApi api = new DefaultApi(apiClient);

        // 现在您可以进行已认证的 API 调用
        try {
            // 示例:添加一个 SSO 用户
            CreateAPISSOUserData userData = new CreateAPISSOUserData();
            userData.setId("user-123");
            userData.setEmail("user@example.com");
            userData.setDisplayName("John Doe");

            AddSSOUserAPIResponse response = api.addSSOUser("YOUR_TENANT_ID", userData)
                .execute();
            System.out.println("User created: " + response);

        } catch (ApiException e) {
            System.err.println("Error: " + e.getResponseBody());
            // 常见错误:
            // - 401:API 密钥缺失或无效
            // - 400:请求验证失败
        }
    }
}

使用公共 API(PublicApi)

公共端点不需要认证:

import com.fastcomments.api.PublicApi;
import com.fastcomments.invoker.ApiException;

PublicApi publicApi = new PublicApi();

try {
    var response = publicApi.getCommentsPublic("YOUR_TENANT_ID", "page-url-id")
        .execute();
    System.out.println(response);
} catch (ApiException e) {
    e.printStackTrace();
}

常见问题

  1. 401 "missing-api-key" 错误:确保在创建 DefaultApi 实例之前调用 apiClient.setApiKey("YOUR_KEY")
  2. 错误的 API 类:对服务器端需要认证的请求使用 DefaultApi,对客户端/公共请求使用 PublicApi
  3. 空 API 密钥:如果 API 密钥为 null,SDK 会静默跳过认证,导致 401 错误。

说明 Internal Link

广播 ID

你会看到在某些 API 调用中需要传入 broadcastId。当你接收到事件时,会收到这个 ID 返回,所以如果你打算在客户端乐观地应用更改,就可以据此忽略该事件(你可能会想这么做,因为它提供了最佳体验)。传入 UUID。该 ID 应足够唯一,以免在同一浏览器会话中出现两次。

需要帮助?

如果您在使用 Java SDK 时遇到任何问题或有任何疑问,请:

贡献

欢迎贡献!请访问 GitHub 仓库 获取贡献指南。