凭据文件#
凭据文件用于定义节点的授权方法。此文件中的设置会影 响 n8n 在凭据模态窗口中显示的内容,且必须反映所连接服务的身份验证要求。
在凭据文件中,您可以使用所有 n8n UI 元素。n8n 会使用加密密钥对通过凭据存储的数据进行加密处理。
凭据文件结构#
凭据文件遵循以下基础结构:
- 导入语句
- 创建凭据类
- 在类中定义控制节点身份验证的属性
结构概览#
import {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
export class ExampleNode implements ICredentialType {
name = 'exampleNodeApi';
displayName = 'Example Node API';
documentationUrl = '';
properties: INodeProperties[] = [
{
displayName: 'API Key',
name: 'apiKey',
type: 'string',
default: '',
},
];
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
// Can be body, header, qs or auth
qs: {
// Use the value from `apiKey` above
'api_key': '={{$credentials.apiKey}}'
}
},
};
test: ICredentialTestRequest = {
request: {
baseURL: '={{$credentials?.domain}}',
url: '/bearer',
},
};
}
参数#
name#
字符串类型。对象的内部名称,用于在节点其他位置引用该对象。
displayName#
字符串类型。n8n在图形界面中使用的显示名称。
documentationUrl#
字符串类型。凭证说明文档的URL地址。
properties#
每个对象包含:
displayName:n8n在图形界面中使用的显示名称name:对象的内部名称,用于在节点其他位置引用该对象type:预期的数据类型,例如stringdefault:n8n用于测试凭证的默认URL
authenticate#
authenticate:对象类型。包含 指示n8n如何将认证数据注入API请求的对象
type#
字符串类型。如果使用在请求头、请求体或查询字符串中发送数据的认证方法,请将此值设置为'generic'
properties#
对象类型。定义认证方法,可选选项包括:
body:对象类型。在请求体中发送认证数据,可包含嵌套对象
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
body: {
username: '={{$credentials.username}}',
password: '={{$credentials.password}}',
},
},
};
header:对象。在请求头中发送认证数据。
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
header: {
Authorization: '=Bearer {{$credentials.authToken}}',
},
},
};
qs:对象。代表“查询字符串”。在请求查询字符串中发送认证数据。
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
qs: {
token: '={{$credentials.token}}',
},
},
};
auth:对象。用于基本认证。需要以username和password作为键名。
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
auth: {
username: '={{$credentials.username}}',
password: '={{$credentials.password}}',
},
},
};
test#
提供一个包含URL和认证类型的request对象,n8n可使用该凭证进行测试。
test: ICredentialTestRequest = {
request: {
baseURL: '={{$credentials?.domain}}',
url: '/bearer',
},
};