Skip to content

Commit

Permalink
fix #39: The Netatmo parser doesn't correctly authenticate when query…
Browse files Browse the repository at this point in the history
…ing data (#40)

* send the access token in the Authorization header using the Bearer scheme
for all calls except for the refresh token
* improved error reporting for failing requests
  • Loading branch information
raducotescu authored Dec 2, 2024
1 parent 4f0eab8 commit 8986f9f
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions sdk-parsers/RMParserFramework/parsers/personalnetatmo-parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def renewAccesTokenIfNeeded(self):
"client_id" : self.clientID,
"client_secret" : self.clientSecret
}
response = self.postRequest(self.authReq, postParams)
response = self.postRequest(self.authReq, postParams, True)
self.accessToken = self.params['accessToken'] =response['access_token']
self.refreshToken = self.params['refreshToken'] = response['refresh_token']
self.accessTokenExpiration = int(response['expire_in']) + time.time()
Expand Down Expand Up @@ -236,16 +236,36 @@ def getMeasure(self, moduleID, deviceID, measure):
return None


def postRequest(self, url, params):
def postRequest(self, url, params, is_refresh_call=False):
params = urlencode(params)
headers = {"Content-Type" : "application/x-www-form-urlencoded;charset=utf-8"}
headers = {
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
}

# Add the Authorization header if not a token refresh call and accessToken is available
if not is_refresh_call and self.accessToken and self.accessToken.strip():
headers["Authorization"] = "Bearer {}".format(self.accessToken)
log.debug("POST request to %s with params %s and headers %s", url, params, headers)
req = urllib2.Request(url=url, data=params, headers=headers)

try:
response = urllib2.urlopen(req)
return json.loads(response.read())
except urllib2.HTTPError, e:
# Log detailed error information
log.error("HTTPError: %s - %s", e.code, e.reason)
try:
error_content = e.read()
log.error("HTTPError Content: %s", error_content)
except Exception as read_error:
log.error("Unable to read HTTPError content: %s", read_error)
except urllib2.URLError, e:
log.error("URLError: %s", e.reason)
except ValueError, e:
log.error("Invalid JSON response: %s", e)
except Exception, e:
log.exception(e)
log.exception("Unexpected error during request: %s", e)

return None


Expand Down

0 comments on commit 8986f9f

Please sign in to comment.