gmailAPI

package module
v0.0.0-...-8eb8b47 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 30, 2023 License: MIT Imports: 12 Imported by: 0

README

gmailAPI

A go package for managing gmail Tokens/credentials.

All credit to the google gmail API devs who provided an example here: https://developers.google.com/gmail/api/quickstart/go

This package was adapted from that example and built in a way that allows the user to import it into any project that uses the gmail API. This package first checks for credentials in a users home directory, if they aren't found it attempts to create the credentials by prompting the use to visit a link and paste the authentication token found at that link into the program. This only needs to be done once. The next version of gmailAPI will include support for multiple users.

USE:

CREDENTIALS:

For gmailAPI to work you must have a gmail account and a file named "client_secret.json" containing your authorization info in the root directory of your project. To obtain credentials please see step one of this guide: https://developers.google.com/gmail/api/quickstart/go

Turning on the gmail API

  • On the Add credentials to your project page, click the Cancel button.
  • At the top of the page, select the OAuth consent screen tab. Select an Email address, enter a Product name if not already set, and click the Save button.
  • Select the Credentials tab, click the Create credentials button and select OAuth client ID.
  • Select the application type Other, enter the name "Gmail API Quickstart", and click the Create button.
  • Click OK to dismiss the resulting dialog.
  • Click the file_download (Download JSON) button to the right of the client ID.
  • Move this file to your working directory and rename it client_secret.json.
package main

import (
	"context"
	"fmt"

	"gitlab.com/hartsfield/gmailAPI"
	gmail "google.golang.org/api/gmail/v1"
)

func main() {
	// Connect to the Gmail API service. Here, we use a context and provide a
	// scope. The scope is used by the Gamil API to determine your privilege
	// level. gmailAPI.ConnectToService is a variadic function and thus can be
	// passed any number of scopes. For more information on scopes see:
	// https://developers.google.com/gmail/api/auth/scopes
	ctx := context.Background()
	srv := gmailAPI.ConnectToService(ctx, gmail.GmailComposeScope)

	// Get a list of your unread messages
	inbox, err := srv.Users.Messages.List("me").Q("in:UNREAD").Do()
	if err != nil {
		fmt.Println(err)
	}

	for _, message := range inbox.Messages {
		// To get the message content, you must make a second call
		// to the gmail API for each individual ID.
	  msg, _ := srv.Users.Messages.Get("me", message.Id).Do()
    fmt.Println(msg.Snippet)
	}
}

Also see my other package, inboxer, which makes performing basic actions on your inbox much more straight-forward.

Documentation

Overview

package gmailAPI is used in applications and libraries that access the gmail api. Most of this code was lightly modified from code found on: https://developers.google.com/gmail/api/quickstart/go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConnectToService

func ConnectToService(ctx context.Context, scope ...string) *gmail.Service

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL