forked from HackIllinois/api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedin_service.go
192 lines (154 loc) · 4.38 KB
/
linkedin_service.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package service
import (
"errors"
"fmt"
"github.com/HackIllinois/api/services/auth/config"
"github.com/HackIllinois/api/services/auth/models"
"github.com/levigross/grequests"
)
const LINKEDIN_USER_INFO_URL = "https://api.linkedin.com/v1/people/~:(id,formatted-name,email-address,first-name,last-name)"
/*
Uses a valid OAuth token to get the user's primary email.
*/
func GetLinkedinEmail(oauth_token string) (string, bool, error) {
request, err := grequests.Get(LINKEDIN_USER_INFO_URL, &grequests.RequestOptions{
Headers: map[string]string{
"Authorization": fmt.Sprintf("Bearer %v", oauth_token),
"Content-Type": "application/json",
"x-li-format": "json"},
})
if err != nil {
return "", false, err
}
var user_info models.LinkedinUserInfo
err = request.JSON(&user_info)
if err != nil {
return "", false, err
}
if user_info.ID == "" {
return "", false, errors.New("Invalid OAuth token.")
}
return user_info.Email, true, nil
}
/*
Uses a valid OAuth authorization code to get a valid OAuth token for the user.
*/
func GetLinkedinOauthToken(code string, redirect_uri string) (string, error) {
const LINKEDIN_OAUTH_TOKEN_URL = "https://www.linkedin.com/oauth/v2/accessToken"
request, err := grequests.Post(LINKEDIN_OAUTH_TOKEN_URL, &grequests.RequestOptions{
Data: map[string]string{
"client_id": config.LINKEDIN_CLIENT_ID,
"client_secret": config.LINKEDIN_CLIENT_SECRET,
"code": code,
"redirect_uri": redirect_uri,
"grant_type": "authorization_code",
},
Headers: map[string]string{
"Accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded",
"x-li-format": "json",
},
})
if err != nil {
return "", err
}
var oauth_token models.LinkedinOauthToken
err = request.JSON(&oauth_token)
if err != nil {
return "", err
}
if oauth_token.Token == "" {
return "", errors.New("Invalid OAuth code.")
}
return oauth_token.Token, nil
}
/*
Uses a valid OAuth token to get the user's unique id.
*/
func GetLinkedinUniqueId(oauth_token string) (string, error) {
request, err := grequests.Get(LINKEDIN_USER_INFO_URL, &grequests.RequestOptions{
Headers: map[string]string{
"Authorization": fmt.Sprintf("Bearer %v", oauth_token),
"Content-Type": "application/json",
"x-li-format": "json"},
})
if err != nil {
return "", err
}
var user_info models.LinkedinUserInfo
err = request.JSON(&user_info)
if err != nil {
return "", err
}
if user_info.ID == "" {
return "", errors.New("Invalid OAuth token.")
}
return fmt.Sprintf("linkedin%v", user_info.ID), nil
}
/*
Uses a valid OAuth token to get the user's username.
*/
func GetLinkedinUsername(oauth_token string) (string, error) {
request, err := grequests.Get(LINKEDIN_USER_INFO_URL, &grequests.RequestOptions{
Headers: map[string]string{
"Authorization": fmt.Sprintf("Bearer %v", oauth_token),
"Content-Type": "application/json",
"x-li-format": "json"},
})
if err != nil {
return "", err
}
var user_info models.LinkedinUserInfo
err = request.JSON(&user_info)
if err != nil {
return "", err
}
if user_info.ID == "" {
return "", errors.New("Invalid oauth token")
}
return user_info.Username, nil
}
/*
Uses a valid OAuth token to get the user's name.
*/
func GetLinkedinFirstName(oauth_token string) (string, error) {
request, err := grequests.Get(LINKEDIN_USER_INFO_URL, &grequests.RequestOptions{
Headers: map[string]string{"Authorization": fmt.Sprintf("Bearer %v", oauth_token),
"Content-Type": "application/json",
"x-li-format": "json"},
})
if err != nil {
return "", err
}
var user_info models.LinkedinUserInfo
err = request.JSON(&user_info)
if err != nil {
return "", err
}
if user_info.ID == "" {
return "", errors.New("Invalid OAuth token.")
}
return user_info.FirstName, nil
}
/*
Uses a valid OAuth token to get the user's last name.
*/
func GetLinkedinLastName(oauth_token string) (string, error) {
request, err := grequests.Get(LINKEDIN_USER_INFO_URL, &grequests.RequestOptions{
Headers: map[string]string{"Authorization": fmt.Sprintf("Bearer %v", oauth_token),
"Content-Type": "application/json",
"x-li-format": "json"},
})
if err != nil {
return "", err
}
var user_info models.LinkedinUserInfo
err = request.JSON(&user_info)
if err != nil {
return "", err
}
if user_info.ID == "" {
return "", errors.New("Invalid OAuth token.")
}
return user_info.LastName, nil
}