-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth.go
67 lines (52 loc) · 1.35 KB
/
oauth.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
package bb_pix_go
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/url"
)
type AuthScope string
const (
ScopePixWrite AuthScope = "pix.write"
ScopePixRead AuthScope = "pix.read"
ScopeCobWrite AuthScope = "cob.write"
ScopeCobRead AuthScope = "cob.read"
ScopeCobvWrite AuthScope = "cobv.write"
ScopeCobvRead AuthScope = "cobv.read"
)
type AccessToken struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
Scope string `json:"scope"`
}
func (bb *BB) GetAccessToken(scope AuthScope) (*AccessToken, error) {
data := url.Values{}
data.Set("grant_type", "client_credentials")
data.Add("scope", string(scope))
body := bytes.NewBufferString(data.Encode())
// criando requisição
req, err := http.NewRequest(http.MethodPost, OauthHomologacaoUrl, body)
req.Header.Set("Authorization", bb.Basic)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
res, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 && resp.StatusCode <= 599 {
return nil, ErrOauthFailed
}
var token AccessToken
err = json.Unmarshal(res, &token)
if err != nil {
return nil, err
}
return &token, nil
}