mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
feat(Google Ads Node): Add new node (#3526)
* Add basic layout with icon for Google Ads * Add node versioning(V1) * Add node and credential to package * Add basic layout with icon for Google Ads * Add node versioning(V1) * Add node and credential to package * Add api call to getall * Fix formdata in the body for the request * N8N-2928 Added custom queries to campaign * Fix header bug and add developer-token field * Add operation and fields to campaign new format * Add more configurations and queries * Add Invoice ressources and operations * Remov old version from the node * Fixed bud with typo * Correctly prepends the baseURL * add query to invocie request * Fixes header not parsing the expression * Invoice param changes * Fixes bug related to headers not being parsed, and bug with auth * Remove useless imports * Added analytics to google ad node and removed useless header * Removed url for testing * Fixed inconsistent behaviour with the access token not being refreshed * Added placeholders to help user * Removed useless comments * Resolved name confusion * Added support for body in a GET method * Removed hyphens, parse body's expression * Renamed operation for clarity * Remove unused code * Removed invoice resource and fixed bug with body and headers The invoice operation was removed since it does not reflect what a user would expect from it. Google ADS invoices are only used for big advertisers where invoicing is performed after the end of the month and for big sums. This would be misleading for the majority of the users expecting an expenses report. Also fixed a bug with header and body being sent since it was broken for multiple input rows. The first execution would override all others. Lastly, made some improvements to the node itself by transforming data, adding filters and operations. * Improve campagin operation and remove analytics; fix tests * Improve tooltips and descriptions * Fix lint issues * Improve tooltip to explain amounts in micros * Change wording for micros * Change the fix to a more elegant solution Co-authored-by: Cyril Gobrecht <cyril.gobrecht@gmail.com> Co-authored-by: Aël Gobrecht <ael.gobrecht@gmail.com>
This commit is contained in:
@@ -58,7 +58,6 @@ import {
|
||||
LoggerProxy as Logger,
|
||||
IExecuteData,
|
||||
OAuth2GrantType,
|
||||
IOAuth2Credentials,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { Agent } from 'https';
|
||||
@@ -78,6 +77,7 @@ import { fromBuffer } from 'file-type';
|
||||
import { lookup } from 'mime-types';
|
||||
|
||||
import axios, {
|
||||
AxiosError,
|
||||
AxiosPromise,
|
||||
AxiosProxyConfig,
|
||||
AxiosRequestConfig,
|
||||
@@ -731,6 +731,10 @@ function convertN8nRequestToAxios(n8nRequest: IHttpRequestOptions): AxiosRequest
|
||||
axiosRequest.headers = axiosRequest.headers || {};
|
||||
axiosRequest.headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
||||
}
|
||||
} else if (
|
||||
axiosRequest.headers[existingContentTypeHeaderKey] === 'application/x-www-form-urlencoded'
|
||||
) {
|
||||
axiosRequest.data = new URLSearchParams(n8nRequest.body as Record<string, string>);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -761,6 +765,12 @@ async function httpRequest(
|
||||
requestOptions: IHttpRequestOptions,
|
||||
): Promise<IN8nHttpFullResponse | IN8nHttpResponse> {
|
||||
const axiosRequest = convertN8nRequestToAxios(requestOptions);
|
||||
if (
|
||||
axiosRequest.data === undefined ||
|
||||
(axiosRequest.method !== undefined && axiosRequest.method.toUpperCase() === 'GET')
|
||||
) {
|
||||
delete axiosRequest.data;
|
||||
}
|
||||
const result = await axios(axiosRequest);
|
||||
if (requestOptions.returnFullResponse) {
|
||||
return {
|
||||
@@ -886,7 +896,7 @@ export async function requestOAuth2(
|
||||
oAuth2Options?: IOAuth2Options,
|
||||
isN8nRequest = false,
|
||||
) {
|
||||
const credentials = (await this.getCredentials(credentialsType)) as unknown as IOAuth2Credentials;
|
||||
const credentials = await this.getCredentials(credentialsType);
|
||||
|
||||
// Only the OAuth2 with authorization code grant needs connection
|
||||
if (
|
||||
@@ -897,10 +907,10 @@ export async function requestOAuth2(
|
||||
}
|
||||
|
||||
const oAuthClient = new clientOAuth2({
|
||||
clientId: credentials.clientId,
|
||||
clientSecret: credentials.clientSecret,
|
||||
accessTokenUri: credentials.accessTokenUrl,
|
||||
scopes: credentials.scope.split(' '),
|
||||
clientId: credentials.clientId as string,
|
||||
clientSecret: credentials.clientSecret as string,
|
||||
accessTokenUri: credentials.accessTokenUrl as string,
|
||||
scopes: (credentials.scope as string).split(' '),
|
||||
});
|
||||
|
||||
let oauthTokenData = credentials.oauthTokenData as clientOAuth2.Data;
|
||||
@@ -936,7 +946,6 @@ export async function requestOAuth2(
|
||||
// Signs the request by adding authorization headers or query parameters depending
|
||||
// on the token-type used.
|
||||
const newRequestOptions = token.sign(requestOptions as clientOAuth2.RequestObject);
|
||||
|
||||
// If keep bearer is false remove the it from the authorization header
|
||||
if (oAuth2Options?.keepBearer === false) {
|
||||
// @ts-ignore
|
||||
@@ -944,7 +953,46 @@ export async function requestOAuth2(
|
||||
// @ts-ignore
|
||||
newRequestOptions?.headers?.Authorization.split(' ')[1];
|
||||
}
|
||||
|
||||
if (isN8nRequest) {
|
||||
return this.helpers.httpRequest(newRequestOptions).catch(async (error: AxiosError) => {
|
||||
if (error.response?.status === 401) {
|
||||
Logger.debug(
|
||||
`OAuth2 token for "${credentialsType}" used by node "${node.name}" expired. Should revalidate.`,
|
||||
);
|
||||
const tokenRefreshOptions: IDataObject = {};
|
||||
if (oAuth2Options?.includeCredentialsOnRefreshOnBody) {
|
||||
const body: IDataObject = {
|
||||
client_id: credentials.clientId as string,
|
||||
client_secret: credentials.clientSecret as string,
|
||||
};
|
||||
tokenRefreshOptions.body = body;
|
||||
tokenRefreshOptions.headers = {
|
||||
Authorization: '',
|
||||
};
|
||||
}
|
||||
const newToken = await token.refresh(tokenRefreshOptions);
|
||||
Logger.debug(
|
||||
`OAuth2 token for "${credentialsType}" used by node "${node.name}" has been renewed.`,
|
||||
);
|
||||
credentials.oauthTokenData = newToken.data;
|
||||
// Find the credentials
|
||||
if (!node.credentials || !node.credentials[credentialsType]) {
|
||||
throw new Error(
|
||||
`The node "${node.name}" does not have credentials of type "${credentialsType}"!`,
|
||||
);
|
||||
}
|
||||
const nodeCredentials = node.credentials[credentialsType];
|
||||
await additionalData.credentialsHelper.updateCredentials(
|
||||
nodeCredentials,
|
||||
credentialsType,
|
||||
credentials,
|
||||
);
|
||||
const refreshedRequestOption = newToken.sign(requestOptions as clientOAuth2.RequestObject);
|
||||
return this.helpers.httpRequest(refreshedRequestOption);
|
||||
}
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
return this.helpers.request!(newRequestOptions).catch(async (error: IResponseError) => {
|
||||
const statusCodeReturned =
|
||||
oAuth2Options?.tokenExpiredStatusCode === undefined
|
||||
@@ -1081,7 +1129,6 @@ export async function requestOAuth1(
|
||||
|
||||
// @ts-ignore
|
||||
requestOptions.headers = oauth.toHeader(oauth.authorize(requestOptions, token));
|
||||
|
||||
if (isN8nRequest) {
|
||||
return this.helpers.httpRequest(requestOptions as IHttpRequestOptions);
|
||||
}
|
||||
@@ -1103,7 +1150,6 @@ export async function httpRequestWithAuthentication(
|
||||
) {
|
||||
try {
|
||||
const parentTypes = additionalData.credentialsHelper.getParentTypes(credentialsType);
|
||||
|
||||
if (parentTypes.includes('oAuth1Api')) {
|
||||
return await requestOAuth1.call(this, credentialsType, requestOptions, true);
|
||||
}
|
||||
@@ -1141,7 +1187,6 @@ export async function httpRequestWithAuthentication(
|
||||
node,
|
||||
additionalData.timezone,
|
||||
);
|
||||
|
||||
return await httpRequest(requestOptions);
|
||||
} catch (error) {
|
||||
throw new NodeApiError(this.getNode(), error);
|
||||
|
||||
Reference in New Issue
Block a user