7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Go言語でTwitterAPIを利用する(anacondaの利用)

Posted at

前提

anacondaをインストール

$ go get github.com/ChimeraCoder/anaconda

Twitterで「golang」で検索した結果を取得してみる

search.go
package main

import (
	"fmt"
	"log"
	"net/url"
	"os"

	"github.com/ChimeraCoder/anaconda"
	"github.com/joho/godotenv"
)

func loadEnv() {
	err := godotenv.Load()
	if err != nil {
		log.Fatal("Error loading .env file")
	}
}

func getTwitterApi() *anaconda.TwitterApi {
	anaconda.SetConsumerKey(os.Getenv("CONSUMER_KEY"))
	anaconda.SetConsumerSecret(os.Getenv("CONSUMER_SECRET"))
	return anaconda.NewTwitterApi(os.Getenv("ACCESS_TOKEN"), os.Getenv("ACCESS_TOKEN_SECRET"))
}

func main() {
	loadEnv()

	api := getTwitterApi()

	v := url.Values{}
	v.Set("count", "30")

	searchResult, _ := api.GetSearch("golang", v)
	for _, tweet := range searchResult.Statuses {
		fmt.Println(tweet.Text)
	}
}

終わりに

記事では検索しかしませんが、ドキュメント読むといろいろなことができる模様。

・ドキュメント
https://godoc.org/github.com/ChimeraCoder/anaconda

7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?