Skip to content

Commit

Permalink
Improve ability to debug getData.py
Browse files Browse the repository at this point in the history
  • Loading branch information
m-roberts committed Jan 29, 2020
1 parent 6b96260 commit 1fefc55
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
20 changes: 11 additions & 9 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
TODO
==

## High priority
* Investigate moving from `credentials.ini` to sourcing from `config.js`
* Simplifies backing up MagicMirror config
* Consider how this might affect multiple users scenario
* Multiple modules, each with their own credentials?
* Investigate occasional OAuth InvalidGrantError when left for a while
### High priority
* Move from `credentials.ini` to sourcing from `config.js`
* Store tokens separately for each `client_id` (e.g. `tokens-13CD5J.ini`)
* Debugging: Add option for parameters instead of via stdin for manual execution in `getData.py`
* Improve look
* Fix spacing and text, etc.
* Remove "zzz" from sleep, keep spacing
Expand All @@ -17,7 +15,7 @@ TODO
* Fix weight icons
* Update screenshot

## Medium priority
### Medium priority
* Migrate to using Python 3
* Python 2 officially deprecated
* Fix iniHandler saving tokens
Expand Down Expand Up @@ -46,11 +44,15 @@ TODO
* Add more config parameters for controlling the view
* Add "Configuration Options" to README
* Add tests
* Add support for multiple users

## Low priority
### Low priority
* Regional formatting options
* 'Nice to have' information
* e.g. Device battery levels
* e.g. Friends leaderboard
* Fix to use latest version of python-shell


## Troubleshooting
### Invalid refresh token
If you are getting `oauthlib.oauth2.rfc6749.errors.InvalidGrantError: (invalid_grant)` when running `getData.py`, it likely means that your access token has expired and your refresh token is not the latest one associated with your account. Try running `setupAccess.py` again, and seeing if this helps.
26 changes: 22 additions & 4 deletions python/getData.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#!/usr/bin/env python
import fitbit
from fitbit.api import FitbitOauth2Client
import json
from iniHandler import set_debug_state, print_data, print_json, ReadCredentials, ReadTokens, WriteTokens
from sys import stdin
import select

# All output must be printed in JSON format, as it is read in by node_helper.js
##############################################
# All output must be printed in JSON format, #
# as it is read in by node_helper.js #
##############################################


def print_empty_resource(resource):
Expand All @@ -27,6 +29,8 @@ def handle_key_error(key_error, resource=None):

if __name__ == "__main__":

debug_mode = False

# Attempt to determine what data to get by
# reading an array passed in to stdin
resource_list = []
Expand Down Expand Up @@ -62,7 +66,13 @@ def handle_key_error(key_error, resource=None):
print_json("status", "Resources to get", resource_list_str)

client_id, client_secret = ReadCredentials()
print_json("debug", "client_id", client_id)
print_json("debug", "client_secret", client_secret)

access_token, refresh_token, expires_at = ReadTokens()
print_json("debug", "access_token", access_token)
print_json("debug", "refresh_token", refresh_token)
print_json("debug", "expires_at", expires_at)

def WriteTokenWrapper(token):
print_json("status", "Access token expired - refreshing tokens")
Expand All @@ -78,8 +88,16 @@ def WriteTokenWrapper(token):
WriteTokens(acc_tok, ref_tok, expires_at)

print_json("debug", "Creating authorised client")
authd_client = fitbit.Fitbit(client_id, client_secret, system="METRIC", oauth2=True, access_token=access_token, refresh_token=refresh_token,
expires_at=expires_at, refresh_cb=WriteTokenWrapper, redirect_uri="http://127.0.0.1:8888/")
authd_client = fitbit.Fitbit(client_id,
client_secret,
system="METRIC",
oauth2=True,
access_token=access_token,
refresh_token=refresh_token,
expires_at=expires_at,
refresh_cb=WriteTokenWrapper,
redirect_uri="http://127.0.0.1:8888/"
)

print_json("debug", "Polling API for data")
#####################################################
Expand Down

0 comments on commit 1fefc55

Please sign in to comment.