Why am I encountering an error when making calls to Google Ads API after successful authentication of my account?

Error getting access token: at Object.run (file:///tmp/__pdg__/dist/code/ebf60822dc1bb78a5ee0682fc43453bd038e171d915a4c4c1110d298514af857/component.mjs:53:40)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at null.executeComponent (/var/task/launch_worker.js:229:22)
at MessagePort.messageHandler (/var/task/launch_worker.js:726:28)

**import** { axios } **from** "@pipedream/platform";
**import** { GoogleAdsApi, enums } **from** 'google-ads-api';

export **default** defineComponent({
props: {
clientId: {
type: “string”,
label: “Client ID”,
},
clientSecret: {
type: “string”,
label: “Client Secret”,
},
refreshToken: {
type: “string”,
label: “Refresh Token”,
},
developerToken: {
type: “string”,
label: “Developer Token”,
},
customerId: {
type: “string”,
label: “Customer ID”,
},
languageId: {
type: “string”,
label: “Language ID”,
},
keywordTexts: {
type: “string[]”,
label: “Keyword Texts”,
},
pageUrl: {
type: “string”,
label: “Page URL”,
},
},
**async** run({ $ }) {
**let** accessToken;

`**try** {`
  `**const** tokenResponse = **await** axios($, {`
    method: "POST",
    url: "https://oauth2.googleapis.com/token",
    data: {
      client_id: `**this**.clientId,`
      client_secret: `**this**.clientSecret,`
      refresh_token: `**this**.refreshToken,`
      grant_type: "refresh_token",
    },
  });
  accessToken = tokenResponse.data.access_token;
} `**catch** (error) {`
  console.error("Error getting access token: ", error);
  `**return**;`
}

`**if** (!accessToken) {`
  console.error("Failed to obtain access token.");
  `**return**;`
}

`**const** client = **new** GoogleAdsApi({`
  client_id: `**this**.clientId,`
  client_secret: `**this**.clientSecret,`
  developer_token: `**this**.developerToken,`
  access_token: accessToken,
});

`**const** service = client.getService('KeywordPlanIdeaService');`

`**const** request = {`
  customer_id: `**this**.customerId,`
  language: `**this**.languageId,`
  include_adult_keywords: `**false**,`
  keyword_plan_network: enums.KeywordPlanNetwork.GOOGLE_SEARCH_AND_PARTNERS,
};

`**if** (**this**.keywordTexts && !**this**.pageUrl) {`
  request.keyword_seed = {
    keywords: `**this**.keywordTexts,`
  };
}

`**if** (!**this**.keywordTexts && **this**.pageUrl) {`
  request.url_seed = {
    url: `**this**.pageUrl,`
  };
}

`**if** (**this**.keywordTexts && **this**.pageUrl) {`
  request.keyword_and_url_seed = {
    url: `**this**.pageUrl,`
    keywords: `**this**.keywordTexts,`
  };
}

`**const** response = **await** service.generateKeywordIdeas(request);`

response.forEach((idea) => {
  `**const** competition = idea.keyword_idea_metrics.competition;`
  `**const** avgMonthlySearches = idea.keyword_idea_metrics.avg_monthly_searches;`
  console.log(`Keyword: ${idea.text}, Competition: ${competition}, Avg Monthly Searches: ${avgMonthlySearches}`);
});

`**return** response;`

},
});

It looks like you’re encountering an error while trying to obtain an access token. To help you debug this issue, I would recommend adding more detailed error logging to the catch block in your code. Replace the current console.error line with the following: ```
console.error("Error getting access token: ", error.response ? error.response.data : error);

10/1/2023, 7:54:50 PM
Error getting access token: at Object.run (file:///tmp/__pdg__/dist/code/d7961abac367d7c783758127878cbebd55621ca68bbcdce738e3ba20fa4afc01/component.mjs:53:40)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at null.executeComponent (/var/task/launch_worker.js:229:22)
at MessagePort.messageHandler (/var/task/launch_worker.js:726:28)

I’m sorry you’re experiencing an issue with obtaining an access token. To better understand the error, please update your catch block in your code with the following line: ```
console.error("Error getting access token: ", error.response ? error.response.data : error);

import { axios } from@pipedream/platform”;
import { GoogleAdsApi, enums } from ‘google-ads-api’;

export default defineComponent({
props: {
google_ads: {
type: “app”,
app: “google_ads”,
},
developerToken: {
type: “string”,
label: “Developer Token”
},
customerId: {
type: “string”,
label: “Customer ID”
},
languageId: {
type: “string”,
label: “Language ID”
},
keywordTexts: {
type: “string[]”,
label: “Keyword Texts”
},
pageUrl: {
type: “string”,
label: “Page URL”
}
},
async run({ $ }) {
const accessToken = this.google_ads.$auth.oauth_access_token;

**if** (!accessToken) {
  console.error("Failed to obtain access token.");
  **return**;
}

**const** client = **new** GoogleAdsApi({
  developer_token: **this**.developerToken,
  access_token: accessToken
});

**const** service = client.getService('KeywordPlanIdeaService');

**const** request = {
  customer_id: **this**.customerId,
  language: **this**.languageId,
  include_adult_keywords: **false**,
  keyword_plan_network: enums.KeywordPlanNetwork.GOOGLE_SEARCH_AND_PARTNERS
};

**if** (**this**.keywordTexts && !**this**.pageUrl) {
  request.keyword_seed = {
    keywords: **this**.keywordTexts
  };
}

**if** (!**this**.keywordTexts && **this**.pageUrl) {
  request.url_seed = {
    url: **this**.pageUrl
  };
}

**if** (**this**.keywordTexts && **this**.pageUrl) {
  request.keyword_and_url_seed = {
    url: **this**.pageUrl,
    keywords: **this**.keywordTexts
  };
}

**const** response = **await** service.generateKeywordIdeas(request);

response.forEach((idea) => {
  **const** competition = idea.keyword_idea_metrics.competition;
  **const** avgMonthlySearches = idea.keyword_idea_metrics.avg_monthly_searches;
  console.log(`Keyword: ${idea.text}, Competition: ${competition}, Avg Monthly Searches: ${avgMonthlySearches}`);
});

**return** response;

}
});

I am getting an error that this App is not Valid when trying to authenticate Google Ads account.

Here is PipeDream documentation:

Authentication
Google Ads uses OAuth authentication. When you connect your Google Ads account, Pipedream will open a popup window where you can sign into Google Ads and grant Pipedream permission to connect to your account. Pipedream securely stores and automatically refreshes the OAuth tokens so you can easily authenticate any Google Ads API.

Pipedream requests the following authorization scopes when you connect your account:

emailprofilehttps://www.googleapis.com/auth/adwords
OAuth Request Configurations:
authorization
GEThttps://accounts.google.com/o/oauth2/auth?client_id={{oauth.client_id}}&redirect_uri={{oauth.redirect_uri}}&state={{oauth.state}}&response_type=code&scope={{oauth.space_separated_scopes}}&prompt=consent&access_type=offline
accessToken
POSThttps://oauth2.googleapis.com/token
content-type: application/x-www-form-urlencoded
accept: application/json
client_id={{oauth.client_id}}&client_secret={{oauth.client_secret}}&redirect_uri={{oauth.redirect_uri}}&grant_type=authorization_code&code={{oauth.code}}
refreshToken
POSThttps://oauth2.googleapis.com/token
content-type: application/x-www-form-urlencoded
accept: application/json
client_id={{oauth.client_id}}&client_secret={{oauth.client_secret}}&grant_type=refresh_token&refresh_token={{oauth.refresh_token}}

Please help me fix my code

It seems like you’re trying to authenticate with Google Ads, but you’re using the wrong app name in the props. The correct app name for Google Ads is "google_ads" instead of "google_ads". Please update your code as follows: ```
import { axios } from “@pipedream/platform”; import { GoogleAdsApi, enums } from “google-ads-api”; export default defineComponent({ props: { google_ads: { type: “app”, app: “google_ads”, }, // Rest of the props }, async run({ $ }) { // Rest of the code }, });

import { axios } from@pipedream/platform”;
import { GoogleAdsApi, enums } from ‘google-ads-api’;

export default defineComponent({

props: {
google_ads: {
type: “app”,
app: “google_ads”,
},
developerToken: {
type: “string”,
label: “Developer Token”
},
customerId: {
type: “string”,
label: “Customer ID”
},
languageId: {
type: “string”,
label: “Language ID”
},
keywordTexts: {
type: “string[]”,
label: “Keyword Texts”
},
pageUrl: {
type: “string”,
label: “Page URL”
}
},

async run({ $ }) {
let accessToken;

_// Get OAuth access token using Pipedream's axios_
**try** {
  **const** userInfo = **await** axios($, {
    url: `https://www.googleapis.com/oauth2/v1/userinfo`,
    headers: {
      Authorization: `Bearer ${**this**.google_ads.$auth.oauth_access_token}`,
    },
  });
  accessToken = **this**.google_ads.$auth.oauth_access_token;
} **catch** (error) {
  console.error("Failed to obtain access token.");
  **return**;
}

_// Initialize GoogleAdsApi with the obtained access token_
**const** client = **new** GoogleAdsApi({
  developer_token: **this**.developerToken,
  access_token: accessToken
});

**const** service = client.getService('KeywordPlanIdeaService');

**const** request = {
  customer_id: **this**.customerId,
  language: **this**.languageId,
  include_adult_keywords: **false**,
  keyword_plan_network: enums.KeywordPlanNetwork.GOOGLE_SEARCH_AND_PARTNERS
};

**if** (**this**.keywordTexts && !**this**.pageUrl) {
  request.keyword_seed = {
    keywords: **this**.keywordTexts
  };
}

**if** (!**this**.keywordTexts && **this**.pageUrl) {
  request.url_seed = {
    url: **this**.pageUrl
  };
}

**if** (**this**.keywordTexts && **this**.pageUrl) {
  request.keyword_and_url_seed = {
    url: **this**.pageUrl,
    keywords: **this**.keywordTexts
  };
}

**const** response = **await** service.generateKeywordIdeas(request);

response.forEach((idea) => {
  **const** competition = idea.keyword_idea_metrics.competition;
  **const** avgMonthlySearches = idea.keyword_idea_metrics.avg_monthly_searches;
  console.log(`Keyword: ${idea.text}, Competition: ${competition}, Avg Monthly Searches: ${avgMonthlySearches}`);
});

**return** response;

}
});

The configuration is greyed out, with a message “App is not valid”

Please help

I apologize for the confusion earlier. It seems like there’s an issue with the app name in the props. Unfortunately, I don’t have information on a pre-built Google Ads app in Pipedream. In this case, you’ll need to use the Google Ads API directly with the access token you’ve obtained. However, I can help you set up the Google Ads API call using the access token. First, make sure you’ve followed the Google Ads API authentication guide to obtain the access token. Once you have the access token, you can use it in your Pipedream component like this: import { axios } from "@pipedream/platform"; import { GoogleAdsApi, enums } from "google-ads-api"; export default defineComponent({ props: { developerToken: { type: "string", label: "Developer Token", }, accessToken: { type: "string", label: "Access Token", }, customerId: { type: "string", label: "Customer ID", }, languageId: { type: "string", label: "Language ID", }, keywordTexts: { type: "string[]", label: "Keyword Texts", }, pageUrl: { type: "string", label: "Page URL", }, }, async run({ $ }) { // Initialize GoogleAdsApi with the obtained access token const client = new GoogleAdsApi({ developer_token: this.developerToken, access_token: this.accessToken, }); // Rest of the code }, }); Replace the google_ads prop with an accessToken prop, and use this.accessToken instead of this.google_ads.$auth.oauth_access_token. Please let me know if you need any further assistance or if you have any questions.

here is the info on the prebuilt google ads integration.

Please help me fix my code:

import { axios } from@pipedream/platform”
export default defineComponent({
props: {
google_ads: {
type: “app”,
app: “google_ads”,
}
},
async run({steps, $}) {
return await axios($, {
url: https://www.googleapis.com/oauth2/v1/userinfo,
headers: {
Authorization: Bearer ${**this**.google_ads.$auth.oauth_access_token},
},
})
},
})

Authentication

Google Ads uses OAuth authentication. When you connect your Google Ads account, Pipedream will open a popup window where you can sign into Google Ads and grant Pipedream permission to connect to your account. Pipedream securely stores and automatically refreshes the OAuth tokens so you can easily authenticate any Google Ads API.
Pipedream requests the following authorization scopes when you connect your account:
emailprofilehttps://www.googleapis.com/auth/adwords
OAuth Request Configurations:

  1. authorization
  2. **GET**https://accounts.google.com/o/oauth2/auth?client_id=**{{oauth.client_id}}**&redirect_uri=**{{oauth.redirect_uri}}**&state=**{{oauth.state}}**&response_type=**code**&scope=**{{oauth.space_separated_scopes}}**&prompt=**consent**&access_type=**offline**
  3. accessToken
  4. **POST**https://oauth2.googleapis.com/token
  5. content-type: application/x-www-form-urlencoded
  6. accept: **application/json**
  7. client_id=**{{oauth.client_id}}**&client_secret=**{{oauth.client_secret}}**&redirect_uri=**{{oauth.redirect_uri}}**&grant_type=**authorization_code**&code=**{{oauth.code}}**
  8. refreshToken
  9. **POST**https://oauth2.googleapis.com/token
  10. content-type: application/x-www-form-urlencoded
  11. accept: **application/json**
  12. client_id=**{{oauth.client_id}}**&client_secret=**{{oauth.client_secret}}**&grant_type=**refresh_token**&refresh_token=**{{oauth.refresh_token}}**

I apologize for the confusion earlier. It seems like there’s an issue with the app name in the props. The correct app name should be “google_ads” instead of “google”. Please update your code as follows: ```
import { axios } from “@pipedream/platform”; export default defineComponent({ props: { google_ads: { type: “app”, app: “google_ads”, }, }, async run({ steps, $ }) { return await axios($, { url: https://www.googleapis.com/oauth2/v1/userinfo, headers: { Authorization: Bearer ${this.google_ads.$auth.oauth_access_token}, }, }); }, });