Access Adwords through GAS - google-apps-script

I want to manage my Adwords campaigns through a script. I have a few Adwords scripts running and this is all fine, but now I need to access a library. Unfortunately this is not possible from the Adwords script interface so I need to access my Adwords MCC from the "normal" scripts interface.
Through resources - advanced google services , I don't come across a Google Adwords API. Any ideas on how to do this?
Thanks!

Turns out this is not (yet) supported.
Source: https://groups.google.com/forum/#!topic/adwords-scripts/G2GiGMp5Ols

Unfornately, AdWords is not available in Advanced Google Services. But you can try communicating using External APIs.
var url = 'https://gdata.youtube.com/feeds/api/videos?'
+ 'q=skateboarding+dog'
+ '&start-index=21'
+ '&max-results=10'
+ '&v=2';
var response = UrlFetchApp.fetch(url);
Logger.log(response);
Making requests to services with OAuth
APIs that act on behalf of a user usually require authorization, often using the OAuth protocol. Apps Script doesn't provide built-in support for the protocol, but there are open source libraries you can use to perform the OAuth flow and send the credentials with your requests:
OAuth1 for Apps Script: Compatible with OAuth 1.0 and 1.0a.
OAuth2 for Apps Script: Compatible with OAuth2.
Hope this helps.

Related

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.

JWT for Google Apps Script?

I'm using an API that requires a JSON Web Token generated with a couple of specific and unique parameters. In node, one would simply use var jwt = require(jsonwebtoken') to load the proper library, but that can't be done in Apps Script.
Is there a GAS library that could substitute node's jsonwebtoken module?
On Google Apps Script server side code can "require" code only from other Google Apps Script project. For details see Libraries.
Related Q&A
Is there a complete definition of the Google App Script Syntax somewhere?
Which Edition of ECMA-262 Does Google Apps Script Support?

Creating an oauth 1.0a signature for Google Script

I am trying to connect to an API using an oauth 1.0a signature for Google Script. I am looking for the functions and libraries to use in the Google Script.
Based from this documentation, APIs that act on behalf of a user usually require authorization, often using the OAuth protocol. Apps Script doesn't provide built-in support for the protocol, but there are open source libraries you can use to perform the OAuth flow and send the credentials with your requests.
Check this OAuth1 for Apps Script which is compatible with OAuth 1.0 and 1.0a.
OAuth1 for Apps Script is a library for Google Apps Script that provides the ability to create and authorize OAuth1 tokens. This library uses Apps Script's new StateTokenBuilder and /usercallback endpoint to handle the redirects.
Note: OAuth1 for Google APIs is deprecated and scheduled to be shut down on April 20, 2015. For accessing Google APIs, use the Apps Script OAuth2 library instead.

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!

Use Adwords API (MccApp) in 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.