Giter Club home page Giter Club logo

goauth2's Introduction

GOAuth2

License: GPL v3 Go Reference Coverage Status Travis (.com)

A simple go library for getting Google OAuth2 token

Examples


Using :

package main

import (
	"fmt"
	"io/ioutil"
	"os"

	oauth "github.com/anthonyme00/GOAuth2"
)

func main() {
	connectionData := oauth.OAuthAPIConnectionData{
		ClientId:     "YOUR_CLIENT_ID",
		ClientSecret: "YOUR_CLIENT_SECRET",
		Scopes:       []string{"https://www.googleapis.com/auth/drive.file"},
	}

	token, err := oauth.GetOAuth2Token(connectionData)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%+v\n", token)

	//Using token in a query
	query := UrlQuery { 
		"access_token":	token.GetAccessToken(),
		"other_query":	"aquery",
	}
	DummyGoogleAPI.Call(query)
}

Refreshing tokens :

token, err := oauth.GetOAuth2Token(connectionData)

//Automatic token refreshing when getting access token
//3 seconds threshold before actual expiration of token
current_token := token.GetAccessToken(3))

//Manual token refreshing
token.Refresh()

//Checking if token is expired
if token.IsExpired(0) {
	fmt.Println("My token is expired...")
}

Serializing and saving to a file :

key := []byte("SUPER_SECRET_KEY")

//Save Token
//You can use the non encrypted version with token.Serialize()
encryptedToken, _ := token.SerializeEncrypted(key)
ioutil.WriteFile("mytoken.dat", encryptedToken, os.FileMode(os.O_CREATE|os.O_TRUNC))

//Load Token
//To load non encrypted token, use token.Deserialize()
tokenToDecrypt, _ := ioutil.ReadFile("mytoken.dat")
decryptedToken := oauth.OAuth2Token{}
decryptedToken.DeserializeEncrypted(tokenToDecrypt, key)
fmt.Printf("%+v\n", token)

Misc


Google API Scopes : https://developers.google.com/identity/protocols/oauth2/scopes

Google OAuth2 Docs : https://developers.google.com/identity/protocols/oauth2

goauth2's People

Contributors

anthonyme00 avatar

Stargazers

Giovanni Allegri avatar  avatar Henri avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.