Use Adwords API (MccApp) in Google Apps Script - google-apps-script

I have a custom API written in GAS (Google Apps Script) and would like to utilize the Adwords API from within it.
Sometimes used along with the MccApp, the service is readily available from within Adwords Scripts itself (My Client Center > Scripts).
For Example:
function account(client) {
var result = {
'id': null,
'campaigns': {}
}
result.id = client.getCustomerId()
var currentAct = AdWordsApp.currentAccount()
MccApp.select(client)
var campaignIterator = AdWordsApp.campaigns().get()
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next()
result.campaigns[campaign.getName()] = campaign.getId()
}
MccApp.select(currentAct)
return result
}
However, this API is not readily available inside a Google Apps Script. I have tried enabling it under "Resources > Advanced Google Services" and also under the developer console, but the UI offers no option that I can see.
QUESTION: Is it possible to enable use of the AdwordsApp and MccApp inside a Google Apps script so that the above code snippet would work in GAS?
If not, I understand already there are two workarounds:
Just use Adwords Script
Communicate with the API from GAS as though it were an external service (i.e... using SOAP, REST, etc...)

After much research, there really is no way to add the MccApp and AdwordsApp services for use in a Google Apps script. The nearest solution is to communicate with the API as though it were external or just use an Adwords Script.

It looks like you might be able to get to the AdWord API through the Management API:
See the Conceptual Overview section:
AdWords Links can be constructed at the Web Property level.
Google Developer Guide - What Is The Management API - Overview
and you can get to the Management API with the Google Analytics API.
Quote:
The Analytics service allows you to use the Google Analytics
Management API and Reporting APIs in Apps Script
Google Documentation - Google Analytics API
So, you need to use the RESOURCES menu, Choose, ADVANCED GOOGLE SERVICES, and then turn the Google Analytics API on.
With the Google Analytics API, you can access the Management API.

Related

Google App Script Advanced Service: Drive API

https://spreadsheet.dev/automatically-convert-excel-spreadsheets-to-google-sheets-using-apps-script
On the page above, Drive API is introduced to convert excel file to Google Sheet
let blob = excelFile.getBlob();
let config = {
title: "[Google Sheets] " + excelFile.getName(),
parents: [{id: excelFile.getParents().next().getId()}],
mimeType: MimeType.GOOGLE_SHEETS
};
let spreadsheet = Drive.Files.insert(config, blob);
I've been checking Google Drive API's ducumentation and references, but couldn't find anything regarding Drive.Files and Drive.Files.insert. Can someone direct me to the right documentation so I can learn to use these interfaces myself? Thanks!
Unfortunately, it seems that the detailed document of Drive API of Advanced Google services is not been officially published. But, in your situation, I thought that the document at the autocomplete of the script editor of Google Apps Script might be useful.
When the script editor of Google Apps Script is used, you can use the autocomplete of each method. In this case, when Drive API is enabled at Advanced Google services, this autocomplete can be also used for the methods of Drive API. When this is used, the documents of each method can be seen as follows. As a sample, Drive.Files.insert is used.
This completion includes an explanation of the method. In the case of Drive.Files.insert, it is found that it is Insert a new file. and the arguments are
resource: Drive_v2.Drive.V2.Schema.File
This is a request body of "Files: insert" of Drive API v2 Ref
mediaData: Blob
This is a Blob.
optionalArgs: Object
This is the query parameter of "Files: insert" of Drive API v2 Ref
, respectively. From this, it is found that this is Drive API v2. And, Drive_v2.Drive.V2.Schema.File is returned. This can be seen at here.
I thought that this document of the autocomplete with the script editor might be useful for understanding how to use Drive API at Advanced Google services.
Note:
This autocomplete can be also used for all APIs (Sheets API, Docs API, and so on) of Advanced Google services. I think that the document shown with the autocomplete with the script editor might be useful for understanding how to use the APIs at Advanced Google services.
References:
Advanced Google services
Using autocomplete
You may look into the use of Drive.Files and Drive.Files.insert under Advanced Drive Service.
It is somewhat the same use of Drive API, but the provided code on the reference you have provided leans toward using Drive API on App Script (which has a somewhat different syntax as oppose to using Drive API with other languages)
References:
https://developers.google.com/apps-script/advanced/drive
https://developers.google.com/drive/api/v2/reference/files/insert
https://developers.google.com/drive/api/samples

Can anything done with google apps scripts be done with the google API?

I noticed that the Google Apps Script editor uses a Bearer token to make requests for a lot of things, that got me thinking: Is it possible to do all functions that are done with the google apps script with the google API on your own server? What is the essential difference? Just the fact that google apps script runs on google servers, while with the google API you would need to create your own servers?
For example many of the document functions, which I thought could only be done with the API, turns out can be found here https://developers.google.com/docs/api/reference/rest/v1/documents/request#InsertInlineImageRequest
so whats the essential difference?
Just the fact that google apps script runs on google servers, while with the google API you would need to create your own servers?
Yes. There are advantages to both methods:
API:
It's a rest API. You can use your own language(eg: Python) in your servers.
The api is more inclusive. There are stuff which can be done with the api, which cannot be accomplished with apps script.
User can set limited scopes for using API.
For example, at Drive API, https://www.googleapis.com/auth/drive.file can be used for Drive API. The same cannot be done with DriveApp of apps script.
Process cost of API is lower than that of the built-in methods for Google Apps Script1 2
Apps script:
No server on your side
Triggers. You can set up functions to run onEdit,onOpen or at a specific time.
Deep integration with Google apps: Sidebars/modal dialogs can only be done with apps script.
All methods of the api can be accessed indirectly through the bearer token.
Authorization/authentication is taken care of by apps script. You don't need to set up oauth.

Google Docs API as Google Apps Script "Advanced Google Service"

I know I can use the Google Docs API along with the UrlFetchApp service. But does anyone know if the Google Docs API will be available as an "Advanced Google Service" in Google Apps Script, if so, can you tell when this will happen?
I can't say for sure (I don't work at Google) but there are information channels you can monitor for news and product updates related to Google Apps Script and GSuite products:
Google Apps Script Release Notes
What's New in GSuite
GSuite Updates
Besides, "Advanced Services" are just wrappers for their respective APIs and their main purpose is to provide code completion. Under the hood, you consume the same resources as you would if you called the API directly via UrlFetch (this includes usage quotas).
If you're up to the task you can write your own implementation based on the discovery docs available for the Google Docs API.

Authorizing Google Charts to access private spreadsheets

I am trying to create a Web App using Google Apps Script to query data from my Google Sheet using Google Charts.
I have been able to successfully query the spreadsheet when the Google Sheet is publicly shared, however since the spreadsheet contains confidential info I would rather do it privately using authorizations.
The reason why I want to use Google Charts visualisation functions (as opposed to the server side Spreadsheet App) is because of the speed of querying large data sets.
I have tried following the steps in the above documentation. That is, creating a client id then using the gapi.auth library to authenticate myself but I continue to receive an error.
When i add the authorization library and first part of the code from the documentation (with console.log simply to see where it get's up to):
<script src="https://apis.google.com/js/auth.js?onload=init"></script>
<script>
var clientId = '1234.apps.googleusercontent.com';
var scopes = 'https://spreadsheets.google.com/feeds';
function init() {
console.log("here");
gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true},handleAuthResult);
}
</script>
I receive the following error:
1289869776-mae_html_user_bin_i18n_mae_html_user.js:41 dropping
postMessage.. was from unexpected window
Any guidance is appreciated.
Since you are creating your web app using Google Apps Script, it's not necessary to "authorize Google Charts to access private charts" because you could use Google Apps Script services and methods to get the data from the spreadsheets and pass them to Google Charts.
On Converting from UiApp + Chart Service to Html Service + Google Visualization API it's shown how to convert the old dashboard example from from UiApp + Chart Service to HtmlService + Google Visualization API. This illustrates how to create a Google Apps Script web app that builds a chart from Google Spreadsheet data without "requiring authorization" as it's "implicitly" handled by Google Apps Script.
From the above link
Two functions cooperate to retrieve the dashboard’s data and display it. As soon as the visualization API is loaded, the sendQuery() function is invoked. Using the google.script.run facility, it sends its request to the server-side getSpreadsheetData() function. This is an asynchronous operation, so two callbacks are provided, a successHandler and a failureHandler. One or the other will receive the result of the server call, depending on the outcome.
H/T to jfllmartin, author of an answer to Converting my google dashboard app from the UI service to the HTML service where the above link was shared.
Related
How to create google dashboard including piechart and range select filter from a spreadsheet?
Display Spreadsheet data in Sites with Html Service
can I suggest you change from using Google sheets to using firebase with Google sheets or just Firebase, and then with Google appscript in the back end.
I often use a Google script to problematically update Firebase with data from the Google sheet. I then enjoy the speed and security of Firebase to deliver a super fast user experience.
There are two go to pages for using Firebase in appscript. The example page and the quick start.
Furthermore, I gave up using Google's own charting library and starting using high charts or chartJS, as they are more accessible.

How to enable Drive API using google Apps script in script editor

I am trying to enable Google Drive API using apps script so that we can upload any file to drive without manual intervention.
edit
function test()
{
var file = DriveApp.getFileById(newSpreadSheet.getId()); file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
}
This is how I'm setting permission for a spreadsheet using code. I need a similar thing to enable drive API as well.So that anybody can use the spreadsheet without enabling the drive API manually from google developer console.
Thanks in advance
Enabling an API is usually done through the Google Developer Console as given in enabling Google APIs Services wherein you enable them from the Google APIs Services dialog box from the Script Editor then through the Google APIs Console link.
However, if you don't want to go through the Developer Console, you may want to try Connecting to public APIs which is applicable for Google (or non-Google) API that isn't available as an Apps Script service. As mentioned,
If you want to use a Google (or non-Google) API that isn't available as an Apps Script service, you can connect to the API's public HTTP interface through the URL Fetch service.
Hope that helps!