func loadGoogm() (*googm.Client, error) { fh, err := os.Open(OAUTH_CREDENTIAL_FILE) if err != nil { return nil, err } defer fh.Close() if err := json.NewDecoder(fh).Decode(&GOOGM_CREDENTIALS); err != nil { return nil, err } client := OauthConfig.Client(oauth2.NoContext, &GOOGM_CREDENTIALS.Token) return googm.NewClient(client, GOOGM_CREDENTIALS.ID) }
func register() error { var id string = "00:01:02:" + time.Now().Format("15:04:05") var name string = VERSION url := OauthConfig.AuthCodeURL("", oauth2.AccessTypeOffline) p.MSG.Println(`Please open the following URL in a browser to authorize gmusic with your Google account. Copy the code given to you at the end of the authorization process below. %s > `, url) var code string if _, err := fmt.Scanln(&code); err != nil { return err } tok, err := OauthConfig.Exchange(oauth2.NoContext, code) if err != nil { return err } httpclient := OauthConfig.Client(oauth2.NoContext, tok) client, err := googm.NewClient(httpclient, id) if err != nil { return err } if err := client.Register(name); err != nil { return err } f, err := os.Create(OAUTH_CREDENTIAL_FILE) if err != nil { return err } defer f.Close() GOOGM_CREDENTIALS.ID = id GOOGM_CREDENTIALS.Token = *tok if err := json.NewEncoder(f).Encode(GOOGM_CREDENTIALS); err != nil { return err } p.MSG.Println("registration successful\n") return nil }