Use method in INodeProperties

Describe the problem/error/question

Hey n8n community, I developing a credentials node using OAuth2 protocol. Can you give me syntax to use method in INodeProperties or some idea to write logic for authUrl?

What is the error message (if any)?

image

Please share your workflow

This is my error syntax

{
			displayName: 'Authorization URL',
			name: 'authUrl',
			type: 'hidden',
			default:
				'=https://accounts.haravan.com/connect/authorize?response_mode=query&response_type=code&scope=openid org profile userinfo {{$self.["scope"].join(" ")}}&redirect_uri={{$self["redirectUri"]}}',
			placeholder: 'adsadsad',
			required: true,
		},

Hey @Hungnpv,

If you are creating OAuth credentials we will automatically add the response mode, type and scopes to the authorization url.

Check out the Hubspot OAuth credential for an example on how to do it.

1 Like

That’s also a good solution @Jon, but in my case, I don’t want to set the default scope like that, I want to let the user choose from the UI.

Hey @Hungnpv,

That takes a bit more work, Check out the GitHub credential to see how you can use parameters in the credential value.

You will want something like…

{
  displayName: 'Authorization URL',
  name: 'authUrl',
  type: 'hidden',
  default: '={{ "https://accounts.haravan.com/connect/authorize?response_mode=query&response_type=code&scope=openid org profile userinfo" + $self["scope"].join(" ") + "&redirect_uri=" + $self["redirectUri"]',
  placeholder: 'adsadsad',
  required: true,
},
1 Like

I was your syntax @Jon. Although It’s still not working but I think it close to resolve my solution. Can you give me another way ?

Hey @Hungnpv,

It could be that our oauth credential is expecting certain fields, Can you share the full credential and the error you are seeing?

This is my workflow @Jon, please review it.

/* eslint-disable n8n-nodes-base/cred-filename-against-convention */
import { ICredentialType, INodeProperties } from 'n8n-workflow';

// const scopes = [
// 	'com.write_shippings',
// 	'com.read_shippings',
// 	'com.write_orders',
// 	'com.read_orders',
// 	'com.write_shipping_zones',
// 	'com.read_shipping_zones',
// 	'com.write_products',
// 	'com.read_products',
// 	'com.write_customers',
// 	'com.read_customers',
// 	'com.write_inventories',
// 	'com.read_inventories',
// ];

// eslint-disable-next-line n8n-nodes-base/cred-filename-against-convention
export class HaravanOAuth2Api implements ICredentialType {
	name = 'haravanOAuth2Api';

	displayName = 'Haravan OAuth2 API';

	documentationUrl = 'https://docs.haravan.com/docs/get-started/create-app/';

	extends = ['oAuth2Api'];

	properties: INodeProperties[] = [
		{
			displayName: 'Grant Type',
			name: 'grantType',
			type: 'options',
			required: true,
			options: [
				{
					name: 'Authorization Code',
					value: 'authorizationCode',
				},
				{
					name: 'Hybird',
					value: 'hybird',
				},
			],
			default: 'authorizationCode',
		},
		{
			displayName: 'Scope',
			name: 'scope',
			type: 'multiOptions',
			required: true,
			options: [
				{
					name: 'Write Customers',
					value: 'com.write_customers',
				},
				{
					name: 'Read Customers',
					value: 'com.read_customers',
				},
				{
					name: 'Write Shippings',
					value: 'com.write_shippings',
				},
				{
					name: 'Read Shippings',
					value: 'com.read_shippings',
				},
				{
					name: 'Write Orders',
					value: 'com.write_orders',
				},
				{
					name: 'Read Orders',
					value: 'com.read_orders',
				},
				{
					name: 'Write Shippings Zones',
					value: 'com.write_shippings_zones',
				},
				{
					name: 'Read Shippings Zones',
					value: 'com.read_shippings_zones',
				},
				{
					name: 'Write Products',
					value: 'com.write_products',
				},
				{
					name: 'Read Products',
					value: 'com.read_products',
				},
				{
					name: 'Write Inventories',
					value: 'com.write_inventories',
				},
				{
					name: 'Read Inventories',
					value: 'com.read_inventories',
				},
			],
			default: [],
		},
		// {
		// 	displayName: 'Scope',
		// 	name: 'scope',
		// 	type: 'string',
		// 	default: scopes.join('  '),
		// },
		{
			displayName: 'Authorization URL',
			name: 'authUrl',
			type: 'hidden',
			default:
				'={{ "https://accounts.haravan.com/connect/authorize?response_mode=query&response_type=code&scope=openid org profile userinfo" + $self["scope"].join("  ") + "&redirect_uri=" + $self["redirectUri"]',
			placeholder: 'adsadsad',
			required: true,
		},
		{
			displayName: 'Access Token URL',
			name: 'accessTokenUrl',
			type: 'hidden',
			default: 'https://accounts.haravan.com/connect/token',
			required: true,
		},
		{
			displayName: 'Client ID',
			name: 'clientId',
			type: 'string',
			default: '',
			required: true,
		},
		{
			displayName: 'Client Secret',
			name: 'clientSecret',
			type: 'string',
			typeOptions: {
				password: true,
			},
			default: '',
			required: true,
		},

		{
			displayName: 'Auth URI Query Parameters',
			name: 'authQueryParameters',
			type: 'hidden',
			default: '',
			description:
				'For some services additional query parameters have to be set which can be defined here',
			placeholder: '',
		},
		{
			displayName: 'Authentication',
			name: 'authentication',
			type: 'hidden',
			default: 'body',
		},
		{
			displayName: 'Redirect Uri',
			name: 'redirectUri',
			type: 'string',
			required: true,
			default: '',
		},
	];
}

Could be put scope value in $self[“scope”].join(" ")

Hey @Hungnpv,

What error are you seeing? I also don’t think Hybrid is a grant type we support so I would suggest removing that option as it will likely cause issues if you try to use it.

The OAuth implmentation we are using will get the value of the scope field so I would probably give that a different name as well.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.