I'm trying to develop a plugin for IntelliJ to let me edit google apps scripts (GAS). The motivation is this. I want my projects to have better version control, refactoring methods, etc.
I think I can leverage the google drive API to do this. Specifically, I want IntelliJ to be able to import, edit, and export GAS to and from my google drive seamlessly.
Here are the steps I followed to try and accomplish this:
I used the developer console to enable the Drive API.
I followed this quickstart guide to create an example apps script project.
I then read this Java quickstart guide to use the GAS API.
I ran this method to read my example project:
public static void main(String[] args) throws IOException {
// ID of the script to call. Acquire this from the Apps Script editor,
// under Publish > Deploy as API executable.
String scriptId = <my script ID>;
Script service = getScriptService();
// Create an execution request object.
ExecutionRequest request = new ExecutionRequest()
.setFunction("getFoldersUnderRoot");
try {
// Make the API request.
Operation op =
service.scripts().run(scriptId, request).execute();
// Print results of request.
if (op.getError() != null) {
// The API executed, but the script returned an error.
System.out.println(getScriptError(op));
} else {
// The result provided by the API needs to be cast into
// the correct type, based upon what types the Apps
// Script function returns. Here, the function returns
// an Apps Script Object with String keys and values,
// so must be cast into a Java Map (folderSet).
Map<String, String> folderSet =
(Map<String, String>)(op.getResponse().get("result"));
if (folderSet.size() == 0) {
System.out.println("No folders returned!");
} else {
System.out.println("Folders under your root folder:");
for (String id: folderSet.keySet()) {
System.out.printf(
"\t%s (%s)\n", folderSet.get(id), id);
}
}
}
} catch (GoogleJsonResponseException e) {
// The API encountered a problem before the script was called.
e.printStackTrace(System.out);
}
}
It gave me the following error:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
Disconnected from the target VM, address: '127.0.0.1:49405', transport: 'socket'
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Project has not enabled the API. Please use Google Developers Console to activate the API for your project.",
"reason" : "forbidden"
} ],
"message" : "Project has not enabled the API. Please use Google Developers Console to activate the API for your project.",
"status" : "PERMISSION_DENIED"
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1056)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at com.michaelsnowden.gas_plugin.AppsScriptApiQuickstart.main(AppsScriptApiQuickstart.java:197)
The error appears to be this specifically:
Project has not enabled the API. Please use Google Developers Console to activate the API for your project.
I thought I did this earlier though. I'm sure I'm using the right client_secret
, the right scopes, and I've been able to use the OAuth playground with my app.
What am I missing to allow me to read scripts from the Google Drive GAS API?