Help us understand the problem. What is going on with this article?

GoでtwitterAPI触ってみた

Goの勉強も含めて、Twitter APIを利用して、「アオキ大好き!最高!」とツイートしている人を全員フォローしてみせます。

anacondaというパッケージを使います。

anacondaとは

Pythonのあれじゃないです。

anacondaとは、Twitter APIにアクセスするためのGoパッケージです。便利!

Twitter APIの利用申請

間違いなく一番めんどくさい過程。
こちらを見ながら頑張りましょう。結構テキトーでも通ります。

実装

main.go
package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/url"
    "strconv"

    "github.com/ChimeraCoder/anaconda"
)

type TwitterAccount struct {
    AccessToken       string `json:"accessToken"`
    AccessTokenSecret string `json:"accessTokenSecret"`
    ConsumerKey       string `json:"consumerKey"`
    ConsumerSecret    string `json:"consumerSecret"`
}

func main() {
    raw, error := ioutil.ReadFile("./twitterAccount.json")
    if error != nil {
        fmt.Println(error.Error())
        return
    }

    var twitterAccount TwitterAccount
    json.Unmarshal(raw, &twitterAccount)

    api := anaconda.NewTwitterApiWithCredentials(twitterAccount.AccessToken, twitterAccount.AccessTokenSecret, twitterAccount.ConsumerKey, twitterAccount.ConsumerSecret)
    v := url.Values{}
    v.Set("count","10000")
    // v.Set("exclude","retweets")
    searchResult, _ := api.GetSearch(`"アオキ大好き!最高!"`, v)
    fmt.Println(strconv.Itoa(len(searchResult.Statuses))+"件ヒットしました!!")
    for _, tweet := range searchResult.Statuses {
        fmt.Printf("%d\n", tweet.User.Id)
        api.FollowUserId(tweet.User.Id,v)
        fmt.Println("--------------------------------------------------------------")
    }
}

twitterAccount.json
{
  "accessToken": "*****************",
  "accessTokenSecret": "*****************",
  "consumerKey": "*****************",
  "consumerSecret": "*****************"
}
結果
   0件ヒットしました!!

今回作成したリポジトリはこちらから

参考:
anaconda github
anaconda Doc

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした