Created
March 13, 2013 16:15
-
-
Save julianshen/5153693 to your computer and use it in GitHub Desktop.
This file contains 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 | |
import ( | |
"encoding/json" | |
"errors" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"net/url" | |
"os" | |
"strings" | |
) | |
var consumerKey string | |
var consumerSecret string | |
type AccessToken struct { | |
Access_Token string | |
Token_Type string | |
} | |
func initApp() { | |
consumerKey = os.Getenv("TWITTER_CONSUMERKEY") | |
consumerSecret = os.Getenv("TWITTER_CONSUMERSECRET") | |
} | |
func getSavedAccessToken() string { | |
file, err := os.Open(".twitter") | |
if err == nil { | |
defer file.Close() | |
bytes, err := ioutil.ReadAll(file) | |
if err == nil { | |
line := string(bytes) | |
return line | |
} | |
} | |
return "" | |
} | |
func authApp() (string, error) { | |
const authUrl = "https://api.twitter.com/oauth2/token" | |
var accessToken string | |
accessToken = "" | |
if len(consumerKey) == 0 || len(consumerSecret) == 0 { | |
return "", errors.New("You must set the TWITTER_CONSUMERKEY and TWITTER_CONSUMERSECRET.") | |
} | |
client := &http.Client{} | |
values := url.Values{"grant_type": {"client_credentials"}} | |
req, err := http.NewRequest("POST", authUrl, strings.NewReader(values.Encode())) | |
if err == nil { | |
req.SetBasicAuth(consumerKey, consumerSecret) | |
resp, err := client.Do(req) | |
if err == nil { | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
if err == nil { | |
token := new(AccessToken) | |
decoder := json.NewDecoder(strings.NewReader(string(body))) | |
decoder.Decode(token) | |
accessToken = token.Access_Token | |
} | |
} | |
} | |
return accessToken, err | |
} | |
func Get(url string, values url.Values, token string) (string, error) { | |
var result string | |
client := &http.Client{} | |
if values != nil { | |
url = url + "?" + values.Encode() | |
} | |
req, err := http.NewRequest("GET", url, nil) | |
req.Header.Set("Authorization", "Bearer "+token) | |
resp, err := client.Do(req) | |
if err == nil { | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
if err == nil { | |
result = string(body) | |
} | |
} | |
return result, err | |
} | |
func main() { | |
initApp() | |
var accessToken string | |
accessToken = getSavedAccessToken() | |
if accessToken == "" { | |
var err error | |
accessToken, err = authApp() | |
//Save token | |
if err == nil { | |
file, err := os.Create(".twitter") | |
if err == nil { | |
defer file.Close() | |
file.WriteString(accessToken) | |
} | |
} else { | |
fmt.Println(err) | |
os.Exit(0) | |
} | |
} | |
if accessToken != "" { | |
result, err := Get("https://api.twitter.com/1.1/search/tweets.json", url.Values{"q": {"http://politicalticker.blogs.cnn.com/2013/03/11/desperate-times-marines-told-to-save-every-round/?on.cnn=1"}}, accessToken) | |
if err == nil { | |
fmt.Println(result) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment