48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
|
|
package helpers
|
||
|
|
|
||
|
|
import (
|
||
|
|
"encoding/json"
|
||
|
|
"fmt"
|
||
|
|
"os"
|
||
|
|
|
||
|
|
"golang.org/x/oauth2"
|
||
|
|
"golang.org/x/oauth2/google"
|
||
|
|
)
|
||
|
|
|
||
|
|
func GetClientSecret(path string) (*oauth2.Config, error) {
|
||
|
|
b, err := os.ReadFile("client_secret.json")
|
||
|
|
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("unable to read client secret file: %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
clientSecret, err := google.ConfigFromJSON(b, "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/spreadsheets.readonly")
|
||
|
|
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("unable to parse client secret file to config: %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return clientSecret, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func GetToken(path string) (*oauth2.Token, error) {
|
||
|
|
file, err := os.Open(path)
|
||
|
|
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("unable to read token file: %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
token := map[string]string{}
|
||
|
|
|
||
|
|
err = json.NewDecoder(file).Decode(&token)
|
||
|
|
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("unable to parse token file to token: %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return &oauth2.Token{
|
||
|
|
AccessToken: token["access_token"],
|
||
|
|
RefreshToken: token["refresh_token"],
|
||
|
|
}, nil
|
||
|
|
}
|