-1

How do I connect a script application to the Google Cloud Platform? I found how to do it manually. enter image description here But how to do it programmatically?

  File file3 = new File()
          .setName("appsscript")
          .setType("JSON")
          .setSource("{\n"
              + "  \"timeZone\": \""+TimeZone.getDefault().toZoneId().toString()+"\",\n"
              + "  \"dependencies\": {},\n"
              + " \"exceptionLogging\": \"STACKDRIVER\",\n"
              + "  \"executionApi\": {\n"
              + "    \"access\": \"ANYONE\"\n"
              + "  } }");

      Content content = new Content().setFiles(Arrays.asList(file1, file2, file3));
      script.projects().updateContent(project.getScriptId(), content).execute();
      Version version = new Version();
      version.setVersionNumber(1);
      Version versions = script.projects().versions().create(project.getScriptId(), version).execute();
      DeploymentConfig deploymentConfig = new DeploymentConfig();
      deploymentConfig.setManifestFileName("appsscript");
      deploymentConfig.setVersionNumber(versions.getVersionNumber());
      Deployment deployment = script.projects().deployments().create(project.getScriptId(), deploymentConfig).execute();
      Operation op = script.scripts().run(deployment.getDeploymentConfig().getScriptId(), request).execute();
   

when executing this code, I get:

POST https://script.googleapis.com/v1/scripts/xxxx:run
    {
      "code": 403,
      "errors": [
        {
          "domain": "global",
          "message": "The caller does not have permission",
          "reason": "forbidden"
        }
      ],
      "message": "The caller does not have permission",
      "status": "PERMISSION_DENIED"
    }

1 Answers1

0

I don't understand what you're trying to do.

Corollary: the simplest way to run Apps Scripts on Google Cloud Platform is to use Apps Script Console. Conventionally (!), developers use the Apps Script Console to create|edit, deploy, test Apps Scripts. Without knowing more, I think you'd want to go this route, unless you can't.

However (!) as with all Google's services (APIs), it's entirely reasonable to use a(n API) client library such as Apps Script to interact with the service directly. This is what you're doing and I think (!?), you're using Google API Client Library for C#|.NET for Apps Script to do it. That's all cool.

When you use a client library, you must authenticate its requests. See the .NET Getting Started guide here to learn more about creating a Google project, enabling services (e.g. Apps Script) in the project, and create credentials that you can use to authenticate the service.

If your Apps Script will access user data, then you will need to use a Web|Installed application flow to provide user(s) with a (Google) login prompt, scopes etc. and have them approve your script accessing their data.

If your Apps Script will not access user data, then you will probably use a Service Account instead. There's a very simple mechanism to authenticate using Service Account credentials called Application Default Credentials (ADC). If you use a Serivce Account, you should use ADCs.

DazWilkin
  • 32,823
  • 5
  • 47
  • 88