-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
62 lines (54 loc) · 1.61 KB
/
app.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
package linkedin
import (
"fmt"
"strings"
)
// App holds the configuration for the LinkedIn application.
//
// ClientID and ClientSecret:
// https://www.linkedin.com/developers/apps/{appID}/auth
type App struct {
ClientID string
ClientSecret string
RedirectURI string
session *Session
}
// New creates a new LinkedIn application and sets clientID and clientSecret.
func New(clientID, clientSecret string) *App {
return &App{
ClientID: clientID,
ClientSecret: clientSecret,
session: defaultSession,
}
}
// ParseCode redeems authorization code for access and refresh tokens.
//
// See https://learn.microsoft.com/en-us/linkedin/shared/authentication/authorization-code-flow?context=linkedin%2Fcontext&tabs=HTTPS1
func (app *App) ParseCode(code string) (Token, error) {
code = strings.TrimSpace(code)
if code == "" {
err := fmt.Errorf("linkedIn: authorization code is empty")
return Token{}, err
}
token, err := app.session.sendAuthRequest("/accessToken", Params{
"grant_type": "authorization_code",
"client_id": app.ClientID,
"client_secret": app.ClientSecret,
"redirect_uri": app.RedirectURI,
"code": code,
})
return token, err
}
// Session creates a new LinkedIn session based on the app configuration.
func (app *App) Session(accessToken string) *Session {
return &Session{
BaseURL: VersionedBaseURL,
accessToken: accessToken,
app: app,
LinkedInVersion: "202404",
}
}
// SetLinkedInVersion overrides the default LinkedIn API version.
func (session *Session) SetLinkedInVersion(version string) {
session.LinkedInVersion = version
}