Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Commit 1c6f104

Browse files
"Updating samples to reflect recent changes."
1 parent 975f184 commit 1c6f104

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

go/oauth.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ import (
1919
const missingClientSecretsMessage = `
2020
Please configure OAuth 2.0
2121
22-
To make this sample run you will need to populate the client_secrets.json file
22+
To make this sample run, you need to populate the client_secrets.json file
2323
found at:
2424
2525
%v
2626
27-
with information from the APIs Console
28-
https://code.google.com/apis/console#access
27+
with information from the {{ Google Cloud Console }}
28+
{{ https://cloud.google.com/console }}
2929
3030
For more information about the client_secrets.json file format, please visit:
3131
https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
@@ -36,8 +36,8 @@ var (
3636
cacheFile = flag.String("cache", "request.token", "Token cache file")
3737
)
3838

39-
// ClientConfig is data structure definition for client_secrets.json.
40-
// This is what we'll unmarshal the JSON configuration file into.
39+
// ClientConfig is a data structure definition for the client_secrets.json file.
40+
// The code unmarshals the JSON configuration file into this structure.
4141
type ClientConfig struct {
4242
ClientID string `json:"client_id"`
4343
ClientSecret string `json:"client_secret"`
@@ -46,14 +46,14 @@ type ClientConfig struct {
4646
TokenURI string `json:"token_uri"`
4747
}
4848

49-
// Config is root level configuration object.
49+
// Config is a root-level configuration object.
5050
type Config struct {
5151
Installed ClientConfig `json:"installed"`
5252
Web ClientConfig `json:"web"`
5353
}
5454

55-
// openURL opens a browser window to that location.
56-
// This code taken from:
55+
// openURL opens a browser window to the specified location.
56+
// This code originally appeared at:
5757
// http://stackoverflow.com/questions/10377243/how-can-i-launch-a-process-that-is-not-a-file-in-go
5858
func openURL(url string) error {
5959
var err error
@@ -71,7 +71,7 @@ func openURL(url string) error {
7171
}
7272

7373
// readConfig reads the configuration from clientSecretsFile.
74-
// Returns an oauth configuration object to be used with the Google API client
74+
// It returns an oauth configuration object for use with the Google API client.
7575
func readConfig(scope string) (*oauth.Config, error) {
7676
// Read the secrets file
7777
data, err := ioutil.ReadFile(*clientSecretsFile)
@@ -104,16 +104,16 @@ func readConfig(scope string) (*oauth.Config, error) {
104104
TokenURL: cfg.Installed.TokenURI,
105105
RedirectURL: redirectUri,
106106
TokenCache: oauth.CacheFile(*cacheFile),
107-
// This gives us a refresh token so we can use this access token indefinitely
107+
// Get a refresh token so we can use the access token indefinitely
108108
AccessType: "offline",
109-
// If we want a refresh token, we must set this to force an approval prompt or
110-
// this won't work
109+
// If we want a refresh token, we must set this attribute
110+
// to force an approval prompt or the code won't work.
111111
ApprovalPrompt: "force",
112112
}, nil
113113
}
114114

115115
// startWebServer starts a web server that listens on http://localhost:8080.
116-
// The purpose of this webserver is to wait for a oauth code in the three-legged auth flow.
116+
// The webserver waits for an oauth code in the three-legged auth flow.
117117
func startWebServer() (codeCh chan string, err error) {
118118
listener, err := net.Listen("tcp", "localhost:8080")
119119
if err != nil {
@@ -132,9 +132,10 @@ func startWebServer() (codeCh chan string, err error) {
132132
}
133133

134134
// buildOAuthHTTPClient takes the user through the three-legged OAuth flow.
135-
// Opens a browser in the native OS or outputs a URL, blocking until the redirect
136-
// completes to the /oauth2callback URI. Returns an instance of an HTTP client
137-
// that can be passed to the constructor of the YouTube client.
135+
// It opens a browser in the native OS or outputs a URL, then blocks until
136+
// the redirect completes to the /oauth2callback URI.
137+
// It returns an instance of an HTTP client that can be passed to the
138+
// constructor of the YouTube client.
138139
func buildOAuthHTTPClient(scope string) (*http.Client, error) {
139140
config, err := readConfig(scope)
140141
if err != nil {
@@ -145,11 +146,13 @@ func buildOAuthHTTPClient(scope string) (*http.Client, error) {
145146
transport := &oauth.Transport{Config: config}
146147

147148
// Try to read the token from the cache file.
148-
// If there's an error, the token is invalid or doesn't exist, do the 3-legged OAuth flow.
149+
// If an error occurs, do the three-legged OAuth flow because
150+
// the token is invalid or doesn't exist.
149151
token, err := config.TokenCache.Token()
150152
if err != nil {
151153
// Start web server.
152-
// This is how this program receives the authorization code when the browser redirects.
154+
// This is how this program receives the authorization code
155+
// when the browser redirects.
153156
codeCh, err := startWebServer()
154157
if err != nil {
155158
return nil, err
@@ -170,8 +173,9 @@ func buildOAuthHTTPClient(scope string) (*http.Client, error) {
170173
// Wait for the web server to get the code.
171174
code := <-codeCh
172175

173-
// This will take care of caching the code on the local filesystem, if necessary,
174-
// as long as the TokenCache attribute in the config is set
176+
// This code caches the authorization code on the local
177+
// filesystem, if necessary, as long as the TokenCache
178+
// attribute in the config is set.
175179
token, err = transport.Exchange(code)
176180
if err != nil {
177181
return nil, err

0 commit comments

Comments
 (0)