|
1 | 1 | package client |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "encoding/json" |
5 | 6 | "errors" |
6 | 7 | "fmt" |
7 | 8 | "io" |
8 | 9 | "log" |
9 | 10 | "net/http" |
10 | 11 | "net/url" |
11 | | - "strings" |
12 | 12 | "time" |
13 | 13 | ) |
14 | 14 |
|
@@ -46,6 +46,11 @@ type endpoints struct { |
46 | 46 | Platforms *url.URL `json:"meshplatforms"` |
47 | 47 | } |
48 | 48 |
|
| 49 | +type loginRequest struct { |
| 50 | + ClientId string `json:"clientId"` |
| 51 | + ClientSecret string `json:"clientSecret"` |
| 52 | +} |
| 53 | + |
49 | 54 | type loginResponse struct { |
50 | 55 | Token string `json:"access_token"` |
51 | 56 | ExpireSec int `json:"expires_in"` |
@@ -86,20 +91,25 @@ func (c *MeshStackProviderClient) login() error { |
86 | 91 | return err |
87 | 92 | } |
88 | 93 |
|
89 | | - formData := url.Values{} |
90 | | - formData.Set("client_id", c.apiKey) |
91 | | - formData.Set("client_secret", c.apiSecret) |
92 | | - formData.Set("grant_type", "client_credentials") |
| 94 | + loginRequest := loginRequest{ |
| 95 | + ClientId: c.apiKey, |
| 96 | + ClientSecret: c.apiSecret, |
| 97 | + } |
| 98 | + |
| 99 | + payload, err := json.Marshal(loginRequest) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
93 | 103 |
|
94 | | - req, _ := http.NewRequest(http.MethodPost, loginPath, strings.NewReader(formData.Encode())) |
95 | | - req.Header.Add("Content-Type", "application/x-www-form-urlencoded") |
| 104 | + req, _ := http.NewRequest(http.MethodPost, loginPath, bytes.NewBuffer(payload)) |
| 105 | + req.Header.Add("Content-Type", "application/json") |
96 | 106 |
|
97 | 107 | res, err := c.httpClient.Do(req) |
98 | 108 |
|
99 | 109 | if err != nil { |
100 | 110 | return err |
101 | 111 | } else if res.StatusCode != 200 { |
102 | | - return errors.New(ERROR_AUTHENTICATION_FAILURE) |
| 112 | + return errors.New(fmt.Sprintf("Status %d: %s", res.StatusCode, ERROR_AUTHENTICATION_FAILURE)) |
103 | 113 | } |
104 | 114 |
|
105 | 115 | defer res.Body.Close() |
|
0 commit comments