~vtuson/scopecreator/twitter-template

« back to all changes in this revision

Viewing changes to src/go/src/github.com/garyburd/go-oauth/examples/twitter/oobmain.go

  • Committer: Victor Palau
  • Date: 2015-03-11 14:24:42 UTC
  • Revision ID: vtuson@gmail.com-20150311142442-f2pxp111c8ynv232
public release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        "github.com/garyburd/go-oauth/oauth"
 
22
        "io"
 
23
        "io/ioutil"
 
24
        "log"
 
25
        "net/http"
 
26
        "os"
 
27
)
 
28
 
 
29
var oauthClient = oauth.Client{
 
30
        TemporaryCredentialRequestURI: "http://api.twitter.com/oauth/request_token",
 
31
        ResourceOwnerAuthorizationURI: "http://api.twitter.com/oauth/authorize",
 
32
        TokenRequestURI:               "http://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(http.DefaultClient, "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(http.DefaultClient, tempCred, code)
 
63
        if err != nil {
 
64
                log.Fatal(err)
 
65
        }
 
66
 
 
67
        resp, err := oauthClient.Get(http.DefaultClient, tokenCred,
 
68
                "http://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
}