Firebase database data to R - python

I have a database in Google Firebase that has streaming sensor data. I have a Shiny app that needs to read this data and map the sensors and their values.
I am trying to pull the data from Firebase into R, but couldn't find any package that does this. The app is currently running on local downloaded data.
I found the FireData package, but have no idea how it works.
I do know that you can pull data from Firebase with Python, but I don't know enough Python to do so, but I would be willing to code it in R with rPython if necessary.
I have:
- The Firebase project link
- The username
- The password
Has anyone tried Firebase and R / Shiny in the past?
I hope my question is clear enough.

The basics to get started with the R package fireData are as follows. First you need to make sure that you have set up a firebase account on GCP (Google Cloud Platform). Once there set up a new project and give it a name
Now that you have a project select the option on the overview page that says "Add Firebase to your web app". It will give you all the credential information you need.
[
One way of dealing with this kind of information in R is to add it to an .Renviron file so that you do not need to share it with your code (for example if it goes to github). There is a good description about how to manage .Renviron files in the Efficient R Programming Book.
API_KEY=AIzaSyBxxxxxxxxxxxxxxxxxxxLwX1sCBsFA
AUTH_DOMAIN=stackoverflow-1c4d6.firebaseapp.com
DATABASE_URL=https://stackoverflow-1c4d6.firebaseio.com
PROJECT_ID=stackoverflow-1c4d6
This will be available to your R session after you restart R (if you have made any changes).
So now you can try it out. But first, change the rules of your firebase Database to allow anyone to make changes and to read (for these examples to work)
Now you can run the following examples
library(fireData)
api_key <- Sys.getenv("API_KEY")
db_url <- Sys.getenv("DATABASE_URL")
project_id <- Sys.getenv("PROJECT_ID")
project_domain <- Sys.getenv("AUTH_DOMAIN")
upload(x = mtcars, projectURL = db_url, directory = "new")
The upload function will return the name of the document it saved, that you can then use to download it.
> upload(x = mtcars, projectURL = db_url, directory = "main")
[1] "main/-L3ObwzQltt8IKjBVgpm"
The dataframe (or vector of value) you uploaded will be available in your Firebase Database Console immediately under that name, so you can verify that everything went as expected.
Now, for instance, if the name that was returned read main/-L3ObwzQltt8IKjBVgpm then you can download it as follows.
download(projectURL = db_url, fileName = "main/-L3ObwzQltt8IKjBVgpm")
You can require authentication, once you have created users. For example, you can create users like so (the users appear in your firebase console).
createUser(projectAPI = api_key, email = "test#email.com", password = "test123")
You can then get their user information and token.
registered_user <- auth(api_key, email = "test#email.com", password = "test123")
And then use the tokenID that is returned to access the files.
download(projectURL = db_url, fileName = "main/-L3ObwzQltt8IKjBVgpm",
secretKey = api_key,
token = registered_user$idToken)

Related

Firebase Admin SDK - Verify ID exists

As part of our current API calls we receive the user's firebase installation ID. e.g. ezOi0OrW6UQAuyf9m0MeRq
Is it possible to verify the ID actually exists via the Firebase Admin SDK in Python?
Tried some of the functions regarding users in the SDK docs and kept getting user ID not found. I may be trying it in the wrong place.
decoded_token = auth.verify_id_token(id_token)
uid = decoded_token['uid']
Reference link: https://firebase.google.com/docs/auth/admin/verify-id-tokens#python

GET and POST Function App Function/Host Keys Using Python (Azure Government)

I am attempting to retrieve and add function/host keys for an Azure Government function app via Python. I am currently working with the information from this question and the corresponding API page. While these are not specific to Azure Government, I would think the process would be similar after updating the URLs to the Azure Government versions. However, I am receiving the error "No route registered for '/api/functions/admin/token'" when running the jwt part of the given code. Is this approach feasible for what I am trying to do?
I also found somewhere that I instead might want to try a GET request like this:
resp = requests.get("https://management.usgovcloudapi.net/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Web/sites/<function-app-name>/functions/admin/masterkey?api-version=20XX-XX-XX", headers={"Authorization": f"Bearer {something}"})
This gives me the error "{"error":{"code":"InvalidAuthenticationToken","message":"The access token is invalid."}}", though. If this is indeed the correct approach, then what format should the Bearer token take?
Bit late answering but it may be useful for someone else in the future, it took me a while to find out how to do this.
If you want to retrieve the keys of a specific function within a function app then you can use list_function_keys() function from the Python SDK
Working with the Az management API directly may be a bit annoying and since the Azure CLI is written in Python whatever operation you do with the CLI you can do it directly in a Python script.
Here's an example of how you can retrieve the keys
from azure.identity import DefaultAzureCredential
from azure.mgmt.web import WebSiteManagementClient
# Your subscription ID
SUB_ID = "00000000-0000-0000-0000-000000000000"
fn_name = "some_function" # Name of your function
app_name = "some_app" # Name of your site/function app
rg_name = "some_rg" # Resource group name to which the function belongs
web_client = WebSiteManagementClient(subscription_id=SUB_ID, credential=DefaultAzureCredential())
keys = web_client.web_apps.list_function_keys(rg_name, app_name, fn_name)
# Your keys will be accessible in the additional_properties param
print(keys.additional_properties)
Hope it helps! I'm new on Azure so if I'm doing something wrong, please don't hesitate to point out my mistake and share your correction

How to retrieve all documents in a collection from firebase in Python

I am unable to retrieve the documents which are available in my collection inside the firestore database. Here is my code.
Every time I run this console dosen't print anything. I am following the documentation avaliable on this link https://firebase.google.com/docs/firestore/query-data/get-data, but it dosen't seems to work.
database_2 = firestore.client()
all_users_ref_2 = database_2.collection(u'user').stream()
for users in all_users_ref_2:
print(u'{} => {}'.format(users.id, users.to_dict()))
Do you have multiple projects? If so, double check that you open a client to the correct project. One quick way to confirm is to pass the project ID to the client:
db = firestore.Client('my-project-id')
Could be an authentication issue, you could download a service account key and use that in your project at the top.
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALs"] = "path/to/key.json"
or as mentioned
database_2 = firestore.Client("<project ID>")
make sure Client is a capital C

Posting user's ssh keys to github using python

I'm trying to automatically setup git system and I'm stuck in a process where I want to add user's key using the github api. This is what I've so far.
USER_SSH_PUB=glob.glob(os.path.expanduser('~/.ssh/temp.k.pub'))
user_Ssh_Pub_Key_File=open(USER_SSH_PUB[0],"r")
GITHUB_URL='https://api.github.com/users/abc/keys'
key_Data=urllib.urlencode({"title":"abcd","key":user_Ssh_Pub_Key_File.read()})
request=urllib2.Request(GITHUB_URL,key_Data) response=urllib2.urlopen(request) |
print response.read()
I get a 404 when I do this. Has anybody done this ?
I assume you want to take a public key and add that to a User's set of keys, i.e., through this API.
The problem is that you can only do this for the authenticated user, you can not do this on the behalf of a different user. So your GITHUB_URL would have to be https://api.github.com/user/keys and you would have to authenticate as user abcd in order to do that.
I don't think there are any python wrappers for the API using urllib2 which work (well), but there are a few listed here which includes mine which is pip-installable. With my library, your code would look like:
from github3 import login
g = login('abcd', password)
with open('~/.ssh/temp.k.pub', 'r') as fd:
key = g.create_key('abcd', fd)
print("Created {0}".format(key.title))
There are other popular wrappers like pygithub3 but I'm not familiar with them.

Appengine - Upload to Google Spreadsheet datastore values

I´d like to know how to upload to a Google Spreadsheet, values stored in the database of my application.
Objective:
Connecting to Google Spreadsheet and automatically fill in a chart in the admin area with values that were passed by the upload.
I've been giving a look in the docs and it seems to me that I have to use Bulk Loader.
Is this the only way? If yes how to configure the Handler if I have a spreadsheet as a link to link text
Someone could make a script to access the Google Spreadsheet and pass the values of a Model?
Model:
class User (db.Model):
photo= db.BlobProperty()
name = db.StringProperty (required = True)
surname = db.StringProperty (required = True)
adress = db.PostalAddressProperty (required = True)
phone = db.PhoneNumberProperty (required = True)
The Bulk Loader has nothing to do with interacting with a Google Docs Spreadsheet. It is used for adding records to your application's datastore.
To manipulate a Google Spreadsheet, you'll need to use the Google Spreadsheet API, which you could easily find on your own using Google.
No one here is going to write this code for you. This is not a free code-writing service. If you write some code that doesn't work and need some help figuring out why, edit your question and include the code along with a thorough description of what isn't working and why if you have any idea.

Categories