-
Notifications
You must be signed in to change notification settings - Fork 0
/
access_token.go
39 lines (29 loc) · 992 Bytes
/
access_token.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
package orange_money_apis
type Token struct {
AccessToken string `json:"access_token"`
Scope string `json:"scope"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
}
// Makes a request to generate a newaccessToken.
func requestNewAccesToken(key, secret, endPoint string) (accessToken string, requestError error) {
tokenPath := utils.join(endPoint, "/token")
basicKey := utils.hash(key, secret)
header := map[string][]string{
"Authorization": {utils.join("Basic ", basicKey)},
}
body := []byte("grant_type=client_credentials")
res, requestError := request.post(tokenPath, body, header)
if requestError != nil {
return "", requestError
}
if res.status != 200 && res.status != 201 {
return "", utils.newError("Backend failed to generate the access Token with message:", res.asText())
}
var token Token
unmarshallErr := res.asJson(&token)
if unmarshallErr != nil {
return "", unmarshallErr
}
return token.AccessToken, nil
}