跳至内容

凭证文件#

凭证文件定义了节点的授权方法。该文件中的设置会影响n8n在凭证弹窗中显示的内容,并且必须反映您所连接服务的认证要求。

在凭证文件中,您可以使用所有n8n UI元素。n8n会使用加密密钥对存储在凭证中的数据进行加密。

凭证文件的结构#

凭证文件遵循以下基本结构:

  1. 导入语句
  2. 为凭据创建一个类
  3. 在类中定义控制节点认证的属性。

大纲结构#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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: 预期的数据类型,例如 string
  • default: n8n用于测试凭证的URL。

authenticate#

  • authenticate: 对象。包含告诉n8n如何将认证数据作为API请求的一部分注入的对象。

type#

字符串。如果您使用的身份验证方法在请求头、请求体或查询字符串中发送数据,请将此设置为'generic'

properties#

对象。定义认证方法。可选选项有:

  • body: 对象。在请求体中发送认证数据。可以包含嵌套对象。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    authenticate: IAuthenticateGeneric = {
    	type: 'generic',
    	properties: {
    		body: {
    			username: '={{$credentials.username}}',
    			password: '={{$credentials.password}}',
    		},
    	},
    };
    

  • header: 对象。在请求头中发送认证数据。

    1
    2
    3
    4
    5
    6
    7
    8
    authenticate: IAuthenticateGeneric = {
    	type: 'generic',
    	properties: {
    		header: {
    			Authorization: '=Bearer {{$credentials.authToken}}',
    		},
    	},
    };
    

  • qs: 对象。代表"查询字符串"。在请求查询字符串中发送认证数据。

    1
    2
    3
    4
    5
    6
    7
    8
    authenticate: IAuthenticateGeneric = {
    	type: 'generic',
    	properties: {
    		qs: {
    			token: '={{$credentials.token}}',
    		},
    	},
    };
    

  • auth: 对象。用于基本认证。需要将usernamepassword作为键名。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    authenticate: IAuthenticateGeneric = {
    	type: 'generic',
    	properties: {
    		auth: {
    			username: '={{$credentials.username}}',
    			password: '={{$credentials.password}}',
    		},
    	},
    };
    

test#

提供一个包含URL和认证类型的request对象,n8n可用其测试凭证。

1
2
3
4
5
6
test: ICredentialTestRequest = {
		request: {
			baseURL: '={{$credentials?.domain}}',
			url: '/bearer',
		},
	};
优云智算