Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
28419fc
Update .travis.yml for Go 1.9 and 1.10
dghubble May 17, 2018
e2436e3
Fix typo in documentation
jasonodonnell Oct 29, 2017
093ee2c
Add CreatedAtTime helper function for DM struct
rjchee Jan 27, 2018
036afe2
Fix verbose CreatedAtTime description
dghubble May 24, 2018
306613e
Add new Direct Message events list API
listonb May 26, 2018
fadf083
Refactor DirectMessage event structs and markers
xeoncross May 26, 2018
1006667
Fix DirectMessage EventsList and deprecate other methods
dghubble Jul 9, 2018
b77318b
Allow test failures on Go tip
dghubble Jul 9, 2018
9b315f8
Add attachment field to DirectMessageEvent message data
xeoncross Jul 28, 2018
a34e905
Remove DirectMessageEvents broken Apps field
dghubble Aug 17, 2018
e75339c
Switch to the golang.org/x/lint/golint import path
dghubble Oct 13, 2018
d707827
Use high-resolution SVG badges
glaslos Aug 31, 2018
62706c8
Update .travis.yml for Go v1.11, remove v1.8
dghubble Nov 18, 2018
856367c
Show Travis status of the master branch only
dghubble Nov 18, 2018
2277311
Fix stream testing for Go v1.11
dghubble Nov 18, 2018
186ef5c
Add `DirectMessageService` support for Twitter Direct Message Events API
dghubble Nov 26, 2018
0451b63
Add a CHANGES changelog
dghubble Nov 26, 2018
3b766e9
Limit .travis.yml email notifications
dghubble Nov 26, 2018
66265ea
Replace test script with a make target
dghubble Nov 26, 2018
22c4b7b
Add support for rate_limit_status endpoint (#124)
ltoussaint Nov 30, 2018
7ecc41c
Add CTAs and QuickReply fields to DirectMessageData (#128)
torufurukawa Dec 18, 2018
7fd79e2
Add QuoteCount and RetweetCount to Tweet (#132)
lexiross Jan 8, 2019
a06c8f4
Add ListsService
peppage Jun 11, 2018
18415e9
Fix create_all and destroy_all parameter
Mushus Jun 24, 2018
75855c6
Fix Cursor type to int64
peppage Jul 12, 2018
895511a
Add Lists the the README REST API list
dghubble Mar 5, 2019
0022a70
Add Go v1.12 and remove Go v1.9
dghubble Mar 5, 2019
53f972d
Add Since and Filter to SearchTweetParams (#141)
HugoJBello May 12, 2019
f4d0de0
Add a go.mod file
dghubble Jul 11, 2019
f8d43cc
Support Premium Search API
tanishiking Apr 19, 2019
5c87ef6
Change AppAuth example to use oauth2/clientcredentials
heavyengineer May 15, 2019
dc9b508
Improve AppAuth example in README, godoc, and examples
dghubble Jul 11, 2019
39e5462
Update links to twitter docs (#147)
Jul 19, 2019
e14e683
Add support for stream errors to message channel (#2)
chilland May 24, 2018
6f2360d
Add ExpectedStop() to Stream
JoshuaC215 Feb 6, 2019
aaa5afd
Expose more test scaffolding for use in implementing libraries
JoshuaC215 Feb 7, 2019
d3a485a
Add stream mutex to prevent data race
JoshuaC215 Feb 7, 2019
96b03ed
Better race fix
JoshuaC215 Feb 7, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
language: go
go:
- 1.6
- 1.7
- 1.8
- tip
- "1.10.x"
- "1.11.x"
- "1.12.x"
install:
- go get github.com/golang/lint/golint
- go get golang.org/x/lint/golint
- go get -v -t ./twitter
script:
- ./test
- make
notifications:
email: change
16 changes: 16 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# go-twitter Changelog

Notable changes over time. Note, `go-twitter` does not follow a semver release cycle since it may change whenever the Twitter API changes (external).

## 07/2019

* Add Go module support ([#143](https://github.com/dghubble/go-twitter/pull/143))

## 11/2018

* Add `DirectMessageService` support for the new Twitter Direct Message Events [API](https://developer.twitter.com/en/docs/direct-messages/api-features) ([#125](https://github.com/dghubble/go-twitter/pull/125))
* Add `EventsNew` method for sending a Direct Message event
* Add `EventsShow` method for getting a single Direct Message event
* Add `EventsList` method for listing recent Direct Message events
* Add`EventsDestroy` method for destroying a Direct Message event

19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.PHONY: all
all: test vet lint fmt

.PHONY: test
test:
@go test ./twitter -cover

.PHONY: vet
vet:
@go vet -all ./twitter

.PHONY: lint
lint:
@golint -set_exit_status ./...

.PHONY: fmt
fmt:
@test -z $$(go fmt ./...)

22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@


# go-twitter [![Build Status](https://travis-ci.org/dghubble/go-twitter.png)](https://travis-ci.org/dghubble/go-twitter) [![GoDoc](https://godoc.org/github.com/dghubble/go-twitter?status.png)](https://godoc.org/github.com/dghubble/go-twitter)
# go-twitter [![Build Status](https://travis-ci.org/dghubble/go-twitter.svg?branch=master)](https://travis-ci.org/dghubble/go-twitter) [![GoDoc](https://godoc.org/github.com/dghubble/go-twitter?status.svg)](https://godoc.org/github.com/dghubble/go-twitter)
<img align="right" src="https://storage.googleapis.com/dghubble/gopher-on-bird.png">

go-twitter is a Go client library for the [Twitter API](https://dev.twitter.com/rest/public). Check the [usage](#usage) section or try the [examples](/examples) to see how to access the Twitter API.
Expand All @@ -9,11 +7,14 @@ go-twitter is a Go client library for the [Twitter API](https://dev.twitter.com/

* Twitter REST API:
* Accounts
* Direct Messages
* DirectMessageEvents
* Favorites
* Friends
* Friendships
* Followers
* Lists
* PremiumSearch
* RateLimits
* Search
* Statuses
* Timelines
Expand Down Expand Up @@ -243,15 +244,20 @@ If no user auth context is needed, make requests as your application with applic
import (
"github.com/dghubble/go-twitter/twitter"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)

config := &oauth2.Config{}
token := &oauth2.Token{AccessToken: accessToken}
// oauth2 configures a client that uses app credentials to keep a fresh token
config := &clientcredentials.Config{
ClientID: "consumerKey",
ClientSecret: "consumerSecret",
TokenURL: "https://api.twitter.com/oauth2/token",
}
// http.Client will automatically authorize Requests
httpClient := config.Client(oauth2.NoContext, token)
httpClient := config.Client(oauth2.NoContext)

// Twitter client
client := twitter.NewClient(httpClient)
client := twitter.NewClient(httpClient)
```

To implement Login with Twitter for web or mobile, see the gologin [package](https://github.com/dghubble/gologin) and [examples](https://github.com/dghubble/gologin/tree/master/examples/twitter).
Expand Down
32 changes: 21 additions & 11 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,42 @@ Get the dependencies and examples
A user access token (OAuth1) grants a consumer application access to a user's Twitter resources.

Setup an OAuth1 `http.Client` with the consumer key and secret and oauth token and secret.

export TWITTER_CONSUMER_KEY=xxx
export TWITTER_CONSUMER_SECRET=xxx
export TWITTER_ACCESS_TOKEN=xxx
export TWITTER_ACCESS_SECRET=xxx
```
export TWITTER_CONSUMER_KEY=xxx
export TWITTER_CONSUMER_SECRET=xxx
export TWITTER_ACCESS_TOKEN=xxx
export TWITTER_ACCESS_SECRET=xxx
```

To make requests as an application, on behalf of a user, create a `twitter` `Client` to get the home timeline, mention timeline, and more (example will **not** post Tweets).

go run user-auth.go
```
go run user-auth.go
```

## App Auth (OAuth2)

An application access token (OAuth2) allows an application to make Twitter API requests for public content, with rate limits counting against the app itself. App auth requests can be made to API endpoints which do not require a user context.

Setup an OAuth2 `http.Client` with the Twitter application access token.
Setup an OAuth2 `http.Client` with the Twitter consumer key and secret.

export TWITTER_APP_ACCESS_TOKEN=xxx
```
export TWITTER_CONSUMER_KEY=xxx
export TWITTER_CONSUMER_SECRET=xxx
```

To make requests as an application, create a `twitter` `Client` and get public Tweets or timelines or other public content.

go run app-auth.go
```
go run app-auth.go
```

## Streaming API

A user access token (OAuth1) is required for Streaming API requests. See above.

go run streaming.go
```
go run streaming.go
```

Hit CTRL-C to stop streaming. Uncomment different examples in code to try different streams.
Hit CTRL-C to stop streaming. Uncomment different examples in code to try different streams.
29 changes: 19 additions & 10 deletions examples/app-auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,36 @@ import (
"flag"
"fmt"
"log"
"os"

"github.com/coreos/pkg/flagutil"
"github.com/dghubble/go-twitter/twitter"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)

func main() {
flags := flag.NewFlagSet("app-auth", flag.ExitOnError)
accessToken := flags.String("app-access-token", "", "Twitter Application Access Token")
flags.Parse(os.Args[1:])
flagutil.SetFlagsFromEnv(flags, "TWITTER")
flags := struct {
consumerKey string
consumerSecret string
}{}

if *accessToken == "" {
flag.StringVar(&flags.consumerKey, "consumer-key", "", "Twitter Consumer Key")
flag.StringVar(&flags.consumerSecret, "consumer-secret", "", "Twitter Consumer Secret")
flag.Parse()
flagutil.SetFlagsFromEnv(flag.CommandLine, "TWITTER")

if flags.consumerKey == "" || flags.consumerSecret == "" {
log.Fatal("Application Access Token required")
}

config := &oauth2.Config{}
token := &oauth2.Token{AccessToken: *accessToken}
// OAuth2 http.Client will automatically authorize Requests
httpClient := config.Client(oauth2.NoContext, token)
// oauth2 configures a client that uses app credentials to keep a fresh token
config := &clientcredentials.Config{
ClientID: flags.consumerKey,
ClientSecret: flags.consumerSecret,
TokenURL: "https://api.twitter.com/oauth2/token",
}
// http.Client will automatically authorize Requests
httpClient := config.Client(oauth2.NoContext)

// Twitter client
client := twitter.NewClient(httpClient)
Expand Down
74 changes: 74 additions & 0 deletions examples/direct_messages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package main

import (
"flag"
"fmt"
"log"
"os"

"github.com/coreos/pkg/flagutil"
"github.com/dghubble/go-twitter/twitter"
"github.com/dghubble/oauth1"
)

func main() {
flags := flag.NewFlagSet("user-auth", flag.ExitOnError)
consumerKey := flags.String("consumer-key", "", "Twitter Consumer Key")
consumerSecret := flags.String("consumer-secret", "", "Twitter Consumer Secret")
accessToken := flags.String("access-token", "", "Twitter Access Token")
accessSecret := flags.String("access-secret", "", "Twitter Access Secret")
flags.Parse(os.Args[1:])
flagutil.SetFlagsFromEnv(flags, "TWITTER")

if *consumerKey == "" || *consumerSecret == "" || *accessToken == "" || *accessSecret == "" {
log.Fatal("Consumer key/secret and Access token/secret required")
}

config := oauth1.NewConfig(*consumerKey, *consumerSecret)
token := oauth1.NewToken(*accessToken, *accessSecret)
// OAuth1 http.Client will automatically authorize Requests
httpClient := config.Client(oauth1.NoContext, token)

// Twitter client
client := twitter.NewClient(httpClient)

// List most recent 10 Direct Messages
messages, _, err := client.DirectMessages.EventsList(
&twitter.DirectMessageEventsListParams{Count: 10},
)
fmt.Println("User's DIRECT MESSAGES:")
if err != nil {
log.Fatal(err)
}
for _, event := range messages.Events {
fmt.Printf("%+v\n", event)
fmt.Printf(" %+v\n", event.Message)
fmt.Printf(" %+v\n", event.Message.Data)
}

// Show Direct Message event
event, _, err := client.DirectMessages.EventsShow("1066903366071017476", nil)
fmt.Printf("DM Events Show:\n%+v, %v\n", event.Message.Data, err)

// Create Direct Message event
/*
event, _, err = client.DirectMessages.EventsNew(&twitter.DirectMessageEventsNewParams{
Event: &twitter.DirectMessageEvent{
Type: "message_create",
Message: &twitter.DirectMessageEventMessage{
Target: &twitter.DirectMessageTarget{
RecipientID: "2856535627",
},
Data: &twitter.DirectMessageData{
Text: "testing",
},
},
},
})
fmt.Printf("DM Event New:\n%+v, %v\n", event, err)
*/

// Destroy Direct Message event
//_, err = client.DirectMessages.EventsDestroy("1066904217049133060")
//fmt.Printf("DM Events Delete:\n err: %v\n", err)
}
12 changes: 12 additions & 0 deletions examples/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module github.com/dghubble/go-twitter/examples

go 1.12

require (
github.com/cenkalti/backoff v2.1.1+incompatible // indirect
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
github.com/dghubble/go-twitter v0.0.0-20190512073027-53f972dc4b06 // indirect
github.com/dghubble/oauth1 v0.6.0 // indirect
github.com/dghubble/sling v1.3.0 // indirect
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 // indirect
)
26 changes: 26 additions & 0 deletions examples/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/cenkalti/backoff v2.1.1+incompatible h1:tKJnvO2kl0zmb/jA5UKAt4VoEVw1qxKWjE/Bpp46npY=
github.com/cenkalti/backoff v2.1.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f h1:lBNOc5arjvs8E5mO2tbpBpLoyyu8B6e44T7hJy6potg=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dghubble/go-twitter v0.0.0-20190512073027-53f972dc4b06 h1:eg2cM+xR5Bgm4hgJ5xmtbOkgDAJSOXP4WGr2hEFdqe4=
github.com/dghubble/go-twitter v0.0.0-20190512073027-53f972dc4b06/go.mod h1:6beqTZaXeBPti9pDBcBEqxfJc7uCbSafqZPRDPQOKoM=
github.com/dghubble/oauth1 v0.6.0 h1:m1yC01Ohc/eF38jwZ8JUjL1a+XHHXtGQgK+MxQbmSx0=
github.com/dghubble/oauth1 v0.6.0/go.mod h1:8pFdfPkv/jr8mkChVbNVuJ0suiHe278BtWI4Tk1ujxk=
github.com/dghubble/sling v1.3.0 h1:pZHjCJq4zJvc6qVQ5wN1jo5oNZlNE0+8T/h0XeXBUKU=
github.com/dghubble/sling v1.3.0/go.mod h1:XXShWaBWKzNLhu2OxikSNFrlsvowtz4kyRuXUG7oQKY=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e h1:bRhVy7zSSasaqNksaRZiA5EEI+Ei4I1nO5Jh72wfHlg=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/dghubble/go-twitter

go 1.12

require (
github.com/cenkalti/backoff v2.1.1+incompatible
github.com/dghubble/sling v1.3.0
github.com/stretchr/testify v1.3.0
)
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
github.com/cenkalti/backoff v2.1.1+incompatible h1:tKJnvO2kl0zmb/jA5UKAt4VoEVw1qxKWjE/Bpp46npY=
github.com/cenkalti/backoff v2.1.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dghubble/sling v1.3.0 h1:pZHjCJq4zJvc6qVQ5wN1jo5oNZlNE0+8T/h0XeXBUKU=
github.com/dghubble/sling v1.3.0/go.mod h1:XXShWaBWKzNLhu2OxikSNFrlsvowtz4kyRuXUG7oQKY=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
23 changes: 0 additions & 23 deletions test

This file was deleted.

4 changes: 4 additions & 0 deletions twitter/backoffs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ func newAggressiveExponentialBackOff() *backoff.ExponentialBackOff {
b.Reset()
return b
}

func newBackoffWithMaxRetries(b backoff.BackOff, maxRetries uint64) backoff.BackOff {
return backoff.WithMaxRetries(b, maxRetries)
}
Loading