func (d *OauthData) checkTokens() (err error) {
	data, err := ioutil.ReadFile(LocalStoreFile)
	if data == nil {
		return errors.New("Local store file is empty.")
	}
	err = json.Unmarshal(data, &d.Tokens)
	if d.Tokens.TokenType == "test" {
		return errors.New("Invalid localstore file data.")
	}
	return
}
checkTokens attempts to retrieve token data from the local flat file
package utils
import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"os"
	"regexp"
	"strings"
	"time"
)
const (
	Scope          string = "https://www.googleapis.com/auth/analytics.readonly"
	ReturnURI      string = "localhost:9000"
	LocalStoreFile string = "localStore.dat"
)
type AuthInfo struct {
	Data AuthData `json:"installed"`
}
type AuthData struct {
	ClientID     string   `json:"client_id"`
	ClientSecret string   `json:"client_secret"`
	AuthURI      string   `json:"auth_uri"`
	TokenURI     string   `json:"token_uri"`
	RedirectURIs []string `json:"redirect_uris"`
}
type AuthCode struct {
	Code         string `json:"code"`
	ClientID     string `json:"client_id"`
	ClientSecret string `json:"client_secret"`
	RedirectURI  string `json:"redirect_uri"`
	GrantType    string `json:"grant_type"`
}
type RefreshData struct {
	AccessToken string `json:"access_token"`
	Expires     int    `json:"expires_in"`
	TokenType   string `json:"token_type"`
}
func (a *AuthCode) ToURLValues() (out url.Values) {
	out = url.Values{"code": {a.Code},
		"client_id":     {a.ClientID},
		"client_secret": {a.ClientSecret},
		"redirect_uri":  {a.RedirectURI},
		"grant_type":    {a.GrantType},
	}
	return
}
type AccessData struct {
	AccessToken  string `json:"access_token"`
	ExpiresIn    int    `json:"expires_in"`
	TokenType    string `json:"token_type"`
	RefreshToken string `json:"refresh_token"`
}
type GError struct {
	Error            string `json:"error"`
	ErrorDescription string `json:"error_description"`
}
type OauthData struct {
	config   *AuthInfo
	Tokens   *AccessData
	JSONfile string
}
func (d *OauthData) ImportConfig(filename string) (err error) {
	configFile, err := ioutil.ReadFile(filename)
	if err != nil {
		log.Fatalf("Error opening config file: %s\n", filename)
	}
	err = json.Unmarshal(configFile, &d.config)
	if err != nil {
		log.Fatalf("Error unmarshalling config file: %s\n", filename)
	}
	log.Println("Imported config file.")
	d.JSONfile = filename
	return
}
func (d *OauthData) RegisterClient() (err error) {
	authUrl := fmt.Sprintf("%s?scope=%s&redirect_uri=http://%s&response_type=code&client_id=%s", d.config.Data.AuthURI, Scope, ReturnURI, d.config.Data.ClientID)
	ch := make(chan string)
	go WebCallback(ch)
	err = BrowserOpen(authUrl)
	CheckError(err)
	var newAccessCode string
	select {
	case newAccessCode = <-ch:
		log.Println("Received new access code")
	case <-time.After(60 * 1e9):
		log.Fatalln("Didn't receive auth token response... giving up.")
		return
	}
	PostData := AuthCode{newAccessCode, d.config.Data.ClientID, d.config.Data.ClientSecret, "http://" + ReturnURI, "authorization_code"}
	req, err := http.PostForm(d.config.Data.TokenURI, PostData.ToURLValues())
	CheckError(err)
	defer req.Body.Close()
	contents, err := ioutil.ReadAll(req.Body)
	CheckError(err)
	data, err := d.ProcessTokenResponse(contents)
	CheckError(err)
	d.Tokens = data
	return
}
func (d *OauthData) InitConnection() (err error) {
	err = d.ImportConfig("client_secret.json")
	CheckError(err)
	err = d.checkTokens()
	if err != nil {
		d.RegisterClient()
	}
	return
}
func (d *OauthData) ProcessTokenResponse(contents []byte) (data *AccessData, err error) {
	reg, err := regexp.Compile("\n")
	CheckError(err)
	out := reg.ReplaceAllString(string(contents), "")
	if strings.Contains(out, "error_description") {
		var tempError GError
		json.Unmarshal([]byte(out), &tempError)
		err = errors.New(fmt.Sprintf("Encountered error %s : %s", tempError.Error, tempError.ErrorDescription))
	} else {
		err := json.Unmarshal([]byte(out), &data)
		CheckError(err)
		d.tokenStore(data)
	}
	return
}
func (d *OauthData) ProcessRefreshResponse(contents []byte) (data *RefreshData, err error) {
	reg, err := regexp.Compile("\n")
	CheckError(err)
	out := reg.ReplaceAllString(string(contents), "")
	if strings.Contains(out, "error_description") {
		var tempError GError
		json.Unmarshal([]byte(out), &tempError)
		err = errors.New(fmt.Sprintf("Encountered error %s : %s", tempError.Error, tempError.ErrorDescription))
	} else {
		err := json.Unmarshal([]byte(out), &data)
		CheckError(err)
	}
	return
}
func (d *OauthData) tokenStore(data *AccessData) (err error) {
	out, err := json.Marshal(data)
	CheckError(err)
	err = ioutil.WriteFile(LocalStoreFile, out, 0644)
	CheckError(err)
	return
}