| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package main |
| 16 |
|
| 17 |
import ( |
| 18 |
"encoding/json" |
| 19 |
"flag" |
| 20 |
"fmt" |
| 21 |
"io" |
| 22 |
"io/ioutil" |
| 23 |
"log" |
| 24 |
"os" |
| 25 |
|
| 26 |
"github.com/gomodule/oauth1/oauth" |
| 27 |
) |
| 28 |
|
| 29 |
var oauthClient = oauth.Client{ |
| 30 |
TemporaryCredentialRequestURI: "https://api.twitter.com/oauth/request_token", |
| 31 |
ResourceOwnerAuthorizationURI: "https://api.twitter.com/oauth/authorize", |
| 32 |
TokenRequestURI: "https://api.twitter.com/oauth/access_token", |
| 33 |
} |
| 34 |
|
| 35 |
var credPath = flag.String("config", "config.json", "Path to configuration file containing the application's credentials.") |
| 36 |
|
| 37 |
func readCredentials() error { |
| 38 |
b, err := ioutil.ReadFile(*credPath) |
| 39 |
if err != nil { |
| 40 |
return err |
| 41 |
} |
| 42 |
return json.Unmarshal(b, &oauthClient.Credentials) |
| 43 |
} |
| 44 |
|
| 45 |
func main() { |
| 46 |
if err := readCredentials(); err != nil { |
| 47 |
log.Fatal(err) |
| 48 |
} |
| 49 |
|
| 50 |
tempCred, err := oauthClient.RequestTemporaryCredentials(nil, "oob", nil) |
| 51 |
if err != nil { |
| 52 |
log.Fatal("RequestTemporaryCredentials:", err) |
| 53 |
} |
| 54 |
|
| 55 |
u := oauthClient.AuthorizationURL(tempCred, nil) |
| 56 |
|
| 57 |
fmt.Printf("1. Go to %s\n2. Authorize the application\n3. Enter verification code:\n", u) |
| 58 |
|
| 59 |
var code string |
| 60 |
fmt.Scanln(&code) |
| 61 |
|
| 62 |
tokenCred, _, err := oauthClient.RequestToken(nil, tempCred, code) |
| 63 |
if err != nil { |
| 64 |
log.Fatal(err) |
| 65 |
} |
| 66 |
|
| 67 |
resp, err := oauthClient.Get(nil, tokenCred, |
| 68 |
"https://api.twitter.com/1.1/statuses/home_timeline.json", nil) |
| 69 |
if err != nil { |
| 70 |
log.Fatal(err) |
| 71 |
} |
| 72 |
defer resp.Body.Close() |
| 73 |
if _, err := io.Copy(os.Stdout, resp.Body); err != nil { |
| 74 |
log.Fatal(err) |
| 75 |
} |
| 76 |
} |