Created
November 4, 2012 14:59
-
-
Save onteria/4012198 to your computer and use it in GitHub Desktop.
Golangで基本のタイムライン構文解析(API 1.1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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