Skip to content
This repository was archived by the owner on Nov 7, 2022. It is now read-only.

Commit dc9b508

Browse files
committed
Improve AppAuth example in README, godoc, and examples
1 parent 5c87ef6 commit dc9b508

6 files changed

Lines changed: 67 additions & 28 deletions

File tree

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,17 @@ import (
247247
"golang.org/x/oauth2/clientcredentials"
248248
)
249249

250+
// oauth2 configures a client that uses app credentials to keep a fresh token
250251
config := &clientcredentials.Config{
251-
ClientID: consumerKey,
252-
ClientSecret: consumerSecret,
252+
ClientID: "consumerKey",
253+
ClientSecret: "consumerSecret",
253254
TokenURL: "https://api.twitter.com/oauth2/token",
254255
}
256+
// http.Client will automatically authorize Requests
255257
httpClient := config.Client(oauth2.NoContext)
256-
twClient := twitter.NewClient(httpClient)
258+
259+
// Twitter client
260+
client := twitter.NewClient(httpClient)
257261
```
258262

259263
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).

examples/README.md

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,42 @@ Get the dependencies and examples
1111
A user access token (OAuth1) grants a consumer application access to a user's Twitter resources.
1212

1313
Setup an OAuth1 `http.Client` with the consumer key and secret and oauth token and secret.
14-
15-
export TWITTER_CONSUMER_KEY=xxx
16-
export TWITTER_CONSUMER_SECRET=xxx
17-
export TWITTER_ACCESS_TOKEN=xxx
18-
export TWITTER_ACCESS_SECRET=xxx
14+
```
15+
export TWITTER_CONSUMER_KEY=xxx
16+
export TWITTER_CONSUMER_SECRET=xxx
17+
export TWITTER_ACCESS_TOKEN=xxx
18+
export TWITTER_ACCESS_SECRET=xxx
19+
```
1920

2021
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).
2122

22-
go run user-auth.go
23+
```
24+
go run user-auth.go
25+
```
2326

2427
## App Auth (OAuth2)
2528

2629
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.
2730

28-
Setup an OAuth2 `http.Client` with the Twitter application access token.
31+
Setup an OAuth2 `http.Client` with the Twitter consumer key and secret.
2932

30-
export TWITTER_APP_ACCESS_TOKEN=xxx
33+
```
34+
export TWITTER_CONSUMER_KEY=xxx
35+
export TWITTER_CONSUMER_SECRET=xxx
36+
```
3137

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

34-
go run app-auth.go
40+
```
41+
go run app-auth.go
42+
```
3543

3644
## Streaming API
3745

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

40-
go run streaming.go
48+
```
49+
go run streaming.go
50+
```
4151

42-
Hit CTRL-C to stop streaming. Uncomment different examples in code to try different streams.
52+
Hit CTRL-C to stop streaming. Uncomment different examples in code to try different streams.

examples/app-auth.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,36 @@ import (
44
"flag"
55
"fmt"
66
"log"
7-
"os"
87

98
"github.com/coreos/pkg/flagutil"
109
"github.com/dghubble/go-twitter/twitter"
1110
"golang.org/x/oauth2"
11+
"golang.org/x/oauth2/clientcredentials"
1212
)
1313

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

20-
if *accessToken == "" {
20+
flag.StringVar(&flags.consumerKey, "consumer-key", "", "Twitter Consumer Key")
21+
flag.StringVar(&flags.consumerSecret, "consumer-secret", "", "Twitter Consumer Secret")
22+
flag.Parse()
23+
flagutil.SetFlagsFromEnv(flag.CommandLine, "TWITTER")
24+
25+
if flags.consumerKey == "" || flags.consumerSecret == "" {
2126
log.Fatal("Application Access Token required")
2227
}
2328

24-
config := &oauth2.Config{}
25-
token := &oauth2.Token{AccessToken: *accessToken}
26-
// OAuth2 http.Client will automatically authorize Requests
27-
httpClient := config.Client(oauth2.NoContext, token)
29+
// oauth2 configures a client that uses app credentials to keep a fresh token
30+
config := &clientcredentials.Config{
31+
ClientID: flags.consumerKey,
32+
ClientSecret: flags.consumerSecret,
33+
TokenURL: "https://api.twitter.com/oauth2/token",
34+
}
35+
// http.Client will automatically authorize Requests
36+
httpClient := config.Client(oauth2.NoContext)
2837

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

examples/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ require (
88
github.com/dghubble/go-twitter v0.0.0-20190512073027-53f972dc4b06 // indirect
99
github.com/dghubble/oauth1 v0.6.0 // indirect
1010
github.com/dghubble/sling v1.3.0 // indirect
11+
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 // indirect
1112
)

examples/go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
12
github.com/cenkalti/backoff v2.1.1+incompatible h1:tKJnvO2kl0zmb/jA5UKAt4VoEVw1qxKWjE/Bpp46npY=
23
github.com/cenkalti/backoff v2.1.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
34
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f h1:lBNOc5arjvs8E5mO2tbpBpLoyyu8B6e44T7hJy6potg=
@@ -9,8 +10,17 @@ github.com/dghubble/oauth1 v0.6.0 h1:m1yC01Ohc/eF38jwZ8JUjL1a+XHHXtGQgK+MxQbmSx0
910
github.com/dghubble/oauth1 v0.6.0/go.mod h1:8pFdfPkv/jr8mkChVbNVuJ0suiHe278BtWI4Tk1ujxk=
1011
github.com/dghubble/sling v1.3.0 h1:pZHjCJq4zJvc6qVQ5wN1jo5oNZlNE0+8T/h0XeXBUKU=
1112
github.com/dghubble/sling v1.3.0/go.mod h1:XXShWaBWKzNLhu2OxikSNFrlsvowtz4kyRuXUG7oQKY=
13+
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
1214
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
1315
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
1416
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1517
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1618
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
19+
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
20+
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e h1:bRhVy7zSSasaqNksaRZiA5EEI+Ei4I1nO5Jh72wfHlg=
21+
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
22+
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0=
23+
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
24+
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
25+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
26+
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=

twitter/doc.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,19 @@ application auth.
5454
import (
5555
"github.com/dghubble/go-twitter/twitter"
5656
"golang.org/x/oauth2"
57+
"golang.org/x/oauth2/clientcredentials"
5758
)
5859
59-
config := &oauth2.Config{}
60-
token := &oauth2.Token{AccessToken: accessToken}
60+
// oauth2 configures a client that uses app credentials to keep a fresh token
61+
config := &clientcredentials.Config{
62+
ClientID: flags.consumerKey,
63+
ClientSecret: flags.consumerSecret,
64+
TokenURL: "https://api.twitter.com/oauth2/token",
65+
}
6166
// http.Client will automatically authorize Requests
62-
httpClient := config.Client(oauth2.NoContext, token)
67+
httpClient := config.Client(oauth2.NoContext)
6368
64-
// twitter client
69+
// Twitter client
6570
client := twitter.NewClient(httpClient)
6671
6772
To implement Login with Twitter, see https://github.com/dghubble/gologin.

0 commit comments

Comments
 (0)