Piece 认证

了解 Piece 认证

Piece 认证用于收集用户凭据并安全地存储以供将来在不同工作流中使用。 认证必须作为 auth 参数在 createPiececreateTriggercreateAction 函数中定义。

这一要求确保在触发器和动作中可以正确推断认证类型。

`createPiece`、`createTrigger` 和 `createAction` 函数的 auth 参数可以接受数组,但不能有多个相同类型的认证属性,即不能有两个 OAUTH2 属性。

秘密文本

此认证收集敏感信息,如密码或 API 密钥。显示为掩码输入字段。

示例:

PieceAuth.SecretText({
    displayName: 'API 密钥',
    description: '请输入你的 API 密钥',
    required: true,
    // 可选验证
    validate: async ({auth}) => {
        if(auth.startsWith('sk_')){
            return {
                valid: true,
            }
        }
        return {
            valid: false,
            error: '无效的 API 密钥'
        }
    }
})

用户名和密码

此认证收集用户名和密码作为单独的字段。

示例:

PieceAuth.BasicAuth({
    displayName: '凭据',
    description: '请输入你的用户名和密码',
    required: true,
    username: {
        displayName: '用户名',
        description: '请输入你的用户名',
    },
    password: {
        displayName: '密码',
        description: '请输入你的密码',
    },
    // 可选验证
    validate: async ({auth}) => {
        if(auth){
            return {
                valid: true,
            }
        }
        return {
            valid: false,
            error: '无效的 API 密钥'
        }
    }
})

自定义

此认证允许通过收集特定属性进行自定义认证,例如基础 URL 和访问令牌。

示例:

PieceAuth.CustomAuth({
    displayName: '自定义认证',
    description: '请输入自定义认证详情',
    props: {
        base_url: Property.ShortText({
            displayName: '基础 URL',
            description: '请输入基础 URL',
            required: true,
        }),
        access_token: PieceAuth.SecretText({
            displayName: '访问令牌',
            description: '请输入访问令牌',
            required: true
        })
    },
    // 可选验证
    validate: async ({auth}) => {
        if(auth){
            return {
                valid: true,
            }
        }
        return {
            valid: false,
            error: '无效的 API 密钥'
        }
    },
    required: true
})

OAuth2

此认证收集 OAuth2 认证详情,包括认证 URL、令牌 URL 和作用域。

示例:

PieceAuth.OAuth2({
    displayName: 'OAuth2 认证',
    grantType: OAuth2GrantType.AUTHORIZATION_CODE,
    required: true,
    authUrl: 'https://example.com/auth',
    tokenUrl: 'https://example.com/token',
    scope: ['read', 'write']
})
请注意 `OAuth2GrantType.CLIENT_CREDENTIALS` 也支持基于服务的认证。