gclientauth

package module
v0.0.0-...-b7609cc Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2020 License: MIT Imports: 14 Imported by: 4

README

Package gclientauth handles settings up a oAuth2 client and access token to talk to Google APIs from cli applications. The client libraries/APIs themselves don't handle authentication so it is necessary to first get the access token that is used to setup the API client to the APIs which this package handles.

It wraps around golang.org/x/oauth2/google to provide some user friendly behavior and removes some boiler plate code for developers.

The code is a mixture of various public Google tutorials and examples such as http://developers.google.com/youtube/v3/quickstart/go.

In order to use this package:

  1. Create a new project on the Google API Console (https://console.developers.google.com/).
  2. In the new project, enable the Google APIs to access.
  3. Setup up the credentials and download the client secret JSON configuration from https://console.developers.google.com/apis/credentials

TIP:

If it is desktop/other credential is chosen then gclientauth will show the user an URL to visit in order toget a code that can be used to get an access token..

If it is a web application then gclientauth will attempt to run a local webserver to get the code itself and create a token so the user don't have to do anything themselves. Make sure you set the redirect url to match what you pass to the library (e.g. localhost:8080).

Example Usage:

package main

func main() {
    scopes := []string{youtube.YoutubeReadonlyScope}
    ctx := oauth2.NoContext
    token, config, err := gclientauth.GetGoogleOauth2Token(ctx, "client_secret.json", "accesstoken.json", scopes, false, "8080")
    ...
    cfg := config.Client(ctx, token)
    ...
    gp, err := googlephotos.New(cfg)
    ...
    res, err := gp.Albums.List().Do()
    for _, a := range res.Albums {
        fmt.Printf("%v\n", a.Title)
    }
}

Documentation

Overview

Package gclientauth handles settings up a oAuth2 client and access token to talk to Google APIs from cli applications. The client libraries/APIs themselves don't handle authentication so it is necessary to first get the access token that is used to setup the API client to the APIs which this package handles.

It wraps around golang.org/x/oauth2/google to provide some user friendly behavior and removes some boiler plate code for developers.

The code is a mixture of various public Google tutorials and examples such as http://developers.google.com/youtube/v3/quickstart/go.

In order to use this package:

  1. Create a new project on the Google API Console (https://console.developers.google.com/).

  2. In the new project, enable the Google APIs to access.

  3. Setup up the credentials and download the client secret JSON configuration from https://console.developers.google.com/apis/credentials

TIP:

If it is **desktop/other** credential is chosen then gclientauth will show the user an URL to visit in order toget a code that can be used to get an access token..

If it is a **web application** then gclientauth will attempt to run a local webserver to get the code itself and create a token so the user don't have to do anything themselves. Make sure that the credential's redirect url port matches what is passed to the package (e.g. localhost:8080).

Example Usage:

package main

   func main() {
	  scopes := []string{youtube.YoutubeReadonlyScope}
	  ctx := oauth2.NoContext
	  token, config, err := gclientauth.GetGoogleOauth2Token(ctx, "client_secret.json", "accesstoken.json", scopes, false, "8080")
	  ...
	  cfg := config.Client(ctx, token)
	  ...
	  gp, err := googlephotos.New(cfg)
	  ...
	  res, err := gp.Albums.List().Do()
	  for _, a := range res.Albums {
		fmt.Printf("%v\n", a.Title)
	  }
   }

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetGoogleOauth2Token

func GetGoogleOauth2Token(ctx context.Context, credential, cachedtoken string, scopes []string, browser bool, port string) (*oauth2.Token, *oauth2.Config, error)

Types

This section is empty.

Jump to

Keyboard shortcuts

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