func makeCredential() error { url, cred, err := anaconda.AuthorizationURL("") if err != nil { log.Println("something wrong happen") return err } fmt.Println("please access this url:", url) var vel string fmt.Print("enter verifier: ") fmt.Scanln(&vel) cred, _, err = anaconda.GetCredentials(cred, vel) if err != nil { log.Println("authentification failed") return err } c := Credential{AccessToken: cred.Token, AccessSecret: cred.Secret} b, err := json.Marshal(c) f, err := os.Create(confPath) if err != nil { log.Println("can't make conf file") return err } defer f.Close() err = ioutil.WriteFile(confPath, b, 0600) if err != nil { return err } return nil }
func (app *GoApp) callbackHandler(w http.ResponseWriter, r *http.Request) { session, _ := app.store.Get(r, sessionName) // アクセストークンを取得したらファイルに書き込みつつログイン情報をsessionへ // セッション中の情報をパース var cred oauth.Credentials err := json.Unmarshal([]byte(session.Values["oauth_credentials"].(string)), &cred) if err != nil { http.Error(w, "Session has broken.", http.StatusInternalServerError) fmt.Println(err) return } // 取得した証明書を元にアクセストークンを取得 _, values, err := anaconda.GetCredentials(&cred, r.FormValue("oauth_verifier")) if err != nil { http.Error(w, "Couldn't authorize.", http.StatusInternalServerError) fmt.Println(err) return } // セッションクリア session.Values = make(map[interface{}]interface{}) // ユーザー情報記録 // oauth_token, oauth_token_secret, user_id, screen_name, x_auth_expires for k, v := range values { session.Values[k] = v[0] } session.Save(r, w) // 認可後はhomeへ http.Redirect(w, r, "/", http.StatusFound) }
func getCred() { key, cred, _ := anaconda.AuthorizationURL("") fmt.Printf("access: %v\n", key) fmt.Printf("Key: ") buf := bufio.NewReader(os.Stdin) line, _, _ := buf.ReadLine() cred, _, _ = anaconda.GetCredentials(cred, string(line)) fmt.Printf("cred: \n", cred) }
func AccessTokenHandler(w http.ResponseWriter, r *http.Request) { c, _, err := anaconda.GetCredentials(credential, r.URL.Query().Get("oauth_verifier")) if err != nil { fmt.Fprintf(w, "%v", err) return } t, err := template.ParseFiles(path.Join(rootDir, "access_token.html")) if err != nil { fmt.Fprintf(w, "%v", err) return } t.Execute(w, c) }
func DoAuth(pin string) (name string, _ bool) { accessToken, vals, err := anaconda.GetCredentials(&requestToken, pin) if err != nil { fmt.Printf("GetCredentials failed: %v\n", err) return "", false } names := vals["screen_name"] if len(names) != 0 { name = names[0] } storeCredential(accessToken, name) return name, true }
func main() { anaconda.SetConsumerKey("j62kyFWBVqCiJElWSHvofSz59") anaconda.SetConsumerSecret("FmA9RF7MAEwLbvXfvYRgZMMstinToo6kbR7CcwfpdhWrsEPyrg") // webbrowser.Open() url, cred, _ := anaconda.AuthorizationURL("") webbrowser.Open(url) var pincode int64 fmt.Scan(&pincode) newcred, _, _ := anaconda.GetCredentials(cred, strconv.FormatInt(pincode, 10)) fmt.Printf("%v\n%v\n", newcred.Token, newcred.Secret) }
func twitterAuth() (*oauth.Credentials, error) { url, tempCred, _ := anaconda.AuthorizationURL("oob") fmt.Printf("Go to %s and enter code: ", url) var code string fmt.Scanln(&code) credentials, _, err := anaconda.GetCredentials(tempCred, code) if err != nil { return nil, err } return credentials, nil }
func (g *gweet) setupAccount() (err error) { fmt.Printf("Account name: ") var name string fmt.Scanln(&name) authUrl, tmpCred, err := anaconda.AuthorizationURL("oob") if err != nil { return } exec.Command("xdg-open", authUrl).Run() fmt.Printf( "1. Go to %s (should have opened automatically)\n"+ "2. Authorize the application\n"+ "3. Enter verification code: ", authUrl, ) var pin string fmt.Scanln(&pin) cred, _, err := anaconda.GetCredentials(tmpCred, pin) if err != nil { return } g.Accounts[name] = *cred if len(g.DefaultAccount) == 0 { g.DefaultAccount = name } newJson, err := json.MarshalIndent(&g, "", " ") if err != nil { return } cfgPath, err := configPath() if err != nil { return } err = ioutil.WriteFile(cfgPath, newJson, os.ModePerm) return }
// Callback handles the twitter oauth callback func Callback(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { var verifier string q := r.URL.Query() fmt.Println(q.Get("oauth_verifier")) if q.Get("oauth_verifier") != "" { verifier = q.Get("oauth_verifier") } else { log.Fatalln("the user declined your authentication request! :(") } credentials, _, err := anaconda.GetCredentials(cred, verifier) if err != nil { // handle error } accessToken = credentials.Token accessSecret = credentials.Secret http.Redirect(w, r, "/", 302) }