fitbit.py 846 B

1234567891011121314151617181920212223
  1. """
  2. The Fitbit API breaks from the OAuth2 RFC standard by returning an "errors"
  3. object list, rather than a single "error" string. This puts hooks in place so
  4. that oauthlib can process an error in the results from access token and refresh
  5. token responses. This is necessary to prevent getting the generic red herring
  6. MissingTokenError.
  7. """
  8. from json import loads, dumps
  9. def fitbit_compliance_fix(session):
  10. def _missing_error(r):
  11. token = loads(r.text)
  12. if "errors" in token:
  13. # Set the error to the first one we have
  14. token["error"] = token["errors"][0]["errorType"]
  15. r._content = dumps(token).encode()
  16. return r
  17. session.register_compliance_hook("access_token_response", _missing_error)
  18. session.register_compliance_hook("refresh_token_response", _missing_error)
  19. return session