This repository has been archived by the owner on Oct 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added authentication keys to start retrieving data from the youtube API
- Loading branch information
Nyah Check
committed
Jul 31, 2017
1 parent
63d58e0
commit 56b4fe4
Showing
3 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
// Copyright 2017 YTD Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
// OAuth to Youtube Data API | ||
|
||
package auth | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"io/ioutil" | ||
"net" | ||
"net/http" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"runtime" | ||
|
||
"code.google.com/p/goauth2/oauth" | ||
) | ||
|
||
const missingClientSecretsMessage = ` OAuth 2.0 not configured ` | ||
|
||
var ( | ||
clientSecretsFile = flag.String("secrets", "client_secrets.json", "Client Secrets configuration") | ||
cacheFile = flag.String("cache", "request.token", "Token cache file") | ||
) | ||
|
||
// ClientConfig is a data structure definition for the client_secrets.json file. | ||
// The code unmarshals the JSON configuration file into this structure. | ||
type ClientConfig struct { | ||
ClientID string `json:"client_id"` | ||
ClientSecret string `json:"client_secret"` | ||
RedirectURIs []string `json:"redirect_uris"` | ||
AuthURI string `json:"auth_uri"` | ||
TokenURI string `json:"token_uri"` | ||
} | ||
|
||
// Config is a root-level configuration object. | ||
type Config struct { | ||
Installed ClientConfig `json:"installed"` | ||
Web ClientConfig `json:"web"` | ||
} | ||
|
||
// openURL opens a browser window to the specified location. | ||
// This code originally appeared at: | ||
// http://stackoverflow.com/questions/10377243/how-can-i-launch-a-process-that-is-not-a-file-in-go | ||
func openURL(url string) error { | ||
var err error | ||
switch runtime.GOOS { | ||
case "linux": | ||
err = exec.Command("xdg-open", url).Start() | ||
case "windows": | ||
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", "http://localhost:4001/").Start() | ||
case "darwin": | ||
err = exec.Command("open", url).Start() | ||
default: | ||
err = fmt.Errorf("Cannot open URL %s on this platform", url) | ||
} | ||
return err | ||
} | ||
|
||
// readConfig reads the configuration from clientSecretsFile. | ||
// It returns an oauth configuration object for use with the Google API client. | ||
func readConfig(scope string) (*oauth.Config, error) { | ||
// Read the secrets file | ||
data, err := ioutil.ReadFile(*clientSecretsFile) | ||
if err != nil { | ||
pwd, _ := os.Getwd() | ||
fullPath := filepath.Join(pwd, *clientSecretsFile) | ||
return nil, fmt.Errorf(missingClientSecretsMessage, fullPath) | ||
} | ||
|
||
cfg := new(Config) | ||
err = json.Unmarshal(data, &cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var redirectUri string | ||
if len(cfg.Web.RedirectURIs) > 0 { | ||
redirectUri = cfg.Web.RedirectURIs[0] | ||
} else if len(cfg.Installed.RedirectURIs) > 0 { | ||
redirectUri = cfg.Installed.RedirectURIs[0] | ||
} else { | ||
return nil, errors.New("Must specify a redirect URI in config file or when creating OAuth client") | ||
} | ||
|
||
return &oauth.Config{ | ||
ClientId: cfg.Installed.ClientID, | ||
ClientSecret: cfg.Installed.ClientSecret, | ||
Scope: scope, | ||
AuthURL: cfg.Installed.AuthURI, | ||
TokenURL: cfg.Installed.TokenURI, | ||
RedirectURL: redirectUri, | ||
TokenCache: oauth.CacheFile(*cacheFile), | ||
// Get a refresh token so we can use the access token indefinitely | ||
AccessType: "offline", | ||
// If we want a refresh token, we must set this attribute | ||
// to force an approval prompt or the code won't work. | ||
ApprovalPrompt: "force", | ||
}, nil | ||
} | ||
|
||
// startWebServer starts a web server that listens on http://localhost:8080. | ||
// The webserver waits for an oauth code in the three-legged auth flow. | ||
func startWebServer() (codeCh chan string, err error) { | ||
listener, err := net.Listen("tcp", "localhost:8080") | ||
if err != nil { | ||
return nil, err | ||
} | ||
codeCh = make(chan string) | ||
go http.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
code := r.FormValue("code") | ||
codeCh <- code // send code to OAuth flow | ||
listener.Close() | ||
w.Header().Set("Content-Type", "text/plain") | ||
fmt.Fprintf(w, "Received code: %v\r\nYou can now safely close this browser window.", code) | ||
})) | ||
|
||
return codeCh, nil | ||
} | ||
|
||
// buildOAuthHTTPClient takes the user through the three-legged OAuth flow. | ||
// It opens a browser in the native OS or outputs a URL, then blocks until | ||
// the redirect completes to the /oauth2callback URI. | ||
// It returns an instance of an HTTP client that can be passed to the | ||
// constructor of the YouTube client. | ||
func buildOAuthHTTPClient(scope string) (*http.Client, error) { | ||
config, err := readConfig(scope) | ||
if err != nil { | ||
msg := fmt.Sprintf("Cannot read configuration file: %v", err) | ||
return nil, errors.New(msg) | ||
} | ||
|
||
transport := &oauth.Transport{Config: config} | ||
|
||
// Try to read the token from the cache file. | ||
// If an error occurs, do the three-legged OAuth flow because | ||
// the token is invalid or doesn't exist. | ||
token, err := config.TokenCache.Token() | ||
if err != nil { | ||
// Start web server. | ||
// This is how this program receives the authorization code | ||
// when the browser redirects. | ||
codeCh, err := startWebServer() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Open url in browser | ||
url := config.AuthCodeURL("") | ||
err = openURL(url) | ||
if err != nil { | ||
fmt.Println("Visit the URL below to get a code.", | ||
" This program will pause until the site is visted.") | ||
} else { | ||
fmt.Println("Your browser has been opened to an authorization URL.", | ||
" This program will resume once authorization has been provided.\n") | ||
} | ||
fmt.Println(url) | ||
|
||
// Wait for the web server to get the code. | ||
code := <-codeCh | ||
|
||
// This code caches the authorization code on the local | ||
// filesystem, if necessary, as long as the TokenCache | ||
// attribute in the config is set. | ||
token, err = transport.Exchange(code) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
transport.Token = token | ||
return transport.Client(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"installed":{"client_id":"1023236677559-agfhktgml9nqi1j17ek0vmr6c7cjt87f.apps.googleusercontent.com","project_id":"central-web-175404","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":"vqNWDovin1e8vR24HNdxF0_G","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"access_token":"ya29.GluZBJ_ogDg3hhCcldf56ndk6Hx4PTW9y7yfFhFXlyJ4-4zXeXQFRV01-l8ycMFGaLCfHDfLAKsDw5Z8yM1Ns6WVSCoo9IKGngX1f2dxh9dCyM38QYNDCf9lVNgs","token_type":"Bearer","refresh_token":"1/v6zTvG9qcdstbIdQGGtqMvpk1H2QRxi7Z1ioxF66R7Q","expiry":"2017-07-30T23:29:26.611806229-06:00"} |