Skip to content

Commit

Permalink
feat(Google Ads Node): Add new node (#3526)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Aël Gobrecht <[email protected]>
  • Loading branch information
3 people authored Jul 4, 2022
1 parent 637e815 commit 088daf9
Show file tree
Hide file tree
Showing 9 changed files with 511 additions and 18 deletions.
67 changes: 56 additions & 11 deletions packages/core/src/NodeExecuteFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ import {
LoggerProxy as Logger,
IExecuteData,
OAuth2GrantType,
IOAuth2Credentials,
} from 'n8n-workflow';

import { Agent } from 'https';
Expand All @@ -78,6 +77,7 @@ import { fromBuffer } from 'file-type';
import { lookup } from 'mime-types';

import axios, {
AxiosError,
AxiosPromise,
AxiosProxyConfig,
AxiosRequestConfig,
Expand Down Expand Up @@ -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>);
}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 (
Expand All @@ -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;
Expand Down Expand Up @@ -936,15 +946,53 @@ 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
newRequestOptions?.headers?.Authorization =
// @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
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -1141,7 +1187,6 @@ export async function httpRequestWithAuthentication(
node,
additionalData.timezone,
);

return await httpRequest(requestOptions);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
Expand Down
32 changes: 32 additions & 0 deletions packages/nodes-base/credentials/GoogleAdsOAuth2Api.credentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
ICredentialType,
INodeProperties,
} from 'n8n-workflow';

const scopes = [
'https://www.googleapis.com/auth/adwords',
];

export class GoogleAdsOAuth2Api implements ICredentialType {
name = 'googleAdsOAuth2Api';
extends = [
'googleOAuth2Api',
];
displayName = 'Google Ads OAuth2 API';
documentationUrl = 'google';
properties: INodeProperties[] = [
{
displayName: 'Developer Token',
name: 'developerToken',
type: 'string',
default: '',
},
{
displayName: 'Scope',
name: 'scope',
type: 'hidden',
default: scopes.join(' '),
},
];

}
Loading

0 comments on commit 088daf9

Please sign in to comment.