func getAccessToken(consumerKey, consumerSecret string) (a AccessToken, err error) { c := oauth.NewConsumer( consumerKey, consumerSecret, oauth.ServiceProvider{ RequestTokenUrl: "https://api.twitter.com/oauth/request_token", AuthorizeTokenUrl: "https://api.twitter.com/oauth/authorize", AccessTokenUrl: "https://api.twitter.com/oauth/access_token", }) requestToken, url, err := c.GetRequestTokenAndUrl("oob") if err != nil { log.Fatal(err) } fmt.Println("(1) Go to: " + url) fmt.Println("(2) Grant access, you should get back a verification code.") open.Run(url) fmt.Print("(3) Enter that verification code here: ") verificationCode := "" fmt.Scanln(&verificationCode) authToken, err := c.AuthorizeToken(requestToken, verificationCode) if err != nil { log.Fatal(err) } a.Token = authToken.Token a.Secret = authToken.Secret err = saveAccessToken(a) return }
// Tweet sends a status update to twitter - returns the response body or error func Tweet(s string) ([]byte, error) { consumer := oauth.NewConsumer(key, secret, oauth.ServiceProvider{}) //consumer.Debug(true) token := &oauth.AccessToken{Token: accessToken, Secret: accessTokenSecret} url := "https://api.twitter.com/1.1/statuses/update.json" data := map[string]string{"status": s} response, err := consumer.Post(url, data, token) if err != nil { return nil, err } defer response.Body.Close() fmt.Println("Response:", response.StatusCode, response.Status) body, err := ioutil.ReadAll(response.Body) if err != nil { return nil, err } // Check for unexpected status codes, and report them if response.StatusCode != http.StatusOK { return nil, fmt.Errorf("#error sending tweet, unexpected status:%d\n\n%s\n", response.StatusCode, body) } return body, nil }
func (self TwitterBot) GetMentions() ([]byte, error) { const uri = "https://api.twitter.com/1.1/statuses/mentions_timeline.json" params := map[string]string{ "count": "800", } accessToken := &oauth.AccessToken{ Token: self.config["accessToken"], Secret: self.config["accessTokenSecret"], } consumer := oauth.NewConsumer(self.config["consumerKey"], self.config["consumerSecret"], *self.serviceProvider) resp, err := consumer.Get(uri, params, accessToken) if err != nil { return nil, err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } return body, nil }
func OAuth1() { consumerKey := "rPJwszCjnGXfRIjFquYD" consumerSecret := "kTIjZmEnGCNiQqqRgyhgUfOJFHnSAVdI" oauthConsumer = oauth.NewConsumer( consumerKey, consumerSecret, oauth.ServiceProvider{ RequestTokenUrl: "http://api.discogs.com/oauth/request_token", AuthorizeTokenUrl: "http://www.discogs.com/oauth/authorize", AccessTokenUrl: "http://api.discogs.com/oauth/access_token", }, ) requestToken, url, err := oauthConsumer.GetRequestTokenAndUrl("oob") if err != nil { log.Fatal(err) } fmt.Println("(1) Go to: " + url) fmt.Println("(2) Grant access, you should get back a verification code.") fmt.Println("(3) Enter that verification code here: ") verificationCode := "" fmt.Scanln(&verificationCode) accessToken, err = oauthConsumer.AuthorizeToken(requestToken, verificationCode) if err != nil { log.Fatal(err) } writeToken() }
func (self TwitterBot) SearchRealtime() ([]byte, error) { const uri = "https://api.twitter.com/1.1/statuses/filter.json" params := map[string]string{ "track": "Letta", } accessToken := &oauth.AccessToken{ Token: self.config["accessToken"], Secret: self.config["accessTokenSecret"], } consumer := oauth.NewConsumer(self.config["consumerKey"], self.config["consumerSecret"], *self.serviceProvider) consumer.Debug(true) resp, err := consumer.Post(uri, params, accessToken) if err != nil { return nil, err } defer resp.Body.Close() reader := bufio.NewReader(resp.Body) var ( isprefix bool = true line []byte ) for isprefix && err == nil { line, isprefix, err = reader.ReadLine() fmt.Println("%s", line) } return nil, nil }
func (self TwitterBot) Unfollow(screenName string) ([]byte, error) { const uri = "https://api.twitter.com/1.1/friendships/destroy.json" params := map[string]string{ "screen_name": screenName, } accessToken := &oauth.AccessToken{ Token: self.config["accessToken"], Secret: self.config["accessTokenSecret"], } consumer := oauth.NewConsumer(self.config["consumerKey"], self.config["consumerSecret"], *self.serviceProvider) resp, err := consumer.Post(uri, params, accessToken) if err != nil { return nil, err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } return body, nil }
func buildOAuthHTTPClient(consumerKey, consumerSecret, accessToken, accessTokenSecret string) (*http.Client, error) { consumer := oauth.NewConsumer( consumerKey, consumerSecret, oauth.ServiceProvider{ RequestTokenUrl: RequestTokenUrl, AuthorizeTokenUrl: AuthorizeTokenUrl, AccessTokenUrl: AccessTokenUrl, }, ) consumer.AdditionalAuthorizationUrlParams = map[string]string{ "Access": URLParamAccessFull, "Permissions": URLParamPermissionsModify, } token := &oauth.AccessToken{ Token: accessToken, Secret: accessTokenSecret, } client, err := consumer.MakeHttpClient(token) if err != nil { return nil, err } return client, nil }
func main() { var consumerKey string = "golang-test-key" var consumerSecretPath string = "../../../../keys/8008/8080/" + consumerKey secret, err := ioutil.ReadFile(consumerSecretPath) check(err) consumerSecret := string(secret) c := oauth.NewConsumer( consumerKey, consumerSecret, oauth.ServiceProvider{ RequestTokenUrl: "", AuthorizeTokenUrl: "", AccessTokenUrl: "", }) //c.Debug(true) var emptyAccessToken oauth.AccessToken response, err := c.Get( "http://localhost:8008/job/12345", map[string]string{}, &emptyAccessToken) if err != nil { log.Fatal(err) } defer response.Body.Close() bits, err := ioutil.ReadAll(response.Body) fmt.Println(string(bits)) }
func makeClients() []*http.Client { var clients []*http.Client for _, key := range getKeys() { consumer := oauth.NewConsumer( key[0], key[1], oauth.ServiceProvider{ RequestTokenUrl: "https://api.twitter.com/oauth/request_token", AuthorizeTokenUrl: "https://api.twitter.com/oauth/authorize", AccessTokenUrl: "https://api.twitter.com/oauth/access_token", }) at := oauth.AccessToken{ Token: key[2], Secret: key[3], } client, err := consumer.MakeHttpClient(&at) if err != nil { log.Fatalln(err) } clients = append(clients, client) } return clients }
func main() { tokens = make(map[string]*oauth.RequestToken) config := parseConfig() c = oauth.NewConsumer( config.ConsumerApiKey, config.ConsumerSecret, oauth.ServiceProvider{ RequestTokenUrl: "https://api.twitter.com/oauth/request_token", AuthorizeTokenUrl: "https://api.twitter.com/oauth/authorize", AccessTokenUrl: "https://api.twitter.com/oauth/access_token", }, ) r := httprouter.New() r.GET("/", IndexAction) r.GET("/api/auth", RedirectUserToTwitter) r.GET("/api/maketoken", GetTwitterToken) r.GET("/api/collage", GenerateCollageAction) r.GET("/api/verify_auth", VerifyAuthAction) r.ServeFiles("/static/*filepath", http.Dir("static/")) fmt.Println("Server running on port :8000") http.ListenAndServe(":8000", r) }
// OAuth1Client returns oauth.Consumer using OAuthProvider details func (o *OAuthProvider) OAuth1Client(c *Context) *oauth.Consumer { if o.ID == "" { log.Error(errors.New("Can't get OAuth1Client – ID not set")) return nil } service := c.Service() config := service.DefaultOAuth1 if config.AccessTokenReceiver == nil { log.Error(errors.New("Can't get OAuth1Client – AccessTokenReceiver not set")) return nil } config.Key = o.ID config.Secret = o.Secret config.AccessTokenURL = replaceBaseURL(config.AccessTokenURL, o.BaseURL) config.AuthorizeTokenURL = replaceBaseURL(config.AuthorizeTokenURL, o.BaseURL) config.RequestTokenURL = replaceBaseURL(config.RequestTokenURL, o.BaseURL) consumer := oauth.NewConsumer( o.ID, o.Secret, oauth.ServiceProvider{ RequestTokenUrl: config.RequestTokenURL, AuthorizeTokenUrl: config.AuthorizeTokenURL, AccessTokenUrl: config.AccessTokenURL, }, ) consumer.AdditionalAuthorizationUrlParams = service.DefaultOAuth1.AdditionalAuthorizationURLParams return consumer }
func loadOAuthConsumers(filename string, oauthProviders OAuthProviders) (OAuthConsumers, error) { b, err := ioutil.ReadFile(filename) if err != nil { return nil, err } // parse the JSON into generic map var data map[string]interface{} err = json.Unmarshal(b, &data) if err != nil { return nil, err } // resolve references to OAuth providers and construct result consumers := make(OAuthConsumers) for k, v := range data { c := v.(map[string]interface{}) provName := c["provider"].(string) if len(provName) == 0 { return nil, fmt.Errorf("unspecified provider for consumer: %s", k) } provider := oauthProviders[provName] if provider == nil { return nil, fmt.Errorf("unknown provider: %s", provName) } key, secret := c["key"].(string), c["secret"].(string) if len(key) == 0 || len(secret) == 0 { return nil, fmt.Errorf("unspecified key and/or secret for consumer: %s", k) } consumers[k] = oauth.NewConsumer(key, secret, *provider) } return consumers, nil }
func (self TwitterBot) GetFollowers(cursor string) ([]byte, error) { const uri = "https://api.twitter.com/1.1/followers/list.json" params := map[string]string{ "count": "1000", "screen_name": self.config["twitterHandle"], "skip_status": "1", "include_user_entities": "0", "cursor": cursor, } accessToken := &oauth.AccessToken{ Token: self.config["accessToken"], Secret: self.config["accessTokenSecret"], } consumer := oauth.NewConsumer(self.config["consumerKey"], self.config["consumerSecret"], *self.serviceProvider) resp, err := consumer.Get(uri, params, accessToken) if err != nil { return nil, err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } return body, nil }
func (self TwitterBot) Search() ([]byte, error) { const uri = "https://api.twitter.com/1.1/search/tweets.json" params := map[string]string{ "count": "100", "q": "#RivoluzDigitale", "result_type": "mixed", } accessToken := &oauth.AccessToken{ Token: self.config["accessToken"], Secret: self.config["accessTokenSecret"], } consumer := oauth.NewConsumer(self.config["consumerKey"], self.config["consumerSecret"], *self.serviceProvider) resp, err := consumer.Get(uri, params, accessToken) if err != nil { return nil, err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } return body, nil }
func makeConsumer(conf Config, name, expiration, scope string) (*oauth.Consumer, error) { c := oauth.NewConsumer( conf.ConsumerKey, conf.ConsumerSecret, oauth.ServiceProvider{ RequestTokenUrl: conf.RequestTokenUrl, AuthorizeTokenUrl: conf.AuthorizeTokenUrl, AccessTokenUrl: conf.AccessTokenUrl, }, ) if name == "" { return nil, fmt.Errorf("Application name required for oauth consumer") } c.AdditionalAuthorizationUrlParams["name"] = name err := validateExpiration(expiration) if err != nil { return nil, err } c.AdditionalAuthorizationUrlParams["expiration"] = expiration err = validateScope(scope) if err != nil { return nil, err } c.AdditionalAuthorizationUrlParams["scope"] = scope return c, nil }
func (self TwitterBot) GetTweets(user string) ([]byte, error) { const uri = "https://api.twitter.com/1.1/statuses/user_timeline.json" params := map[string]string{ "count": "200", "screen_name": user, //"trim_user": "******", //"max_id": "315451926944833537", } accessToken := &oauth.AccessToken{ Token: self.config["accessToken"], Secret: self.config["accessTokenSecret"], } consumer := oauth.NewConsumer(self.config["consumerKey"], self.config["consumerSecret"], *self.serviceProvider) resp, err := consumer.Get(uri, params, accessToken) if err != nil { return nil, err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } return body, nil }
func CallTwitter(ep string, query map[string]string) (io.ReadCloser, error) { consumer := oauth.NewConsumer(conf.ConsumerKey, conf.ConsumerSecret, oauth.ServiceProvider{}) //consumer.Debug(true) accessToken := &oauth.AccessToken{ Token: conf.AccessToken, Secret: conf.AccessTokenSecret, } // client, err := consumer.MakeHttpClient(accessToken) // if err != nil { // return t, err // } // query := url.QueryEscape("q=" + s + "&page=1&count=20") // ep := fmt.Sprintf("%s/users/search.json?%s", twitterEndPoint, query) // response, err := client.Get(ep) // FIXME this is deprecated but MakeHttpClient makes a bad // auth because do not encode "&" in "&" response, err := consumer.Get(twitterEndPoint+ep, query, accessToken) if err != nil { return nil, err } if response.StatusCode != 200 { b, _ := ioutil.ReadAll(response.Body) return nil, fmt.Errorf("%s, %s", http.StatusText(response.StatusCode), string(b)) } return response.Body, nil }
func main() { var consumerKey string = os.Args[1] var consumerSecret string = os.Args[2] var tweet string = string(os.Args[5]) c := oauth.NewConsumer( consumerKey, consumerSecret, oauth.ServiceProvider{ RequestTokenUrl: "http://api.twitter.com/oauth/request_token", AuthorizeTokenUrl: "https://api.twitter.com/oauth/authorize", AccessTokenUrl: "https://api.twitter.com/oauth/access_token", }) at := oauth.AccessToken{Token: os.Args[3], Secret: os.Args[4]} _, err := c.Post( "http://api.twitter.com/1/statuses/update.json", "", map[string]string{ "key": "", "status": tweet, }, &at) if err != nil { log.Fatal(err) } }
func main() { var screenName *string = flag.String("name", "", "twitter user name") var consumerKey *string = flag.String("key", "", "") var consumerSecret *string = flag.String("secret", "", "") flag.Parse() fmt.Printf("fetching for: %s \n", *screenName) if *consumerKey == "" || *consumerSecret == "" { // panic("needed key and secret, exiting") log.Fatal("needed key and secret, exiting") os.Exit(1) } c := oauth.NewConsumer( *consumerKey, *consumerSecret, oauth.ServiceProvider{ RequestTokenUrl: "https://api.twitter.com/oauth/request_token", AuthorizeTokenUrl: "https://api.twitter.com/oauth/authorize", AccessTokenUrl: "https://api.twitter.com/oauth/access_token", }) c.Debug(true) requestToken, u, err := c.GetRequestTokenAndUrl("oob") if err != nil { log.Fatal(err) } exec.Command("google-chrome-stable", u).Run() fmt.Println("(1) Grant access, you should get back a verification code.") fmt.Println("(2) Enter that verification code here: ") verificationCode := "" fmt.Scanln(&verificationCode) accessToken, err := c.AuthorizeToken(requestToken, verificationCode) if err != nil { log.Fatal(err) } client, err := c.MakeHttpClient(accessToken) if err != nil { log.Fatal(err) } response, err := client.Get( "https://api.twitter.com/1.1/statuses/home_timeline.json?count=1") if err != nil { log.Fatal(err) } defer response.Body.Close() bits, err := ioutil.ReadAll(response.Body) fmt.Println("The newest item in your home timeline is: " + string(bits)) }
func main() { var consumerKey *string = flag.String("consumerkey", "xxx", "") var consumerSecret *string = flag.String("consumersecret", "xxx", "") var cookieSecret *string = flag.String("cookiesecret", "xxx", "") var mongoUrl *string = flag.String("mongourl", "xxx", "") flag.Parse() consumer = oauth.NewConsumer( *consumerKey, *consumerSecret, oauth.ServiceProvider{ RequestTokenUrl: "https://api.twitter.com/oauth/request_token", AuthorizeTokenUrl: "https://api.twitter.com/oauth/authorize", AccessTokenUrl: "https://api.twitter.com/oauth/access_token", }) var err os.Error mgoPool, err = mgo.Mongo(*mongoUrl) if err != nil { panic(err) } defer mgoPool.Close() tplSet = new(template.Set) fmap := make(map[string]interface{}) fmap["cut"] = CutString fmap["anchor"] = GenAnchorTagStr fmap["sn2url"] = GetTwitterAccountURL fmap["ht2url"] = GetSearchResultURL fmap["d2url"] = GetDomainURL tplSet.Funcs(fmap) tplSet = template.SetMust( tplSet.ParseTemplateFiles( "tpl/index.html", "tpl/place.html", "tpl/hashtag.html", "tpl/url.html", "tpl/domain.html", "tpl/sn.html", "tpl/tab.html", "tpl/link.html", )) f, ferr := os.Create("server.log") if ferr != nil { panic(ferr) } logger := log.New(f, "", log.Ldate|log.Ltime) web.SetLogger(logger) web.Config.StaticDir = "../htdocs" web.Config.CookieSecret = *cookieSecret web.Get("/web/login", onLogin) web.Get("/web/callback", onCallback) web.Get("/web/stats/([0-9a-zA-Z_]+)/([a-z]+)/([0-9 ]*)", onStats) web.Get("/web", onStatsDef) web.Run("127.0.0.1:8080") }
func NewClient(consumerKey, consumerSecret string) Rdio { c := oauth.NewConsumer(consumerKey, consumerSecret, oauth.ServiceProvider{ RequestTokenUrl: "http://api.rdio.com/oauth/request_token", AuthorizeTokenUrl: "https://www.rdio.com/oauth/authorize", AccessTokenUrl: "http://api.rdio.com/oauth/access_token", }) return Rdio{consumer: c} }
// Create a new Fanfou client, root should be RootFanfou or RootSandbox func (c *Fanfou) NewFanfouClient() (*FanfouClient, error) { if c.Key == "" || c.Secret == "" { return nil, errors.New("No consumer key or secret") } return &FanfouClient{ oauth.NewConsumer(c.Key, c.Secret, *fanfouService), c.AccessToken, }, nil }
// GetConsumer generates an OAuth Consumer for the Yahoo fantasy sports API func GetConsumer(clientID string, clientSecret string) *oauth.Consumer { return oauth.NewConsumer( clientID, clientSecret, oauth.ServiceProvider{ RequestTokenUrl: YahooRequestTokenURL, AuthorizeTokenUrl: YahooAuthTokenURL, AccessTokenUrl: YahooGetTokenURL, }) }
func AuthenticatedClient(consumerKey, consumerSecret, accessToken, accessTokenSecret string) Rdio { token := oauth.AccessToken{Token: accessToken, Secret: accessTokenSecret} c := oauth.NewConsumer(consumerKey, consumerSecret, oauth.ServiceProvider{ RequestTokenUrl: "http://api.rdio.com/oauth/request_token", AuthorizeTokenUrl: "https://www.rdio.com/oauth/authorize", AccessTokenUrl: "http://api.rdio.com/oauth/access_token", }) return Rdio{consumer: c, AccessToken: &token} }
func GetClient() *oauth.Consumer { return oauth.NewConsumer( kocha.Getenv("RETWITTER_TWITTER_CONSUMER_KEY", ""), kocha.Getenv("RETWITTER_TWITTER_CONSUMER_SECRET", ""), oauth.ServiceProvider{ AuthorizeTokenUrl: "https://api.twitter.com/oauth/authorize", RequestTokenUrl: "https://api.twitter.com/oauth/request_token", AccessTokenUrl: "https://api.twitter.com/oauth/access_token", }, ) }
func createTwitterOAuth() *oauth.Consumer { return oauth.NewConsumer( revel.Config.StringDefault("twitter.key", ""), revel.Config.StringDefault("twitter.secret", ""), oauth.ServiceProvider{ AuthorizeTokenUrl: "https://api.twitter.com/oauth/authorize", RequestTokenUrl: "https://api.twitter.com/oauth/request_token", AccessTokenUrl: "https://api.twitter.com/oauth/access_token", }, ) }
func newConsumer(credentials *Credentials) *oauth.Consumer { return oauth.NewConsumer( credentials.ConsumerKey, credentials.ConsumerSecret, oauth.ServiceProvider{ RequestTokenUrl: requestUrl, AuthorizeTokenUrl: authUrl, AccessTokenUrl: tokenUrl, }) }
func NewTwitterConsumer(consumerKey, consumerSecret string) *oauth.Consumer { return oauth.NewConsumer( consumerKey, consumerSecret, oauth.ServiceProvider{ RequestTokenUrl: "https://api.twitter.com/oauth/request_token", AuthorizeTokenUrl: "https://api.twitter.com/oauth/authorize", AccessTokenUrl: "https://api.twitter.com/oauth/access_token", }, ) }
func NewConsumer(credentials *Credentials, authpoints *AuthPoints) *oauth.Consumer { return oauth.NewConsumer( credentials.ConsumerKey, credentials.ConsumerSecret, oauth.ServiceProvider{ RequestTokenUrl: authpoints.RequestToken, AuthorizeTokenUrl: authpoints.Authorize, AccessTokenUrl: authpoints.AccessToken, }) }
func newOauthConsumer() *oauth.Consumer { consumerKey := os.Getenv("TWITTER_CONSUMER_KEY") consumerSecret := os.Getenv("TWITTER_CONSUMER_SECRET") provider := oauth.ServiceProvider{ RequestTokenUrl: "https://api.twitter.com/oauth/request_token", AuthorizeTokenUrl: "https://api.twitter.com/oauth/authorize", AccessTokenUrl: "https://api.twitter.com/oauth/access_token", } return oauth.NewConsumer(consumerKey, consumerSecret, provider) }