Debian Codegraph @

examples/twitteroob/main.go

raw symbols SCIP
1 // Copyright 2013 Gary Burd
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"): you may
4 // not use this file except in compliance with the License. You may obtain
5 // a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations
13 // under the License.
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 }