Sample code for 30+ languages & platforms
Go Requires Chilkat v11.0.0+

Authorize a desktop application to use Gmail

See more GMail REST API Examples

Enable the Gmail API, configure the OAuth consent screen, and create a Desktop app OAuth client. Launch the system browser and receive the authorization code on a loopback listener. The sample requests read-only access with PKCE and offline access. For another operation, select the scope in that example before authorizing. Gmail scopes.

Run in a private, user-only directory. The file contains bearer credentials. In a deployed application use protected credential storage. To reuse an existing grant, copy the older examples’ qa_data/tokens/gmail.json to gmail-token.json in this private directory. The token retains its original scopes. Gmail clients using IMAP, POP, or SMTP with OAuth need the https://mail.google.com/ scope. Use a separate cache for each user and client.

Google documentation

Chilkat Go Downloads

Go
    success := false

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    oauth := chilkat.NewOAuth2()
    oauth.SetTokenEndpoint("https://oauth2.googleapis.com/token")
    oauth.SetClientId("YOUR_DESKTOP_CLIENT_ID")
    oauth.SetClientSecret("YOUR_DESKTOP_CLIENT_SECRET")

    oauth.SetAuthorizationEndpoint("https://accounts.google.com/o/oauth2/v2/auth")
    oauth.SetLocalHost("127.0.0.1")
    oauth.SetListenPort(55568)
    oauth.SetListenPortRangeEnd(55578)
    oauth.SetCodeChallenge(true)
    oauth.SetCodeChallengeMethod("S256")
    oauth.SetScope("https://www.googleapis.com/auth/gmail.readonly")
    success = oauth.AddAuthQueryParam("access_type","offline")
    if success != true {
        fmt.Println("Could not configure offline access.")
        oauth.DisposeOAuth2()
        return
    }

    //  This is the interactive setup step. Subsequent refreshes use the saved refresh token.
    success = oauth.AddAuthQueryParam("prompt","consent")
    if success != true {
        fmt.Println("Could not configure consent.")
        oauth.DisposeOAuth2()
        return
    }

    authUrl := oauth.StartAuth()
    if oauth.LastMethodSuccess() != true {
        fmt.Println("Could not start the loopback listener.")
        oauth.DisposeOAuth2()
        return
    }

    success = oauth.LaunchBrowser(*authUrl)
    if success != true {
        oauth.Cancel()
        fmt.Println("Could not open the system browser.")
        oauth.DisposeOAuth2()
        return
    }

    waited := 0
    for ((oauth.AuthFlowState() < 3) && (waited < 180000)) {
        oauth.SleepMs(200)
        waited = waited + 200
    }

    if oauth.AuthFlowState() < 3 {
        oauth.Cancel()
        fmt.Println("Authorization timed out.")
        oauth.DisposeOAuth2()
        return
    }

    if oauth.AuthFlowState() != 3 {
        fmt.Println("Authorization did not complete. Flow state: ", oauth.AuthFlowState())
        oauth.DisposeOAuth2()
        return
    }

    token := chilkat.NewJsonObject()
    success = token.Load(oauth.AccessTokenResponse())
    if success != true {
        fmt.Println("Could not parse the token response.")
        oauth.DisposeOAuth2()
        token.DisposeJsonObject()
        return
    }

    if (token.JsonTypeOf("refresh_token") != 1) || (token.StringOfEquals("refresh_token","",true) == true) {
        fmt.Println("No offline refresh token was issued. The existing cache was preserved.")
        oauth.DisposeOAuth2()
        token.DisposeJsonObject()
        return
    }

    success = token.WriteFile("gmail-token.json")
    if success != true {
        fmt.Println("Could not save the token file.")
        oauth.DisposeOAuth2()
        token.DisposeJsonObject()
        return
    }

    fmt.Println("Credentials saved in the private working directory.")

    oauth.DisposeOAuth2()
    token.DisposeJsonObject()