This program performs the OAuth2 handshake to obtain authorization from a user interested in sharing their Observant data with this app via the Observant Openlink API. Our goal is to provide developers with a Python example similar to the Java example provided by Observant. The code is not production-quality or secure (we store the secret and access token in plain text files), but it provides a starting point for writing a service that interacts with the Observant API.
In order to use the program, you must contact Observant to get permission (and the client secret, test-user login, and test-user password) to access their test facility. Place the client secret value in creds.json for the key 'secret'. Use the login and password when prompted to do so during the OAuth2 process. Keeping everything else unchanged (including the required redirect URL), this program should work for you out of the box. Improvements and suggestions are welcomed!
The program uses
- The Observant Openlink OADA API,
- The Google APIs OAuth2 library. Install via: pip install --upgrade google-api-python-client
- Python Flask
- Other setup required:
pip install --upgrade pip; pip install oauth2client httplib2 flask
Run the program via python ObsServ.py. And direct your browser to the server (http://localhost:9977/). The initial handshake takes a few seconds to perform all of the redirects, so be patient. More details can be found in the code concerning one work around required for Basic Auth credentials/access
To register a client with Observant you email Jon Havey and send him your preferred
- client\_id, e.g. MyFarmClient
- and redirect urls (http, https, static IP and localhost, port, and route, e.g. http://XXX.XXX.XXX.XXX:9977/myfarm/oada, https://XXX.XXX.XXX.XXX:9977/myfarm/oada, http://localhost:9977/myfarm/oada, https://localhost:9977/myfarm/oada He will send you the client secret in return. Once you receive this you can test against the IP-based redirects by setting up your server at that IP and running the program via python ObsServ.py prod. You should plug whichever redirect you are testing into redir=... in main in the production setting.
There is also a command line version you can use. This version gives you a URL to place in your browser so that you can manually retrieve the authorization code.
- Generate and store the access tokens:
python ObsServ_CLI.py - Use stored access tokens to query the test service (Watermark sensor):
python ObsServ_CLI.py -r -n - Use stored access tokens (or generate them) to query the test service (Watermark sensor):
python ObsServ_CLI.py -r