// Hashtags returns a channel over which tweets can be received, which match // at least one of the given hashtags. func Hashtags(cred *Credentials, Hashtags ...string) (<-chan *Tweet, error) { c := make(chan *Tweet) restclient := twittergo.NewClient(&oauth1a.ClientConfig{ ConsumerKey: cred.ConsumerKey, ConsumerSecret: cred.ConsumerSecret, }, &oauth1a.UserConfig{ AccessTokenKey: cred.AccessToken, AccessTokenSecret: cred.AccessSecret, }) httpstream.OauthCon = &oauth.OAuthConsumer{ ConsumerKey: cred.ConsumerKey, ConsumerSecret: cred.ConsumerSecret, } streamclient := httpstream.NewOAuthClient(&oauth.AccessToken{ Token: cred.AccessToken, Secret: cred.AccessSecret, }, newChannelConverter(c, restclient)) err := streamclient.Filter(nil, Hashtags, nil, nil, false, nil) if err != nil { close(c) } return c, err }
// Connect initializes a connection via Twitter API. func Connect() *twittergo.Client { config := &oauth1a.ClientConfig{ ConsumerKey: Key, ConsumerSecret: Secret, } return twittergo.NewClient(config, nil) }
func GetCredentials(config *Config) (client *twittergo.Client) { oc := &oauth1a.ClientConfig{ ConsumerKey: config.OAuth.Key, ConsumerSecret: config.OAuth.Secret, } return twittergo.NewClient(oc, nil) }
func LoadCredentials() (client *twittergo.Client, err error) { credentials, err := ioutil.ReadFile("account.yaml") if err != nil { fmt.Println("There is no account.yaml") return nil, err } m := make(map[interface{}]interface{}) err = yaml.Unmarshal([]byte(credentials), &m) if err != nil { log.Fatalf("error: %v", err) } twitter := m["twitter"].(map[interface{}]interface{}) //fmt.Printf("%v\n", twitter) fmt.Printf("[%v]\n", twitter["consumerKey"]) fmt.Printf("[%v]\n", twitter["consumerSecret"]) fmt.Printf("[%v]\n", twitter["accessToken"]) fmt.Printf("[%v]\n", twitter["accessTokenSecret"]) config := &oauth1a.ClientConfig{ ConsumerKey: twitter["consumerKey"].(string), ConsumerSecret: twitter["consumerSecret"].(string), } user := oauth1a.NewAuthorizedConfig(twitter["accessToken"].(string), twitter["accessTokenSecret"].(string)) client = twittergo.NewClient(config, user) return client, err }
func LoadCredentials() (client *twittergo.Client, err error) { config := &oauth1a.ClientConfig{ ConsumerKey: bot.Config.TwitterConsumerKey, ConsumerSecret: bot.Config.TwitterConsumerSecret, } client = twittergo.NewClient(config, nil) return }
func LoadCredentials() (client *twittergo.Client, err error) { config := &oauth1a.ClientConfig{ ConsumerKey: os.Getenv("CONSUMER_KEY"), ConsumerSecret: os.Getenv("CONSUMER_SECRET"), } user := oauth1a.NewAuthorizedConfig(os.Getenv("API_KEY"), os.Getenv("API_SECRET")) client = twittergo.NewClient(config, user) return }
func createClient() *twittergo.Client { a := services.Config.Twitter.Auth config := &oauth1a.ClientConfig{ ConsumerKey: a.Consumer_key, ConsumerSecret: a.Consumer_secret, } user := oauth1a.NewAuthorizedConfig(a.Token, a.Token_secret) return twittergo.NewClient(config, user) }
func loadCredentials() (client *twittergo.Client) { config := &oauth1a.ClientConfig{ ConsumerKey: os.Getenv("twitter_ConsumerKey"), ConsumerSecret: os.Getenv("twitter_ConsumerSecret"), } user := oauth1a.NewAuthorizedConfig( os.Getenv("twitter_AccessToken"), os.Getenv("twitter_AcessTokenSecret"), ) client = twittergo.NewClient(config, user) return }
// The loading of credentials, the login, and tweeting functionality // has been addapted from the example provided with github.com/kurrik/twittergo func logIn(bot *TweetBot) (client *twittergo.Client, err error) { if len(bot.credentials) > 0 { lines := strings.Split(string(bot.credentials), "\n") config := &oauth1a.ClientConfig{ ConsumerKey: lines[0], ConsumerSecret: lines[1], } user := oauth1a.NewAuthorizedConfig(lines[2], lines[3]) client = twittergo.NewClient(config, user) } return }
func getTwitter(config Config) *twittergo.Client { twitterConfig := &oauth1a.ClientConfig{ ConsumerKey: config.Twitter.ConsumerKey, ConsumerSecret: config.Twitter.ConsumerSecret, } user := oauth1a.NewAuthorizedConfig( config.Twitter.AccessToken, config.Twitter.AccessTokenSecret, ) return twittergo.NewClient(twitterConfig, user) }
func LoadCredentials() (client *twittergo.Client, err error) { credentials, err := ioutil.ReadFile("CREDENTIALS") if err != nil { return } lines := strings.Split(string(credentials), "\n") config := &oauth1a.ClientConfig{ ConsumerKey: lines[0], ConsumerSecret: lines[1], } client = twittergo.NewClient(config, nil) return }
func NewTwitterUserClient() *twitterComm { config := &oauth1a.ClientConfig{ ConsumerKey: "my-api-key", ConsumerSecret: "my-api-secret", } user := oauth1a.NewAuthorizedConfig("oauth", "sucks") client := twittergo.NewClient(config, user) if err := client.FetchAppToken(); err != nil { fmt.Println("Error occured creating the client.", err) panic("ERROR CREATING USER CLIENT") } return &twitterComm{client} }
func LoadCredentials() (client *twittergo.Client, err error) { credentials, err := ioutil.ReadFile(credentials) if err != nil { return } lines := strings.Split(string(credentials), "\n") config := &oauth1a.ClientConfig{ ConsumerKey: lines[0], ConsumerSecret: lines[1], } user := oauth1a.NewAuthorizedConfig(lines[2], lines[3]) client = twittergo.NewClient(config, user) return }
func main() { // create the config config := &oauth1a.ClientConfig{ ConsumerKey: twitterConsumerKey, ConsumerSecret: twitterConsumerSecret, } // create the client c := twittergo.NewClient(config, nil) if err := c.FetchAppToken(); err != nil { fmt.Fprintf(os.Stderr, "Could not fetch app token: %v\n", err) os.Exit(2) } // we don't need to save the token I am lazy _ = c.GetAppToken() // create the request v := url.Values{} v.Set("q", hashtag) req, err := http.NewRequest("GET", "/1.1/search/tweets.json?"+v.Encode(), nil) if err != nil { fmt.Fprintf(os.Stderr, "Could not parse request: %v\n", err) os.Exit(2) } resp, err := c.SendRequest(req) if err != nil { fmt.Fprintf(os.Stderr, "Could not send request: %v", err) os.Exit(2) } if resp.HasRateLimit() { fmt.Printf("Rate limit: %v\n", resp.RateLimit()) fmt.Printf("Rate limit remaining: %v\n", resp.RateLimitRemaining()) fmt.Printf("Rate limit reset: %v\n", resp.RateLimitReset()) } sr := &twittergo.SearchResults{} if err := resp.Parse(sr); err != nil { fmt.Fprintf(os.Stderr, "Problem parsing response: %v\n", err) os.Exit(2) } tweets := sr.Statuses() for _, t := range tweets { fmt.Println(t.Text()) } }
func LoadCredentials() (client *twittergo.Client, err error) { twitter, err := readYaml() if err != nil { return } config := &oauth1a.ClientConfig{ ConsumerKey: twitter["consumerKey"].(string), ConsumerSecret: twitter["consumerSecret"].(string), } user := oauth1a.NewAuthorizedConfig(twitter["accessToken"].(string), twitter["accessTokenSecret"].(string)) client = twittergo.NewClient(config, user) return }
func main() { config_file, err := ioutil.ReadFile("CREDENTIALS") if err != nil { return } lines := strings.Split(string(config_file), "\n") config := &oauth1a.ClientConfig{ ConsumerKey: lines[0], ConsumerSecret: lines[1], } user := oauth1a.NewAuthorizedConfig(lines[2], lines[3]) client := twittergo.NewClient(config, user) fmt.Println(client) fmt.Println(&twittergo.Timeline{}) }
func loadCredentials() *twittergo.Client { err := godotenv.Load() if err != nil { log.Fatal(".env file missing") } consumerKey := os.Getenv("TWITTER_CONSUMER_KEY") consumerSecret := os.Getenv("TWITTER_CONSUMER_SECRET") config := &oauth1a.ClientConfig{ ConsumerKey: consumerKey, ConsumerSecret: consumerSecret, } accessToken := os.Getenv("TWITTER_ACCESS_TOKEN") accessTokenSecret := os.Getenv("TWITTER_ACCESS_TOKEN_SECRET") user := oauth1a.NewAuthorizedConfig(accessToken, accessTokenSecret) return twittergo.NewClient(config, user) }
func sync(name string, user *config.User) { if user.Enabled { weibo_account := user.GetAccount("tsina") twitter_account := user.GetAccount("twitter") posts := Timeline(weibo_account.Name, user.Last_weibo_id) oauth_user := oauth1a.NewAuthorizedConfig(twitter_account.Oauth_token_key, twitter_account.Oauth_token_secret) client := twittergo.NewClient(twitter_config, oauth_user) for i := len(posts) - 1; i >= 0; i-- { post := posts[i] if post.Id > user.Last_weibo_id { user.Last_weibo_id = post.Id tweet, err := Tweet(client, post.Text) log.Println(weibo_account.Name, post.Text, tweet) if err != nil { log.Println("[error]", tweet, err) } } } } }
// New creates a new instance of the Twitter Social func New(credentials *social.Credentials) (twitter *Twitter, err error) { log.Debugln("Creating a new Twitter Social") var user *oauth1a.UserConfig if !credentials.AppOnly() { log.Debugln("Application only authorization") user = oauth1a.NewAuthorizedConfig(credentials.Token, credentials.TokenSecret) } config := &oauth1a.ClientConfig{ ConsumerKey: credentials.Key, ConsumerSecret: credentials.Secret, } client := twittergo.NewClient(config, user) // Assign for return value twitter = &Twitter{ api: client, credentials: credentials, } return }
func GetTwitterClient(ctx appengine.Context) (c *twittergo.Client, err error) { var ( cred *Credentials config *oauth1a.ClientConfig user *oauth1a.UserConfig ) if cred, err = LoadCredentials(ctx); err != nil { return } if cred.ConsumerKey == "" || cred.ConsumerSecret == "" { err = fmt.Errorf("Blank consumer secret and/or key") return } config = &oauth1a.ClientConfig{ ConsumerKey: cred.ConsumerKey, ConsumerSecret: cred.ConsumerSecret, } if cred.AccessToken != "" { user = oauth1a.NewAuthorizedConfig(cred.AccessToken, cred.AccessSecret) } c = twittergo.NewClient(config, user) c.HttpClient = urlfetch.Client(ctx) return }
func HandleCreateUserByTwitter(r render.Render, bindErr binding.Errors, userForm models.UserForm, db *models.DB) { if bindErr.Count() > 0 { r.JSON(400, JsonErrBinding(bindErr)) return } // Build Twitter client config := &oauth1a.ClientConfig{ ConsumerKey: TWITTER_CONSUMER_KEY, ConsumerSecret: TWITTER_CONSUMER_SECRET, } oauthUser := oauth1a.NewAuthorizedConfig(userForm.TwitterAccessToken, userForm.TwitterAccessSecret) client := twittergo.NewClient(config, oauthUser) // Build request to send to Twitter req, err := http.NewRequest("GET", "/1.1/account/verify_credentials.json", nil) if err != nil { log.Println("Could not build request for Twitter:", err.Error()) r.JSON(500, JsonErr("Sorry, an internal server error has occurred.")) } // Send it resp, err := client.SendRequest(req) if err != nil { log.Println("Error sending request to Twitter:", err.Error()) r.JSON(504, JsonErr("Could not communicate properly with Twitter, "+ "please try again soon.")) return } // Parse the response var result = &twittergo.User{} if err = resp.Parse(result); err != nil { log.Println("Error parsing Twitter result:", err.Error(), "Response:", resp) r.JSON(504, JsonErr("Got a bad response from Twitter, please try "+ "again soon.")) return } user, err := db.GetUserWithId(result.IdStr()) if err == nil { user, err = db.UpdateUserAuth(user.Id, userForm.TwitterAccessToken, userForm.TwitterAccessSecret) if err != nil { log.Println("Error updating user auth:", err.Error()) r.JSON(500, JsonErr("Sorry, an internal server error has occurred.")) return } r.JSON(200, map[string]*models.User{"user": user}) return } // Now let's create that user, shall we? user, err = db.CreateUser( result.IdStr(), result.ScreenName(), (*result)["profile_image_url"].(string), userForm.TwitterAccessToken, userForm.TwitterAccessSecret, ) if err != nil { log.Println("Error creating user:"******"Sorry, an internal server error has occurred.")) return } r.JSON(200, map[string]*models.User{"user": user}) }
// Run executes the program. func (m *Main) Run() error { logger := log.New(m.Stderr, "", log.LstdFlags) // Validate options. if m.DataDir == "" { return errors.New("data directory required") } // Create base directory, if not exists. if err := os.MkdirAll(m.DataDir, 0777); err != nil { return err } // Open data store. m.store = scuttlebutt.NewStore(filepath.Join(m.DataDir, "db")) m.store.RemoteStore = github.NewStore(m.Config.GitHub.Token) if err := m.store.Open(); err != nil { return fmt.Errorf("open store: %s", err) } // Initialize poller. m.poller = twitter.NewPoller() m.poller.Client = twittergo.NewClient(&oauth1a.ClientConfig{ ConsumerKey: m.Config.Twitter.Key, ConsumerSecret: m.Config.Twitter.Secret, }, nil) // Initialize notifiers for each account for _, acc := range m.Config.Accounts { client := twittergo.NewClient( &oauth1a.ClientConfig{ ConsumerKey: m.Config.Twitter.Key, ConsumerSecret: m.Config.Twitter.Secret, }, oauth1a.NewAuthorizedConfig(acc.Key, acc.Secret), ) n := twitter.NewNotifier() n.Username = acc.Username n.Language = acc.Language n.Client = client m.notifiers = append(m.notifiers, n) } // Open HTTP listener. ln, err := net.Listen("tcp", m.Addr) if err != nil { return err } m.Listener = ln m.Handler = &scuttlebutt.Handler{Store: m.store} // Run HTTP server is separate goroutine. logger.Printf("Listening on http://localhost%s", m.Addr) go http.Serve(m.Listener, m.Handler) // Create a poller & notify monitor. m.wg.Add(2) go m.runPoller() go m.runNotifier() return nil }