|
const { google } = require('googleapis') |
|
const localservices = google.localservices('v1') |
|
const readline = require('readline') |
|
const { promises: fs } = require('fs') |
|
|
|
TOKEN_PATH = './token.json' |
|
|
|
const retrieveToken = async (path) => { |
|
try { |
|
const data = await fs.readFile(path, 'utf8') |
|
return JSON.parse(data) |
|
} catch (err) { |
|
return generateToken() |
|
} |
|
} |
|
|
|
const generateToken = async () => { |
|
const url = oauth2Client.generateAuthUrl({ |
|
// 'online' (default) or 'offline' (gets refresh_token) |
|
access_type: 'offline', |
|
// If you only need one scope you can pass it as a string |
|
// https://developers.google.com/identity/protocols/oauth2/scopes |
|
scope: ['https://www.googleapis.com/auth/adwords'], |
|
}) |
|
|
|
console.log('Authorize this app by visiting this url:', url) |
|
const rl = readline.createInterface({ |
|
input: process.stdin, |
|
output: process.stdout, |
|
}) |
|
|
|
const code = await new Promise((resolve) => { |
|
rl.question('Enter the code from that page here: ', (code) => resolve(code)) |
|
}) |
|
|
|
const { token } = await oauth2Client.getToken(code) |
|
|
|
await fs.writeFile(TOKEN_PATH, JSON.stringify(token)) |
|
|
|
return token |
|
} |
|
|
|
const generateOauth2Client = async () => { |
|
const oauth2Client = new google.auth.OAuth2( |
|
'<client_id>', |
|
'<client_secret>', |
|
'urn:ietf:wg:oauth:2.0:oob' // or <redirect_uris[0]> |
|
) |
|
|
|
oauth2Client.on('tokens', (tokens) => { |
|
if (tokens.access_token) { |
|
// store the new file |
|
fs.writeFile(TOKEN_PATH, JSON.stringify(tokens)) |
|
} |
|
// todo - add error handling |
|
}) |
|
|
|
credentials = await retrieveToken(TOKEN_PATH) |
|
|
|
oauth2Client.setCredentials(credentials) |
|
|
|
return oauth2Client |
|
} |
|
|
|
async function main() { |
|
const oauth2Client = await generateOauth2Client() |
|
|
|
// Acquire an auth client, and bind it to all future calls |
|
// const authClient = await auth.getClient(); |
|
google.options({ auth: oauth2Client }) |
|
// detailedLeadReports |
|
// Do the magic |
|
const res = await localservices.detailedLeadReports.search({ |
|
// // Day of month. Must be from 1 to 31 and valid for the year and month, or 0 if specifying a year by itself or a year and month where the day is not significant. |
|
// 'endDate.day': 23, |
|
// // Month of year. Must be from 1 to 12, or 0 if specifying a year without a month and day. |
|
// 'endDate.month': 9, |
|
// // Year of date. Must be from 1 to 9999, or 0 if specifying a date without a year. |
|
// 'endDate.year': 2020, |
|
'pageSize': 10, |
|
// // The maximum number of accounts to return. If the page size is unset, page size will default to 1000. Maximum page_size is 10000. Optional. |
|
// // pageSize: 'placeholder-value', |
|
// // The `next_page_token` value returned from a previous request to SearchDetailedLeadReports that indicates where listing should continue. Optional. |
|
// // pageToken: 'placeholder-value', |
|
// // A query string for searching for account reports. Caller must provide a customer id of their MCC account with an associated Gaia Mint that allows read permission on their linked accounts. Search expressions are case insensitive. Example query: | Query | Description | |-------------------------|-----------------------------------------------| | manager_customer_id:123 | Get Detailed Lead Report for Manager with id | | | 123. | Required. |
|
// query: "| manager_customer_id:XXXX | Get Detailed Lead Report for Manager with id XXXX. |", |
|
query: 'manager_customer_id:XXXX' |
|
// // Day of month. Must be from 1 to 31 and valid for the year and month, or 0 if specifying a year by itself or a year and month where the day is not significant. |
|
// 'startDate.day': 1, |
|
// // Month of year. Must be from 1 to 12, or 0 if specifying a year without a month and day. |
|
// 'startDate.month': 10, |
|
// // Year of date. Must be from 1 to 9999, or 0 if specifying a date without a year. |
|
// 'startDate.year': 2020 |
|
}) |
|
console.log(res.data) |
|
// Example response |
|
// { |
|
// "accountReports": [], |
|
// "nextPageToken": "my_nextPageToken" |
|
// } |
|
} |
|
|
|
main().catch((e) => { |
|
console.error(e) |
|
throw e |
|
}) |