Created
March 20, 2015 04:27
-
-
Save hirokazumiyaji/d7f9c9eee0494f3f1959 to your computer and use it in GitHub Desktop.
Twitter Application Only Authentication
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 | |
import ( | |
"encoding/base64" | |
"encoding/json" | |
"net/http" | |
"net/url" | |
"strconv" | |
"strings" | |
) | |
const Endpoint = "https://api.twitter.com" | |
type Token struct { | |
TokenType string `json:"token_type"` | |
AccessToken string `json:"access_token"` | |
} | |
func (t *Token) String() string { | |
return base64.StdEncoding.EncodeToString([]byte(t.TokenType + " " + t.AccessToken)) | |
} | |
type UserTimelineOptions struct { | |
UserId int | |
ScreenName string | |
SinceId int | |
Count int | |
MaxId int | |
TrimUser bool | |
ExcludeReplies bool | |
ContributorDetails bool | |
IncludeRts bool | |
} | |
func (o UserTimelineOptions) Values() url.Values { | |
values := url.Values{} | |
if options.UserId != 0 { | |
values.Add("user_id", strconv.Itoa(options.UserId)) | |
} | |
if options.ScreenName != 0 { | |
values.Add("screen_name", strconv.Itoa(options.ScreenName)) | |
} | |
if options.SinceId != 0 { | |
values.Add("since_id", strconv.Itoa(options.SinceId)) | |
} | |
if options.Count != 0 { | |
values.Add("count", strconv.Itoa(options.Count)) | |
} | |
if options.MaxId != 0 { | |
values.Add("max_id", strconv.Itoa(options.MaxId)) | |
} | |
if options.TrimUser != true { | |
values.Add("trim_user", strconv.FormatBool(options.TrimUser)) | |
} | |
if options.ExcludeReplies != true { | |
values.Add("exclude_replies", strconv.FormatBool(options.ExcludeReplies)) | |
} | |
if options.ContributorDetails != true { | |
values.Add("contributor_details", strconv.FormatBool(options.ContributorDetails)) | |
} | |
if options.IncludeRts != false { | |
values.Add("include_rts", strconv.FormatBool(options.IncludeRts)) | |
} | |
return values | |
} | |
type Tweet struct { | |
} | |
func GetToken(consumerKey, consumerSecret string) (*Token, error) { | |
basicToken := "Basic " + base64.StdEncoding.EncodeToString([]byte(consumerKey + ":" consumerSecret)) | |
values := url.Values{} | |
values.Add("grant_type", "client_credentials") | |
req, err := http.NewRequest("POST", Endpoint+"/oauth2/token", strings.NewReader(values.Encode())) | |
if err != nil { | |
return nil, err | |
} | |
req.Header.Add("Authorization", basicToken) | |
req.Header.Add("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8") | |
client := http.Client{} | |
res, err := client.Do(req) | |
if err != nil { | |
return nil, err | |
} | |
defer res.Body.Close() | |
var token Token | |
if err = json.NewDecoder(res.Body).Decode(&token); err != nil { | |
return nil, err | |
} | |
return &token, nil | |
} | |
func GetUserTimeline(token Token, options UserTimelineOptions) ([]Tweet, error) { | |
values := options.Values() | |
req, err := http.NewRequest("GET", Endpoint+"/1.1/statuses/user_timeline.json", nil) | |
if err != nil { | |
return nil, err | |
} | |
req,Header.Add("Authorization", token.String()) | |
req.Header.Add("Content-Type", "application/json; charset=utf-8") | |
req.URL.RawQuery = values.Encode() | |
client := http.Client{} | |
res, err := client.Do(req) | |
if err != nil { | |
return nil, err | |
} | |
defer res.Body.Close() | |
var timeline []Tweet | |
if err = json.NewDecoder(res.Body).Decode(timeline); err != nil { | |
return nil, err | |
} | |
return timeline, nil | |
} | |
func main() { | |
var consumerKey, consumerSecret string | |
token := GetToken(consumerKey, consumerSecret) | |
timeline := GetUserTimeline(token, UserTimelineOptions{}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment