Merge pull request #1583 from cli/oauth-device-fallback-fix
Handle "unauthorized_client" HTTP 400 error when doing Device Flow
This commit is contained in:
commit
2aa0f817ef
1 changed files with 18 additions and 11 deletions
|
|
@ -57,21 +57,28 @@ func (oa *OAuthFlow) ObtainAccessToken() (accessToken string, err error) {
|
|||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode == 401 || resp.StatusCode == 403 || resp.StatusCode == 404 {
|
||||
// OAuth Device Flow is not available; continue with OAuth browser flow with a
|
||||
// local server endpoint as callback target
|
||||
return oa.localServerFlow()
|
||||
} else if resp.StatusCode != 200 {
|
||||
return "", fmt.Errorf("error: HTTP %d (%s)", resp.StatusCode, initURL)
|
||||
}
|
||||
|
||||
var values url.Values
|
||||
bb, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
values, err := url.ParseQuery(string(bb))
|
||||
if err != nil {
|
||||
return
|
||||
if resp.StatusCode == 200 || strings.HasPrefix(resp.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
|
||||
values, err = url.ParseQuery(string(bb))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if resp.StatusCode == 401 || resp.StatusCode == 403 || resp.StatusCode == 404 ||
|
||||
(resp.StatusCode == 400 && values != nil && values.Get("error") == "unauthorized_client") {
|
||||
// OAuth Device Flow is not available; continue with OAuth browser flow with a
|
||||
// local server endpoint as callback target
|
||||
return oa.localServerFlow()
|
||||
} else if resp.StatusCode != 200 {
|
||||
if values != nil && values.Get("error_description") != "" {
|
||||
return "", fmt.Errorf("HTTP %d: %s (%s)", resp.StatusCode, values.Get("error_description"), initURL)
|
||||
}
|
||||
return "", fmt.Errorf("error: HTTP %d (%s)", resp.StatusCode, initURL)
|
||||
}
|
||||
|
||||
timeNow := oa.TimeNow
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue