Skip to content

larksuite/oapi-sdk-java

Repository files navigation

飞书开放接口SDK/Feishu OpenPlatform Server SDK

旨在让开发者便捷的调用飞书开放API、处理订阅的事件、处理服务端推送的卡片行为等。

Feishu Open Platform offers a series of server-side atomic APIs to achieve diverse functionalities. However, actual coding requires additional work, such as obtaining and maintaining access tokens, encrypting and decrypting data, and verifying request signatures. Furthermore, the lack of semantic descriptions for function calls and type system support can increase coding burdens.

To address these issues, Feishu Open Platform has developed the Open Interface SDK, which incorporates all lengthy logic processes, provides a comprehensive type system, and offers a semantic programming interface to enhance the coding experience.

介绍文档 Introduction Documents

App Registration

The SDK provides RegisterApp.register(...) for one-click app creation based on OAuth 2.0 Device Authorization Grant (RFC 8628). It returns a verification URL that users can open in Feishu/Lark to authorize and automatically register an app, then obtain the app credentials without manually creating one in the developer console.

import com.lark.oapi.scene.registration.QRCodeInfo;
import com.lark.oapi.scene.registration.RegisterApp;
import com.lark.oapi.scene.registration.RegisterAppException;
import com.lark.oapi.scene.registration.RegisterAppOptions;
import com.lark.oapi.scene.registration.RegisterAppResult;

public class Sample {

    public static void main(String[] args) {
        try {
            RegisterAppResult result = RegisterApp.register(
                    RegisterAppOptions.newBuilder()
                            .source("test")
                            .onQRCode(info -> {
                                System.out.println("Please scan the QR code:");
                                System.out.println(info.getUrl());
                                System.out.println(String.format("Expires in %s seconds", info.getExpireIn()));
                            })
                            .onStatusChange(info -> System.out.println(
                                    "Status: " + info.getStatus()
                                            + (info.getInterval() > 0
                                            ? String.format(" (interval: %ss)", info.getInterval())
                                            : "")
                            ))
                            .build()
            );

            System.out.println("App ID: " + result.getClientId());
            System.out.println("App Secret: " + result.getClientSecret());
            System.out.println("User Info: " + result.getUserInfo());
        } catch (RegisterAppException e) {
            System.err.println("Failed: " + e.getCode() + " " + e.getDescription());
        }
    }
}

Real runnable demo:

RegisterAppOptions parameters

Parameter Description Type Required Default
source Source identifier, appended to the QR code URL source parameter as java-sdk/{source} String No -
domain Custom Feishu accounts base URL String No https://accounts.feishu.cn
larkDomain Custom Lark accounts base URL, used when tenant brand is detected as Lark String No https://accounts.larksuite.com
onQRCode Callback when the verification URL is ready. Receives QRCodeInfo with url and expireIn Consumer<QRCodeInfo> Yes -
onStatusChange Callback on polling status changes. Receives StatusChangeInfo Consumer<StatusChangeInfo> No -

Return value

Field Type Description
clientId String App ID
clientSecret String App Secret
userInfo UserInfo Scanning user info
userInfo.openId String User's open_id
userInfo.tenantBrand String "feishu" or "lark"

Status callback

Status Description
polling Authorization is still pending
slow_down The server asks the client to slow down polling; interval contains the new interval in seconds
domain_switched The SDK detected a Lark tenant and switched polling to larkDomain

Error handling

RegisterAppException contains code and description fields:

code Description
access_denied User denied the authorization
expired_token QR code expired or polling timed out
abort Registration was canceled via thread interruption
invalid_response The service returned an unexpected response

扩展示例

我们还基于 SDK 封装了常用的 API 组合调用及业务场景示例,如:

更多示例可参考:https://github.com/larksuite/oapi-sdk-java-demo

加入交流互助群

单击加入交流互助群

License

使用 MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages