Skip to content

Instantly share code, notes, and snippets.

@mayrop
Last active September 4, 2022 01:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mayrop/236e7ff55a7fb16b9b30437f00a52978 to your computer and use it in GitHub Desktop.
Save mayrop/236e7ff55a7fb16b9b30437f00a52978 to your computer and use it in GitHub Desktop.

Create Authentication

  • Using OAuth, go to Google Developer Console and create a project.
  • Follow the steps here. I used the Desktop version.
  • Download the JSON credentials file and store it

Adding the keys in the file (hardcoded for now)

  • <client_id>
  • <client_secret>

Getting your customer ID

  • Follow the steps here to get your customer ID
  • Note that you'll neex to remove the dashes, so 123-456-7890 should be 1234567890

Run Script

  • Run the script and add the OAuth token using the email account that handles the Google Local Services customers
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
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment