Skip to content

Instantly share code, notes, and snippets.

@onteria
Created November 4, 2012 14:59
Show Gist options
  • Save onteria/4012198 to your computer and use it in GitHub Desktop.
Save onteria/4012198 to your computer and use it in GitHub Desktop.
Golangで基本のタイムライン構文解析(API 1.1)
package main
// export GOPATH="/hoge/hoge/hoge"
// go get github.com/garyburd/go-oauth/oauth
import (
"github.com/garyburd/go-oauth/oauth"
"net/http"
"net/url"
"fmt"
"encoding/json"
)
var (
oauthClient = oauth.Client{
TemporaryCredentialRequestURI: "https://api.twitter.com/oauth/request_token",
ResourceOwnerAuthorizationURI: "https://api.twitter.com/oauth/authorize",
TokenRequestURI: "https://api.twitter.com/oauth/access_token",
Credentials: oauth.Credentials{
Token: "[CONSUMER KEY]",
Secret: "[CONSUMER SECRET]",
},
}
tokenInfo = oauth.Credentials{
Token: "[TOKEN KEY]",
Secret: "[TOKEN SECRET]",
}
)
type TweetObject struct{
Text string
User UserObject
}
type UserObject struct {
Name string
Screen_Name string
}
func main() {
urlStr := "https://api.twitter.com/1.1/statuses/home_timeline.json"
params := make(url.Values)
oauthClient.SignParam(&tokenInfo, "GET", urlStr, params)
resp, _ := http.Get(urlStr + "?" + params.Encode())
defer resp.Body.Close()
var tweets []TweetObject
json.NewDecoder(resp.Body).Decode(&tweets)
for i := range tweets {
fmt.Printf("%s (%s): %s\n", tweets[i].User.Screen_Name, tweets[i].User.Name, tweets[i].Text)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment