Firebase .get function : get() takes 2 positional arguments but 3 were given - python

anyone knows whats gg on? I followed this online tutorial and actually used this exact same fucntion a year ago. Did updates happens? not sure why I can't run it anymore
this was from a youtube tutorial:
firebase = firebase.FirebaseApplication('https://capstone-eb408.firebaseio.com/',None)
result=firebase.get("/capstone-eb408/picdetails", '')
print(result)
I also tried this and I got the same error ( this was from my prev ears code which ran fine):
firebase = firebase.FirebaseApplication('https://capstone-eb408.firebaseio.com/',None)
result=firebase.get('picsdetails', 'picname')
print(result)
TypeError: get() takes 2 positional arguments but 3 were given
And when I tried this.... they said they needed a string for an argument? but its a string am I right? am confused ehehe:
firebase = firebase.FirebaseApplication('https://capstone-eb408.firebaseio.com/',None)
result=firebase.get('\capstone-eb408\picdetails')
print(result)
error: request = urllib.request.Request(json_url+'.json?auth='+self.firebaseToken)
TypeError: must be str, not NoneType
Send help hahah :) thanku!!!

FOUND THE ANS!! AND HOPE THE HELPS OTHERS!!!
realize that we need to get credentials and set up that part too now! so here is what to do!
In firebase, go to setting >project settings> service accounts> Firebase admin SDK
you will be able to generate your token! then paste the code you see given to help with authentification into whatever other code you have.
Remember to save the token in a folder and paste the path into the code!!
My code is now this
from firebase import firebase
import firebase_admin
from firebase_admin import credentials
''' Database Set Up '''
cred = credentials.Certificate("/Users/Kee Ga Mun/Documents/academics/Term 8/Capstone/firebasekey.json")
firebase_admin.initialize_app(cred)
firebase = firebase.FirebaseApplication("https://capstone-eb408.firebaseio.com/",None)
data={'Name':'Sembcorp','Number':123543}
result= firebase.post("/capstonert-d9746/Customer",data)
print(result)
And we can replace with other get.() etc functions too but this one post data :)

Related

How do I set enable_https_traffic_only using Python SDK in Azure?

I am trying to write a Python script to enable "Secure transfer enabled" on an Azure storage account. I'm having some trouble understanding what I need to put in for the following piece of code:
storage_account = storage_client.storage_accounts.update(
GROUP_NAME, STORAGE_ACCOUNT_NAME,
StorageAccountUpdateParameters(enable_https_traffic_only(bool=true))
)
I've tried just about every combination I can think of to set this value to true, has anyone got this scenario to work?
Your code is not valid Python:
storage_account = storage_client.storage_accounts.update(
GROUP_NAME, STORAGE_ACCOUNT_NAME,
StorageAccountUpdateParameters(enable_https_traffic_only = True)
)
This page might help:
https://github.com/Azure-Samples/storage-python-manage/blob/master/example.py

Retrieve gs:// path for a storage Bucket and Object at GCS using Python api client

I'm working on a script to create google cloud functions using api, in which i need to get gs:// form of path to the storage bucket OR Object,
Here's what i have tried:
svc = discovery.build('storage', 'v1', http=views.getauth(), cache_discovery=False)
svc_req = svc.objects().get(bucket=func_obj.bucket, object=func_obj.fname, projection='full')
svc_response = svc_req.execute()
print(' Metadata is coming below:')
print(svc_response)
It returns metadata which doesn't include any link in the form of gs:// , how can i get the path to a bucket OR Object in the form of "gs://"
Help me, please!
Thanks in advance!
If you go to API Explorer and simulate your request you will see that this library returns you exactly the same output in python data structures. In there ID looks the most like gs:// link:
"id": "test-bucket/text.txt/0000000000000000"
or in python it looks like:
u'id': u'test-bucket/text.txt/00000000000000000'
Simple use this to transform it into a gs:// link:
import os
u'gs://' + os.path.dirname(svc_response['id'])
which would return:
u'gs://test-bucket/text.txt'
If you want to use the google-cloud-python you will be facing the same issue.

Unable to apply projection query over google datastore. Error : No matching index found

I am new to Google Datastore and following this google documentation.
https://cloud.google.com/datastore/docs/datastore-api-tutorial
I have done authorization for calling Google APIs by reading this doc.
https://developers.google.com/identity/protocols/application-default-credentials#callingpython
I have only two files:
1. client_secret.json
2. datastoreConnection.py
and this is my datastoreConnection.py till now:
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = '/path/to/client_secret.json'
from oauth2client.client import GoogleCredentials
credentials = GoogleCredentials.get_application_default()
from google.cloud import datastore
client = datastore.Client(project ="xyz",namespace = "staging")
def list_tasks(client):
query = client.query(kind='testData')
query.projection = ['patientName']
return list(query.fetch(limit=10))
print(list_tasks(client))
This code is working fine and returning me the required data.
Problem is arising when I apply Projection with multiple properties.
e.g
query.projection = ['patientName','age']
code is giving me the error:
google.cloud.exceptions.PreconditionFailed: 412 no matching index found. recommended index is:<br/>- kind: testData<br/> properties:<br/> - name: age<br/> - name: patientName<br/>
For making projection query I read this.
https://cloud.google.com/datastore/docs/concepts/queries#datastore-projection-query-python
I have cross checked the property names but still having the same error.
How can I solve this ?
I have seen index.yaml file in other questions related to this kind of problem. Is this necessary to use and what are its benefit?
Any time you need to retrieve more than one property, but not all properties (*), you need to have a matching composite index. In this case you need a composite index on patient name and age properties. You can create a composite index using index.yaml file or gcloud command line tool. The documentation below should help you:
https://cloud.google.com/datastore/docs/concepts/indexes.

get all files in drive via REST API

I'm trying to access all my files in my drive via the endpoint
/me/drive/root/children
However it returns 0 children even though the following observations happen:
Calling /me/drive/root returns:
","folder":{"childCount":3},"root":{},"size":28413,"specialFolder":{"name":"documents"}}
More interestingly, doing the API call from the Graph Explorer:
https://graph.microsoft.io/en-us/graph-explorer does show the 3 files that I have when using me/drive/root/children.
The graph explorer matches perfectly the API call when using /me/drive/root, but not /me/drive/root/children.
What is happening?
EDIT:
Following Brad suggestion I decoded the token with https://jwt.io/ and the parameters scp reads:
scp": "Mail.Send User.Read",
Second edit:
I removed all the app permissions from apps.dev.microsoft.com and I still have the same observations. It's looks like the permissions I set there have no effect.
The code above follows the example found at:
https://dev.office.com/code-samples-detail/5989
As it turns out, the whole confusing was coming from here:
microsoft = oauth.remote_app(
'microsoft',
consumer_key=client_id,
consumer_secret=client_secret,
request_token_params={'scope': 'User.Read Mail.Send Files.Read Files.ReadWrite'},
base_url='https://graph.microsoft.com/v1.0/',
request_token_url=None,
access_token_method='POST',
access_token_url='https://login.microsoftonline.com/common/oauth2/v2.0/token',
authorize_url='https://login.microsoftonline.com/common/oauth2/v2.0/authorize'
)
I did not have the right scopes declared in request_token_params. So even if the app has the permissions, without the scopes declared there you cannot access the sheets.

Trying to do batch update to Google spreadsheet using gdata python libraries

I have been trying to figure this out for a while now and just dont seem to be able to break through so hopefully someone out there has done this before.
My issue is that I am trying to do a batch update of a google spreadsheet using the gdata python client libraries and authenticating via oauth2. I have found an example of how to do the batch update using the gdata.spreadsheet.service module here: https://code.google.com/p/gdata-python-client/wiki/UsingBatchOperations
However that does not seem to work when authenticating via oauth2 and so I am having to use the gdata.spreadsheets.client module instead as discussed in this post: https://code.google.com/p/gdata-python-client/issues/detail?id=549
Using the gdata.spreadsheets.client module works for authentication and for updating the sheet however batch commands does not seem to work. Below is my latest variation of the code which is about the closest I have got. It seems to work but the sheet is not updated and the batch_status returned is: 'Insert not supported on batch.' (Note: I did try modifying the batch_operation and batch_id parameters of the CellEntries in the commented out code but this did not work either.)
Thanks for any help you can provide.
import gdata
import gdata.gauth
import gdata.service
import gdata.spreadsheets
import gdata.spreadsheets.client
import gdata.spreadsheets.data
token = gdata.gauth.OAuth2Token(client_id=Client_id,client_secret=Client_secret,scope=Scope,
access_token=ACCESS_TOKEN, refresh_token=REFRESH_TOKEN,
user_agent=User_agent)
client = gdata.spreadsheets.client.SpreadsheetsClient()
token.authorize(client)
range = "D6:D13"
cellq = gdata.spreadsheets.client.CellQuery(range=range, return_empty='true')
cells = client.GetCells(file_id, 'od6', q=cellq)
objData = gdata.spreadsheets.data
batch = objData.BuildBatchCellsUpdate(file_id, 'od6')
n = 1
for cell in cells.entry:
cell.cell.input_value = str(n)
batch.add_batch_entry(cell, cell.id.text, batch_id_string=cell.title.text, operation_string='update')
n = n + 1
client.batch(batch, force=True)

Categories