|
| 1 | +Integrate Firebase |
| 2 | +################## |
| 3 | + |
| 4 | +You can integrate Firebase project into your Python app in |
| 5 | +two ways. |
| 6 | + |
| 7 | +User based Authentication |
| 8 | +************************* |
| 9 | + |
| 10 | +For use with only user based authentication we can create the |
| 11 | +following configuration: |
| 12 | + |
| 13 | +.. code-block:: python |
| 14 | +
|
| 15 | + # Import Firebase REST API library |
| 16 | + import firebase |
| 17 | +
|
| 18 | + # Firebase configuration |
| 19 | + config = { |
| 20 | + "apiKey": "apiKey", |
| 21 | + "authDomain": "projectId.firebaseapp.com", |
| 22 | + "databaseURL": "https://databaseName.firebaseio.com", |
| 23 | + "projectId": "projectId", |
| 24 | + "storageBucket": "projectId.appspot.com", |
| 25 | + "messagingSenderId": "messagingSenderId", |
| 26 | + "appId": "appId" |
| 27 | + } |
| 28 | +
|
| 29 | + # Instantiates a Firebase app |
| 30 | + firebaseApp = firebase.initialize_app(config) |
| 31 | +.. |
| 32 | +
|
| 33 | + |
| 34 | +Admin based Authentication |
| 35 | +************************** |
| 36 | + |
| 37 | +We can optionally send `service account credential`_ to our app that |
| 38 | +will allow our server to authenticate with Firebase as an **admin** |
| 39 | +and disregard any security rules. |
| 40 | + |
| 41 | +.. _service account credential: https://firebase.google.com/docs/server/setup#prerequisites |
| 42 | + |
| 43 | + |
| 44 | +Service Account Secret File |
| 45 | +=========================== |
| 46 | + |
| 47 | +The following example uses the service account secrets `file` path |
| 48 | +as the value for `serviceAccount` key. |
| 49 | + |
| 50 | +.. code-block:: python |
| 51 | +
|
| 52 | + # Import Firebase REST API library |
| 53 | + import firebase |
| 54 | +
|
| 55 | + # Firebase configuration with service account secret file path |
| 56 | + config = { |
| 57 | + "apiKey": "apiKey", |
| 58 | + "authDomain": "projectId.firebaseapp.com", |
| 59 | + "databaseURL": "https://databaseName.firebaseio.com", |
| 60 | + "projectId": "projectId", |
| 61 | + "storageBucket": "projectId.appspot.com", |
| 62 | + "messagingSenderId": "messagingSenderId", |
| 63 | + "appId": "appId" |
| 64 | +
|
| 65 | + "serviceAccount": "path/to/serviceAccountCredentials.json" |
| 66 | + } |
| 67 | +
|
| 68 | + firebaseApp = firebase.initialize_app(config) |
| 69 | +.. |
| 70 | +
|
| 71 | + |
| 72 | +Service Account Secret Dict |
| 73 | +=========================== |
| 74 | + |
| 75 | + |
| 76 | +The following example uses the service account secrets `dict` |
| 77 | +as the value for `serviceAccount` key. |
| 78 | + |
| 79 | +.. code-block:: python |
| 80 | +
|
| 81 | + # Import Firebase REST API library |
| 82 | + import firebase |
| 83 | +
|
| 84 | + # Firebase configuration |
| 85 | + config = { |
| 86 | + "apiKey": "apiKey", |
| 87 | + "authDomain": "projectId.firebaseapp.com", |
| 88 | + "databaseURL": "https://databaseName.firebaseio.com", |
| 89 | + "projectId": "projectId", |
| 90 | + "storageBucket": "projectId.appspot.com", |
| 91 | + "messagingSenderId": "messagingSenderId", |
| 92 | + "appId": "appId" |
| 93 | + } |
| 94 | +
|
| 95 | + # Service Account Secret dict |
| 96 | + service_account_key = { |
| 97 | + "type": "service_account", |
| 98 | + "project_id": "project_id", |
| 99 | + "private_key_id": "private_key_id", |
| 100 | + "private_key": "private_key", |
| 101 | + "client_email": "client_email", |
| 102 | + "client_id": "client_id", |
| 103 | + "auth_uri": "https://accounts.google.com/o/oauth2/auth", |
| 104 | + "token_uri": "https://oauth2.googleapis.com/token", |
| 105 | + "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", |
| 106 | + "client_x509_cert_url": "client_x509_cert_url" |
| 107 | + } |
| 108 | +
|
| 109 | + config['serviceAccount'] = service_account_key |
| 110 | +
|
| 111 | + firebaseApp = firebase.initialize_app(config) |
| 112 | +.. |
| 113 | +
|
| 114 | +.. note:: |
| 115 | + Adding a service account will authenticate as an admin |
| 116 | + by default for all database queries, check out the |
| 117 | + `Authentication documentation` for how to authenticate users. |
| 118 | + |
| 119 | +Use Services |
| 120 | +************ |
| 121 | + |
| 122 | +A Firebase app can use multiple Firebase services. |
| 123 | + |
| 124 | +``firebaseApp.auth()`` - `Authentication` |
| 125 | + |
| 126 | +``firebaseApp.database()`` - `Database` |
| 127 | + |
| 128 | +``firebaseApp.storage()`` - `Storage` |
| 129 | + |
| 130 | +Check out the documentation for each service for further details. |
| 131 | + |
0 commit comments