I am attempting to call the Local Services API, which is documented here: Local Services API documentation
I am calling the API inside of Apps Script, which is a way to write JavaScript inside of a Google Sheet. I have closely followed the tutorial that has been written to call other Google APIs in Apps Script, but I have not been able to call this one.
Here is the function to call the API:
function makeRequest() {
var adsService = getAdsService();
console.log(adsService.getAccessToken()) //I am getting a token back here
var response = UrlFetchApp.fetch('https://localservices.googleapis.com/v1/accountReports:search?query=manager_customer_id:{manager_customer_id}', {
headers: {
Authorization: 'Bearer ' + adsService.getAccessToken()
}
});}
The makeRequest()
function calls the getAdsService
function below. Inside of getAdsService
I am specifying ‘adwords’ and I have set the scope for .setScope('https://www.googleapis.com/auth/adwords')
function getAdsService() {
// Create a new service with the given name. The name will be used when
// persisting the authorized token, so ensure it is unique within the
// scope of the property store.
return OAuth2.createService('adwords')
// Set the endpoint URLs, which are the same for all Google services.
.setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')
.setTokenUrl('https://accounts.google.com/o/oauth2/token')
// Set the client ID and secret, from the Google Developers Console.
.setClientId(ClientID)
.setClientSecret(ClientSecret)
// Set the name of the callback function in the script referenced
// above that should be invoked to complete the OAuth flow.
.setCallbackFunction('authCallback')
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getUserProperties())
// Set the scopes to request (space-separated for Google services).
.setScope('https://www.googleapis.com/auth/adwords')
// Below are Google-specific OAuth2 parameters.
// Sets the login hint, which will prevent the account chooser screen
// from being shown to users logged in with multiple accounts.
.setParam('login_hint', Session.getEffectiveUser().getEmail())
// Requests offline access.
.setParam('access_type', 'offline')
// Consent prompt is required to ensure a refresh token is always
// returned when requesting offline access.
.setParam('prompt', 'consent');
}
There are also 2 other methods to redirect the user to authorize this application.
I believe the problem is the way I am structuring the request. I believe I need to supply more information to the API call in the makeRequest
function. I have seen a 'payload' : JSON.stringify(request, null, 2)
go in the headers section of the request function where request
specifies a specific set of instructions to call from the API but I do not know how to structure that for this case. A solution would be super appreciated.