-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.go
122 lines (101 loc) · 3.48 KB
/
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"context"
"encoding/json"
"fmt"
"html"
"log"
"net/http"
"os"
"github.com/franciscoescher/golinkedin"
)
func encodeJson(w http.ResponseWriter, data interface{}) {
val, err := json.MarshalIndent(data, "", " ")
if err != nil {
return
}
_, _ = w.Write(val)
}
func serve(b *golinkedin.Builder, port string, route string) {
http.HandleFunc(route, func(w http.ResponseWriter, r *http.Request) {
// reads query params
errparam := r.URL.Query().Get("error")
if errparam != "" {
desc := html.UnescapeString(r.URL.Query().Get("error_description"))
msg := fmt.Sprintf("Error: %s\nError Description: %s", errparam, desc)
log.Fatal(msg)
}
code := r.URL.Query().Get("code")
c, err := b.GetClient(context.Background(), code)
if err != nil {
log.Fatal(err)
}
_, _ = w.Write([]byte("Response for GetProfile:\n\n"))
profile, err := c.GetProfile()
if err != nil {
log.Fatal(err)
} else {
fmt.Println(profile)
encodeJson(w, profile)
}
fmt.Println("========================================")
_, _ = w.Write([]byte("\n\n\n\n========================================\nResponse for GetPrimaryContact:\n\n"))
contact, err := c.GetPrimaryContact()
if err != nil {
log.Fatal(err)
} else {
fmt.Println(contact)
encodeJson(w, contact)
}
fmt.Println("========================================")
_, _ = w.Write([]byte("\n\n\n\n========================================\nResponse for GetEmailAddress:\n\n"))
email, err := c.GetEmailAddress()
if err != nil {
log.Fatal(err)
} else {
fmt.Println(email)
encodeJson(w, email)
}
fmt.Println("========================================")
_, _ = w.Write([]byte("\n\n\n\n========================================\nResponse for GetPeople:\n\n"))
people, err := c.GetPeople(profile.ID)
if err != nil {
log.Fatal(err)
} else {
fmt.Println(people)
encodeJson(w, people)
}
})
http.HandleFunc("/auth", func(w http.ResponseWriter, r *http.Request) {
authUrl := b.GetAuthURL("state")
_, _ = w.Write([]byte(authUrl))
})
fmt.Printf("Starting server at port %s\n", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
}
}
// To run this test, you need to setup a callback endpoint and place it at LINKEDIN_REDIRECT_HOST env.
// This endpoint should receive the callback from LinkedIn and receive the access code,
// which you can then use to call InitHTTPClientWithUserToken.
func main() {
clientId := os.Getenv("LINKEDIN_CLIENT_ID")
clientSecret := os.Getenv("LINKEDIN_CLIENT_SECRET")
redirect_host := os.Getenv("LINKEDIN_REDIRECT_HOST")
redirect_callback_url := os.Getenv("LINKEDIN_REDIRECT_ROUTE")
redirect_port := os.Getenv("LINKEDIN_REDIRECT_PORT")
fullRedirectURL := fmt.Sprintf("%s:%s%s", redirect_host, redirect_port, redirect_callback_url)
// add r_basicprofile to enable GetProfile (if you have the permission)
c := golinkedin.NewBuilder(clientId, clientSecret, []string{"r_liteprofile", "r_emailaddress"}, fullRedirectURL)
// to run this test, you need to uncomment the following line and
// visit the URL in your browser. You will be redirected to the
// redirect URL with a code. Copy the code and set it as an
// environment variable called LINKEDIN_CODE
//
authUrl := c.GetAuthURL("state")
fmt.Println("========================================")
fmt.Println("Visit the following URL in your browser:")
fmt.Println(authUrl)
fmt.Println("========================================")
serve(c, redirect_port, redirect_callback_url)
}