forked from rwestlund/quickbooks-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.go
47 lines (43 loc) · 1.24 KB
/
users.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
package quickbooks
import (
"encoding/json"
"net/http"
)
type Address struct {
StreetAddress string `json:"streetAddress"`
Locality string `json:"locality"`
Region string `json:"region"`
PostalCode string `json:"postalCode"`
Country string `json:"country"`
}
type UserInfo struct {
Sub string `json:"sub"`
Email string `json:"email"`
EmailVerified bool `json:"emailVerified"`
GivenName string `json:"givenName"`
FamilyName string `json:"familyName"`
PhoneNumber string `json:"phoneNumber"`
PhoneNumberVerified bool `json:"phoneNumberVerified"`
Address Address `json:"address"`
}
func (c *Client) GetUserInfo(idToken string) (*UserInfo, error) {
var req *http.Request
req, err := http.NewRequest("GET", c.discoveryAPI.UserinfoEndpoint, nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Authorization", "Bearer "+idToken)
var res *http.Response
res, err = c.Client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, parseFailure(res)
}
var r UserInfo
err = json.NewDecoder(res.Body).Decode(&r)
return &r, err
}