intuit / oauth-jsclient

Intuit's NodeJS OAuth client provides a set of methods to make it easier to work with OAuth2.0 and Open ID
https://developer.intuit.com/
Apache License 2.0
120 stars 154 forks source link

Handle 401 AuthorizationFailure response #110

Open lardcanoe opened 3 years ago

lardcanoe commented 3 years ago

The AuthResponse.prototype.getJson function in intuit-oauth/src/response/AuthResponse.js needs to handle a 401 error more elegantly. I had to monkey-patch the function to figure out what was going on. The OAuth credentials I had were able to refresh the token, but the subsequent call failed with AuthResponse is not JSON.

URL:

'https://quickbooks.api.intuit.com/v3/company/91303#####/companyinfo/91303######'

Part of response:

  body: '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><IntuitResponse xmlns="http://schema.intuit.com/finance/v3" time="2021-04-07T06:07:54.701-07:00"><Fault type="AuthorizatonFault"><Error code="120"><Message>Authorization Failure</Message><Detail>AuthorizationFailure: --11014-You do not have access to use QuickBooks Online Plus., statusCode: 401</Detail></Error></Fault></IntuitResponse>',
  status: 401,
  statusText: 'Unauthorized'
}

Some key headers:

'QBO-Version',
'1955.157',
'ErrorCode',
'120',
'ErrorCause',
'AuthorizationFailure: --11014-You do not have access to use QuickBooks Online Plus., statusCode: 401',
'Message',
'Authorization Failure'
abisalehalliprasan commented 3 years ago

@lardcanoe : The SDK provides all the error details when an exception is thrown ref: https://github.com/intuit/oauth-jsclient#error-logging

Which provides more information about the error :

  originalMessage: 'Response has an Error',
  error: 'Unauthorized',
  error_description: 'Unauthorized',
  intuit_tid: '1-606dceb5-xxxxxxxxxxxce650'

If I am missing anything, Could you maybe post the exact error you saw so that we could see if any additional details need to be added?

Thanks

lardcanoe commented 3 years ago

https://github.com/intuit/oauth-jsclient/blob/master/src/response/AuthResponse.js#L106

which is called from

https://github.com/intuit/oauth-jsclient/blob/master/src/OAuthClient.js#L586

AuthResponse.prototype.getJson is throwing plain Error which is bubbling up that doesn't have any of the other fields present.

abisalehalliprasan commented 3 years ago

Thanks @lardcanoe . I will get this fixed in the next release 👍