@@ -3,6 +3,7 @@ package main
33import (
44 "encoding/json"
55 "fmt"
6+ "io/ioutil"
67 "log"
78 "net"
89 "net/http"
@@ -15,6 +16,7 @@ import (
1516
1617 "golang.org/x/net/context"
1718 "golang.org/x/oauth2"
19+ "golang.org/x/oauth2/google"
1820)
1921
2022// This variable indicates whether the script should launch a web server to
@@ -54,8 +56,28 @@ https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
5456
5557// getClient uses a Context and Config to retrieve a Token
5658// then generate a Client. It returns the generated Client.
57- func getClient (ctx context.Context , config * oauth2.Config ) * http.Client {
58- cacheFile , err := tokenCacheFile ()
59+ func getClient (scope string ) * http.Client {
60+ ctx := context .Background ()
61+
62+ b , err := ioutil .ReadFile ("client_secret.json" )
63+ if err != nil {
64+ log .Fatalf ("Unable to read client secret file: %v" , err )
65+ }
66+
67+ // If modifying the scope, delete your previously saved credentials
68+ // at ~/.credentials/youtube-go.json
69+ config , err := google .ConfigFromJSON (b , scope )
70+ if err != nil {
71+ log .Fatalf ("Unable to parse client secret file to config: %v" , err )
72+ }
73+
74+ // Use a redirect URI like this for a web app. The redirect URI must be a
75+ // valid one for your OAuth2 credentials.
76+ config .RedirectURL = "http://localhost:8090"
77+ // Use the following redirect URI if launchWebServer=false in oauth2.go
78+ // config.RedirectURL = "urn:ietf:wg:oauth:2.0:oob"
79+
80+ cacheFile , err := tokenCacheFile ()
5981 if err != nil {
6082 log .Fatalf ("Unable to get path to cached credential file. %v" , err )
6183 }
@@ -114,6 +136,7 @@ func openURL(url string) error {
114136 return err
115137}
116138
139+ // Exchange the authorization code for an access token
117140func exchangeToken (config * oauth2.Config , code string ) (* oauth2.Token , error ) {
118141 tok , err := config .Exchange (oauth2 .NoContext , code )
119142 if err != nil {
0 commit comments