forked from prasmussen/gdrive
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Go fmt the code and use proper imports
This allows people to be able to go get your project.
- Loading branch information
1 parent
d9b1ad5
commit 019930d
Showing
7 changed files
with
670 additions
and
672 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 |
---|---|---|
@@ -1,68 +1,68 @@ | ||
package auth | ||
|
||
import ( | ||
"net/http" | ||
"fmt" | ||
"code.google.com/p/goauth2/oauth" | ||
"../util" | ||
"code.google.com/p/goauth2/oauth" | ||
"fmt" | ||
"github.com/prasmussen/gdrive/util" | ||
"net/http" | ||
) | ||
|
||
// Get auth code from user | ||
func promptUserForAuthCode(config *oauth.Config) string { | ||
authUrl := config.AuthCodeURL("state") | ||
fmt.Println("Go to the following link in your browser:") | ||
fmt.Printf("%v\n\n", authUrl) | ||
return util.Prompt("Enter verification code: ") | ||
authUrl := config.AuthCodeURL("state") | ||
fmt.Println("Go to the following link in your browser:") | ||
fmt.Printf("%v\n\n", authUrl) | ||
return util.Prompt("Enter verification code: ") | ||
} | ||
|
||
// Returns true if we have a valid cached token | ||
func hasValidToken(cacheFile oauth.CacheFile, transport *oauth.Transport) bool { | ||
// Check if we have a cached token | ||
token, err := cacheFile.Token() | ||
if err != nil { | ||
return false | ||
} | ||
// Check if we have a cached token | ||
token, err := cacheFile.Token() | ||
if err != nil { | ||
return false | ||
} | ||
|
||
// Refresh token if its expired | ||
if token.Expired() { | ||
transport.Token = token | ||
err = transport.Refresh() | ||
if err != nil { | ||
fmt.Println(err) | ||
return false | ||
} | ||
} | ||
return true | ||
// Refresh token if its expired | ||
if token.Expired() { | ||
transport.Token = token | ||
err = transport.Refresh() | ||
if err != nil { | ||
fmt.Println(err) | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func GetOauth2Client(clientId, clientSecret, cachePath string) (*http.Client, error) { | ||
cacheFile := oauth.CacheFile(cachePath) | ||
cacheFile := oauth.CacheFile(cachePath) | ||
|
||
config := &oauth.Config{ | ||
ClientId: clientId, | ||
ClientSecret: clientSecret, | ||
Scope: "https://www.googleapis.com/auth/drive", | ||
RedirectURL: "urn:ietf:wg:oauth:2.0:oob", | ||
AuthURL: "https://accounts.google.com/o/oauth2/auth", | ||
TokenURL: "https://accounts.google.com/o/oauth2/token", | ||
TokenCache: cacheFile, | ||
} | ||
config := &oauth.Config{ | ||
ClientId: clientId, | ||
ClientSecret: clientSecret, | ||
Scope: "https://www.googleapis.com/auth/drive", | ||
RedirectURL: "urn:ietf:wg:oauth:2.0:oob", | ||
AuthURL: "https://accounts.google.com/o/oauth2/auth", | ||
TokenURL: "https://accounts.google.com/o/oauth2/token", | ||
TokenCache: cacheFile, | ||
} | ||
|
||
transport := &oauth.Transport{ | ||
Config: config, | ||
Transport: http.DefaultTransport, | ||
} | ||
transport := &oauth.Transport{ | ||
Config: config, | ||
Transport: http.DefaultTransport, | ||
} | ||
|
||
// Return client if we have a valid token | ||
if hasValidToken(cacheFile, transport) { | ||
return transport.Client(), nil | ||
} | ||
// Return client if we have a valid token | ||
if hasValidToken(cacheFile, transport) { | ||
return transport.Client(), nil | ||
} | ||
|
||
// Get auth code from user and request a new token | ||
code := promptUserForAuthCode(config) | ||
_, err := transport.Exchange(code) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return transport.Client(), nil | ||
// Get auth code from user and request a new token | ||
code := promptUserForAuthCode(config) | ||
_, err := transport.Exchange(code) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return transport.Client(), nil | ||
} |
Oops, something went wrong.