GithubHelp home page GithubHelp logo

airthings / python-flask-ext-api-example Goto Github PK

View Code? Open in Web Editor NEW
6.0 7.0 8.0 477 KB

A simple Flask front-end web app accessing the Airthings ext-api using the requests_oauthlib library in Python.

Python 59.70% HTML 19.77% CSS 20.53%

python-flask-ext-api-example's Introduction

Accessing the Airthings Api with Python and Flask

This sample code involves using a simple Flask webapp providing a front-end interface using the requests-oauthlib Python3 library to access the Airthings for Business API using the Authorization Grant OAuth2 flow.

Please note that this is a basic example created only to show how to access information from the Airthings API. Do not use this code in production without first implementing standard security within your web application.

Required Dependencies

  • Python 3
  • pip to install the python packages for the project

Compatibility

This example is built to work with the Airthings for Business API.

Installation

To set up the example Flask webapp simply run the following from the command line:

git clone https://github.com/Airthings/python-flask-ext-api-example
cd python-flask-ext-api-example
pip install -r requirements.txt

The webapp can then be run by using:

python app.py

Basic Usage

Please refer to the Getting Started guide for a more generalized overview of accessing the the Airthings API.

Before starting ensure you have your credentials:

Adding Configuration and Gaining Access Token

The AirthingsAccount class within the AirthingsAccount.py file can be used to begin the OAuth2 flow. The class can be initialised with by being supplied with the API credentials. Three steps must be followed to gain an access token:

Step 1

Initialize the AirthingsAccount with your API credentials.

my_account = AirthingsAccount(
    client_id     = AIRTHINGS_CLIENT_ID,
    client_secret = IRTHINGS_CLIENT_SECRET,
    redirect_uri  = APP_REDIRECT_URI
)

Step 2

The get_authorization function returns an authorization URL that will provide a link provided by Airthings that will let you log into your account via username and password. Upon logging in you will then be redirected to the redirect URI that you provided.

my_account.get_authorization()

Step 3

Ideally, the redirect URI will contain a code that can be used to fetch access tokens. The get_access_token function can be provided with the redirected page URL you were sent to from the previous step to allow you to get your access token.

my_account.get_access_token(request.url)

Making Requests

Currently, only GET requests are supported, which are listed in the API documentation. The routes defined in the app.py file detail how these requests can be made, while the index.html can display basic JSON results.

Securing API Credentials

There are a number of ways to supply API credentials to the AirthingsAccount class. It is critical to never include client secrets within your web app directly. This can lead to a number of security risks that may compromise your account information.

python-flask-ext-api-example's People

Contributors

mcallistertyler avatar bloonguyen1207 avatar devdavidkarlsson avatar gdubya avatar

Stargazers

Josh Breeds avatar Nick Snell avatar Jan Ebert avatar Eddy G avatar Mike avatar Alexander Sagen avatar

Watchers

Alexander Sagen avatar James Cloos avatar  avatar  avatar Antoine Promerova avatar Özer Cevikaslan avatar Josh Breeds avatar

python-flask-ext-api-example's Issues

Where To Input / Find Redirect URI For Home / Domestic Beta API

I am trying to set up this example with the new home / domestic API.

I have created a client in my dashboard however, in the readme of this repo, the app requires a redirect URI and states that this can be found at this location however, there is no option to either input a URI, or one displayed in the Integrations->API->Clients screen

Expose My Pollen Levels And Outdoor Air Quality In API Response?

I am aware that these are pseudo values that aren't collected from the device, but it appears that there are other sensor values which are the same.

Would it be possible to expose the pollen levels and outdoor air quality metrics that are available in the app and for my location via the API?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.