Skip to content

Commit

Permalink
Updated to v2.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
tanaikech committed Jan 3, 2020
1 parent b8cc278 commit 70b0c82
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 88 deletions.
155 changes: 82 additions & 73 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
gogauth
====
# gogauth

[![Build Status](https://travis-ci.org/tanaikech/gogauth.svg?branch=master)](https://travis-ci.org/tanaikech/gogauth)
[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENCE)


This is a CLI tool to easily retrieve access token for using Google APIs.

# Demo
Expand All @@ -14,6 +12,7 @@ This is a CLI tool to easily retrieve access token for using Google APIs.
This is a demo for retrieving values from spreadsheet using Google Sheet API v4. The access token obtained by gogauth is used for this. This is a batch file for windows command prompt.

# Description

One day, I noticed users who feel difficulty for retrieving access token from Google. I thought that if the access token can easily retrieve, users who can use various convenience Google APIs will increase. So I created this. Also this can be used for testing sample script like the demo. If this will be helpful for you, I'm glad.

Features of this CLI tool is as follows.
Expand All @@ -25,109 +24,112 @@ Features of this CLI tool is as follows.
3. Confirms condition of access token. For the access token, you can see expiration time, owner, scopes and client id.

# How to get gogauth

Download an executable file of gogauth from [the release page](https://github.com/tanaikech/gogauth/releases) and import to a directory with path.

or

Use go get.

~~~bash
```bash
$ go get -u github.com/tanaikech/gogauth
~~~
```

# Requirement
gogauth requires following ``client_secret.json``. <u>Please put it to the current working directory.</u> This is because to use some accounts is supposed. Each account can be managed in each directory.

## <u>Download ``client_secret.json``</u>
gogauth requires following `client_secret.json`. <u>Please put it to the current working directory.</u> This is because to use some accounts is supposed. Each account can be managed in each directory.

## <u>Download `client_secret.json`</u>

1. On the Script Editor
- -> Resources
- -> Cloud Platform Project
- -> Click "Projects currently associated with this script"
- -> Resources
- -> Cloud Platform Project
- -> Click "Projects currently associated with this script"
2. On the Console Project
- Authentication information at left side
- -> Create a valid Client ID as OAyth client ID
- -> Choose **etc**
- -> Input Name (This is a name you want.)
- -> done
- -> Download a JSON file with Client ID and Client Secret as **``client_secret.json``** using download button. Please rename the file name to **``client_secret.json``**.
- Authentication information at left side
- -> Create a valid Client ID as OAyth client ID
- -> Choose **etc**
- -> Input Name (This is a name you want.)
- -> done
- -> Download a JSON file with Client ID and Client Secret as **`client_secret.json`** using download button. Please rename the file name to **`client_secret.json`**.

The detailed information is [here](https://developers.google.com/identity/protocols/OAuth2).

Downloaded "client_secret.json" is as follows.

~~~json
```json
{
"installed": {
"client_id": "#####",
"project_id": "#####",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_secret": "#####",
"redirect_uris": [
"#####"
]
}
"installed": {
"client_id": "#####",
"project_id": "#####",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_secret": "#####",
"redirect_uris": ["#####"]
}
}
~~~
```

# Usage

## Help

~~~bash
```bash
$ gogauth --help
~~~
```

## Retrieving Access Token

~~~bash
```bash
$ gogauth g
~~~
```

- Please run this under the directory with ``client_secret.json``.
- Please run this under the directory with `client_secret.json`.
- When above is run, your browser is launched and waits for login to Google.
- Please login to Google.
- [Authorization for Google Services](https://developers.google.com/apps-script/guides/services/authorization) is launched. Please authorize it.
- The authorization code can be retrieved automatically. And access token is displayed on your terminal.
- If your browser isn't launched or spends for 30 seconds from the wait of authorization, it becomes the input work queue. This is a manual mode. Please copy displayed URL and paste it to your browser, and login to Google. A **code** is displayed on the browser. Please copy it and paste it to your terminal during the input work queue. If you cannot find the code on your browser, please check the URL of your browser.
- When access token is displayed on your terminal, the authorization is completed and ``gogauth.cfg`` is created on a directory you currently stay.
- ``gogauth.cfg`` includes client id, client secret, access token, refresh token, scopes and so on.
- At the default, there are 1 scope (``https://www.googleapis.com/auth/drive.readonly``). If you want to change and add the scopes, please modify ``gogauth.cfg`` and run ``$ gogauth g -r``. Of course, you can write several scopes like as follows.
- If your browser isn't launched or spends for 30 seconds from the wait of authorization, it becomes the input work queue. This is a manual mode. Please copy displayed URL and paste it to your browser, and login to Google. A **code** is displayed on the browser. Please copy it and paste it to your terminal during the input work queue. If you cannot find the code on your browser, please check the URL of your browser.
- When access token is displayed on your terminal, the authorization is completed and `gogauth.cfg` is created on a directory you currently stay.
- `gogauth.cfg` includes client id, client secret, access token, refresh token, scopes and so on.
- At the default, there are 1 scope (`https://www.googleapis.com/auth/drive.readonly`). If you want to change and add the scopes, please modify `gogauth.cfg` and run `$ gogauth g -r`. Of course, you can write several scopes like as follows.

~~~json
```json
{
"client_id": "#####",
"client_secret": "#####",
"refresh_token": "#####",
"access_token": "#####",
"expires_in": 12345,
"scopes": [
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/drive"
]
"client_id": "#####",
"client_secret": "#####",
"refresh_token": "#####",
"access_token": "#####",
"expires_in": 12345,
"scopes": [
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/drive"
]
}
~~~
```

After modified it, please execute below.

~~~bash
```bash
$ gogauth g -r
~~~
```

By this, new access token and refresh token for the modified scopes are retrieved, and updated ``gogauth.cfg``. From this version, the code for retrieving refresh token is retrieved by web server on gogauth. The port for the server is 8080 as a default port number. If you want to change the port, please run below.
By this, new access token and refresh token for the modified scopes are retrieved, and updated `gogauth.cfg`. From this version, the code for retrieving refresh token is retrieved by web server on gogauth. The port for the server is 8080 as a default port number. If you want to change the port, please run below.

~~~bash
```bash
$ gogauth g -r -p [ port number ]
~~~
```

## Confirm Condition of Access Token

Also this tool can confirm the condition of access token as follows.

~~~bash
```bash
$ gogauth c -a [ access token ]
~~~
```

~~~json
```json
{
"aud": "#####",
"scope": "https://www.googleapis.com/auth/drive",
Expand All @@ -137,14 +139,15 @@ $ gogauth c -a [ access token ]
"expires_in": "1000",
"access_type": "offline"
}
~~~
```

``"exp_datetime"`` means the expiration time for the access token. ``"expires_in"`` means the remaining time for the access token.
`"exp_datetime"` means the expiration time for the access token. `"expires_in"` means the remaining time for the access token.

## Sample

This is a sample using gogauth. This sample retrieve values from spreadsheet. You can see this at above demo.

~~~batch
```batch
@echo off
setlocal
set range="a1:b5"
Expand All @@ -154,38 +157,44 @@ set sheetid="#####"
curl -X GET -fsSL ^
-H "Authorization: Bearer %accesstoken%" ^
"%url%%sheetid%/values/%range%?fields=values"
~~~
```

# Licence

[MIT](LICENCE)

# Author

[TANAIKE](https://github.com/tanaikech)

If you have any questions, feel free to tell me.

# Update History

<a name="v202"></a>

- v2.0.2 (January 3, 2020)

1. It seems that the specification of `github.com/urfave/cli` was changed by the update of [https://github.com/urfave/cli](https://github.com/urfave/cli). By this, when `go get -u github.com/tanaikech/gogauth` is run, an error occurred. So this error was removed.

* v2.0.1 (May 8, 2017)

- Remove bugs.
- Remove bugs.

* v2.0.0 (April 19, 2017)

- There are big changes at this version.
- For retrieving code from browser, it doesn't use PhantomJS, it adopted the use of web server on gogauth.
Reference sites are as follows. Thank you so much.
- [http://d.hatena.ne.jp/taknb2nch/20140226/1393394578](http://d.hatena.ne.jp/taknb2nch/20140226/1393394578)
- [http://qiita.com/yamasaki-masahide/items/f4eb7cd17a9ea1fe5467](http://qiita.com/yamasaki-masahide/items/f4eb7cd17a9ea1fe5467)
- There are big changes at this version.
- For retrieving code from browser, it doesn't use PhantomJS, it adopted the use of web server on gogauth.
Reference sites are as follows. Thank you so much. - [http://d.hatena.ne.jp/taknb2nch/20140226/1393394578](http://d.hatena.ne.jp/taknb2nch/20140226/1393394578) - [http://qiita.com/yamasaki-masahide/items/f4eb7cd17a9ea1fe5467](http://qiita.com/yamasaki-masahide/items/f4eb7cd17a9ea1fe5467)

* v1.1.0 (March 4, 2017)

- Added 2 commands.
- Added option ``--nophantomjs`` for command of ``getaccesstoken``.
- Added option ``--accesstoken`` for command of ``checkaccesstoken``.
- Added 2 modes for retrieving scopes.
- Changed cfg file of ``gogauthcfg.json`` for 2 modes.
- Added 2 commands.
- Added option `--nophantomjs` for command of `getaccesstoken`.
- Added option `--accesstoken` for command of `checkaccesstoken`.
- Added 2 modes for retrieving scopes.
- Changed cfg file of `gogauthcfg.json` for 2 modes.

* v1.0.0 (February 24, 2017)

Initial release.
Initial release.
34 changes: 19 additions & 15 deletions gogauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,28 @@ import (
func main() {
app := cli.NewApp()
app.Name = "gogauth"
app.Author = "tanaike [ https://github.com/tanaikech/gogauth ] "
app.Email = "tanaike@hotmail.com"
app.Usage = "Retrieves accesstoken for using Drive API from Google."
app.Version = "2.0.1"
app.Commands = []cli.Command{
app.Authors = []*cli.Author{
{Name: "tanaike [ https://github.com/tanaikech/gogauth ] ", Email: "tanaike@hotmail.com"},
}
app.UsageText = "Retrieves accesstoken for using Drive API from Google."
app.Version = "2.0.2"
app.Commands = []*cli.Command{
{
Name: "getaccesstoken",
Aliases: []string{"g"},
Usage: "Get accesstoken",
Action: getTokens,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "reauth, r",
Usage: "Retrieve access and refresh tokens. If you changed scopes, please use this.",
&cli.BoolFlag{
Name: "reauth, r",
Aliases: []string{"r"},
Usage: "Retrieve access and refresh tokens. If you changed scopes, please use this.",
},
cli.IntFlag{
Name: "port, p",
Usage: "Port number of temporal web server for retrieving authorization code.",
Value: 8080,
&cli.IntFlag{
Name: "port, p",
Aliases: []string{"p"},
Usage: "Port number of temporal web server for retrieving authorization code.",
Value: 8080,
},
},
},
Expand All @@ -40,9 +43,10 @@ func main() {
Usage: "Check accesstoken",
Action: checkAccesstoken,
Flags: []cli.Flag{
cli.StringFlag{
Name: "accesstoken, a",
Usage: "Confirm condition of current accesstoken",
&cli.StringFlag{
Name: "accesstoken, a",
Aliases: []string{"a"},
Usage: "Confirm condition of current accesstoken",
},
},
},
Expand Down

0 comments on commit 70b0c82

Please sign in to comment.