forked from strava/go.strava
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth_example.go
104 lines (86 loc) · 3.68 KB
/
oauth_example.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// oauth_example.go provides a simple example implementing Strava OAuth
// using the go.strava library.
//
// usage:
// > go get github.com/strava/go.strava
// > cd $GOPATH/github.com/strava/go.strava/examples
// > go run oauth_example.go -id=youappsid -secret=yourappsecret
//
// Visit http://localhost:8080 in your webbrowser
//
// Application id and secret can be found at https://www.strava.com/settings/api
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"github.com/strava/go.strava"
)
const port = 8080 // port of local demo server
var authenticator *strava.OAuthAuthenticator
func main() {
// setup the credentials for your app
// These need to be set to reflect your application
// and can be found at https://www.strava.com/settings/api
flag.IntVar(&strava.ClientId, "id", 0, "Strava Client ID")
flag.StringVar(&strava.ClientSecret, "secret", "", "Strava Client Secret")
flag.Parse()
if strava.ClientId == 0 || strava.ClientSecret == "" {
fmt.Println("\nPlease provide your application's client_id and client_secret.")
fmt.Println("For example: go run oauth_example.go -id=9 -secret=longrandomsecret\n")
flag.PrintDefaults()
os.Exit(1)
}
// define a strava.OAuthAuthenticator to hold state.
// The callback url is used to generate an AuthorizationURL.
// The RequestClientGenerator can be used to generate an http.RequestClient.
// This is usually when running on the Google App Engine platform.
authenticator = &strava.OAuthAuthenticator{
CallbackURL: fmt.Sprintf("http://localhost:%d/exchange_token", port),
RequestClientGenerator: nil,
}
http.HandleFunc("/", indexHandler)
path, err := authenticator.CallbackPath()
if err != nil {
// possibly that the callback url set above is invalid
fmt.Println(err)
os.Exit(1)
}
http.HandleFunc(path, authenticator.HandlerFunc(oAuthSuccess, oAuthFailure))
// start the server
fmt.Printf("Visit http://localhost:%d/ to view the demo\n", port)
fmt.Printf("ctrl-c to exit")
http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
// you should make this a template in your real application
fmt.Fprintf(w, `<a href="%s">`, authenticator.AuthorizationURL("state1", strava.Permissions.Public, true))
fmt.Fprint(w, `<img src="http://strava.github.io/api/images/ConnectWithStrava.png" />`)
fmt.Fprint(w, `</a>`)
}
func oAuthSuccess(auth *strava.AuthorizationResponse, w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "SUCCESS:\nAt this point you can use this information to create a new user or link the account to one of your existing users\n")
fmt.Fprintf(w, "State: %s\n\n", auth.State)
fmt.Fprintf(w, "Access Token: %s\n\n", auth.AccessToken)
fmt.Fprintf(w, "The Authenticated Athlete (you):\n")
content, _ := json.MarshalIndent(auth.Athlete, "", " ")
fmt.Fprint(w, string(content))
}
func oAuthFailure(err error, w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Authorization Failure:\n")
// some standard error checking
if err == strava.OAuthAuthorizationDeniedErr {
fmt.Fprint(w, "The user clicked the 'Do not Authorize' button on the previous page.\n")
fmt.Fprint(w, "This is the main error your application should handle.")
} else if err == strava.OAuthInvalidCredentialsErr {
fmt.Fprint(w, "You provided an incorrect client_id or client_secret.\nDid you remember to set them at the begininng of this file?")
} else if err == strava.OAuthInvalidCodeErr {
fmt.Fprint(w, "The temporary token was not recognized, this shouldn't happen normally")
} else if err == strava.OAuthServerErr {
fmt.Fprint(w, "There was some sort of server error, try again to see if the problem continues")
} else {
fmt.Fprint(w, err)
}
}