Go quickstart

  • This quickstart guides you through creating a Go command-line application that interacts with the Google Apps Script API.

  • The guide covers setting up your development environment, configuring the OAuth consent screen and credentials, and preparing your workspace with necessary Go modules.

  • You will set up a sample Go file with provided code that handles authentication and interacts with the Apps Script API to create and update a script project.

  • Running the sample involves building and executing the Go file, which will guide you through the authorization process the first time it's run.

Create a Go command-line application that makes requests to theGoogle Apps Script API.

Quickstarts explain how to set up and run an app that calls aGoogle Workspace API. This quickstart uses asimplified authentication approach that is appropriate for a testingenvironment. For a production environment, we recommend learning aboutauthentication and authorizationbeforechoosing the access credentialsthat are appropriate for your app.

This quickstart uses Google Workspace's recommended API client librariesto handle some details of the authentication and authorization flow.

Objectives

  • Set up your environment.
  • Set up the sample.
  • Run the sample.

Prerequisites

  • A Google account with Google Drive enabled.

Set up your environment

To complete this quickstart, set up your environment.

Enable the API

Before using Google APIs, you need to turn them on in a Google Cloud project.You can turn on one or more APIs in a single Google Cloud project.
  • In the Google Cloud console, enable the Google Apps Script API.

    Enable the API

Configure the OAuth consent screen

If you're using a new Google Cloud project to complete this quickstart, configurethe OAuth consent screen. If you've alreadycompleted this step for your Cloud project, skip to the next section.

  1. In the Google Cloud console, go to Menu>Google Auth platform>Branding.

    Go to Branding

  2. If you have already configured the Google Auth platform, you can configure the following OAuth Consent Screen settings inBranding,Audience, andData Access. If you see a message that saysGoogle Auth platform not configured yet, clickGet Started:
    1. UnderApp Information, inApp name, enter a name for the app.
    2. InUser support email, choose a support email address where users can contact you if they have questions about their consent.
    3. ClickNext.
    4. UnderAudience, selectInternal.
    5. ClickNext.
    6. UnderContact Information, enter anEmail address where you can be notified about any changes to your project.
    7. ClickNext.
    8. UnderFinish, review theGoogle API Services User Data Policy and if you agree, selectI agree to the Google API Services: User Data Policy.
    9. ClickContinue.
    10. ClickCreate.
  3. For now, you can skip adding scopes. In the future, when you create an app for use outside of your Google Workspace organization, you must change theUser type toExternal. Then add the authorization scopes that your app requires. To learn more, see the fullConfigure OAuth consent guide.

Authorize credentials for a desktop application

To authenticate end users and access user data in your app, you need tocreate one or more OAuth 2.0 Client IDs. A client ID is used to identify asingle app to Google's OAuth servers. If your app runs on multiple platforms,you must create a separate client ID for each platform.
  1. In the Google Cloud console, go to Menu>Google Auth platform>Clients.

    Go to Clients

  2. ClickCreate Client.
  3. ClickApplication type>Desktop app.
  4. In theName field, type a name for the credential. This name is only shown in the Google Cloud console.
  5. ClickCreate.

    The newly created credential appears under "OAuth 2.0 Client IDs."

  6. Save the downloaded JSON file ascredentials.json, and move the file to your working directory.

Prepare the workspace

  1. Create a working directory:

    mkdir quickstart
  2. Change to the working directory:

    cd quickstart
  3. Initialize the new module:

    go mod init quickstart
  4. Get the Google Apps Script API Go client library and OAuth2.0 package:

    go get google.golang.org/api/script/v1go get golang.org/x/oauth2/google

Set up the sample

  1. In your working directory, create a file namedquickstart.go.

  2. In the file, paste the following code:

    apps_script/quickstart/quickstart.go
    packagemainimport("context""encoding/json""fmt""log""net/http""os""golang.org/x/oauth2""golang.org/x/oauth2/google""google.golang.org/api/option""google.golang.org/api/script/v1")// Retrieve a token, saves the token, then returns the generated client.funcgetClient(config*oauth2.Config)*http.Client{// The file token.json stores the user's access and refresh tokens, and is// created automatically when the authorization flow completes for the first// time.tokFile:="token.json"tok,err:=tokenFromFile(tokFile)iferr!=nil{tok=getTokenFromWeb(config)saveToken(tokFile,tok)}returnconfig.Client(context.Background(),tok)}// Request a token from the web, then returns the retrieved token.funcgetTokenFromWeb(config*oauth2.Config)*oauth2.Token{authURL:=config.AuthCodeURL("state-token",oauth2.AccessTypeOffline)fmt.Printf("Go to the following link in your browser then type the "+"authorization code: \n%v\n",authURL)varauthCodestringif_,err:=fmt.Scan(&authCode);err!=nil{log.Fatalf("Unable to read authorization code: %v",err)}tok,err:=config.Exchange(context.TODO(),authCode)iferr!=nil{log.Fatalf("Unable to retrieve token from web: %v",err)}returntok}// Retrieves a token from a local file.functokenFromFile(filestring)(*oauth2.Token,error){f,err:=os.Open(file)iferr!=nil{returnnil,err}deferf.Close()tok:=&oauth2.Token{}err=json.NewDecoder(f).Decode(tok)returntok,err}// Saves a token to a file path.funcsaveToken(pathstring,token*oauth2.Token){fmt.Printf("Saving credential file to: %s\n",path)f,err:=os.OpenFile(path,os.O_RDWR|os.O_CREATE|os.O_TRUNC,0600)iferr!=nil{log.Fatalf("Unable to cache oauth token: %v",err)}deferf.Close()json.NewEncoder(f).Encode(token)}funcmain(){ctx:=context.Background()b,err:=os.ReadFile("credentials.json")iferr!=nil{log.Fatalf("Unable to read client secret file: %v",err)}// If modifying these scopes, delete your previously saved token.json.config,err:=google.ConfigFromJSON(b,"https://www.googleapis.com/auth/script.projects")iferr!=nil{log.Fatalf("Unable to parse client secret file to config: %v",err)}client:=getClient(config)srv,err:=script.NewService(ctx,option.WithHTTPClient(client))iferr!=nil{log.Fatalf("Unable to retrieve Script client: %v",err)}req:=script.CreateProjectRequest{Title:"My Script"}createRes,err:=srv.Projects.Create(&req).Do()iferr!=nil{// The API encountered a problem.log.Fatalf("The API returned an error: %v",err)}content:=&script.Content{ScriptId:createRes.ScriptId,Files:[]*script.File{{Name:   "hello",Type:   "SERVER_JS",Source: "function helloWorld() {\n  console.log('Hello, world!');}",}, {Name: "appsscript",Type: "JSON",Source: "{\"timeZone\":\"America/New_York\",\"exceptionLogging\":" +"\"CLOUD\"}",}},}updateContentRes,err:=srv.Projects.UpdateContent(createRes.ScriptId,content).Do()iferr!=nil{// The API encountered a problem.log.Fatalf("The API returned an error: %v",err)}log.Printf("https://script.google.com/d/%v/edit",updateContentRes.ScriptId)}

Run the sample

  1. In your working directory, build and run the sample:

    go run quickstart.go
  1. The first time you run the sample, it prompts you to authorize access:
    1. If you're not already signed in to your Google Account, sign in when prompted. If you're signed in to multiple accounts, select one account to use for authorization.
    2. ClickAccept.

    Your Go application runs and calls the Google Apps Script API.

    Authorization information is stored in the file system, so the next time you run the sample code, you aren't prompted for authorization.

Next steps

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-11 UTC.