Skip to content

Commit 1f93366

Browse files
authored
Fix DeviceAuth not populating RetrieveError
1 parent 792c877 commit 1f93366

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

deviceauth.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,29 @@ func retrieveDeviceAuth(ctx context.Context, c *Config, v url.Values) (*DeviceAu
116116
return nil, fmt.Errorf("oauth2: cannot auth device: %v", err)
117117
}
118118
if code := r.StatusCode; code < 200 || code > 299 {
119-
return nil, &RetrieveError{
119+
retrieveError := &RetrieveError{
120120
Response: r,
121121
Body: body,
122122
}
123+
124+
content, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type"))
125+
switch content {
126+
case "application/x-www-form-urlencoded", "text/plain":
127+
// some endpoints return a query string
128+
vals, err := url.ParseQuery(string(body))
129+
if err != nil {
130+
return nil, fmt.Errorf("oauth2: cannot parse response: %v", err)
131+
}
132+
retrieveError.ErrorCode = vals.Get("error")
133+
retrieveError.ErrorDescription = vals.Get("error_description")
134+
retrieveError.ErrorURI = vals.Get("error_uri")
135+
default:
136+
if err = json.Unmarshal(body, &retrieveError); err != nil {
137+
return nil, fmt.Errorf("oauth2: cannot parse json: %v", err)
138+
}
139+
}
140+
141+
return nil, err
123142
}
124143

125144
da := &DeviceAuthResponse{}

0 commit comments

Comments
 (0)