Sharing Google Sheet with API Credentials - Domain Not Found - python

I'm a beginner trying to access a google sheet via python gspread. I'm following these instructions: https://towardsdatascience.com/accessing-google-spreadsheet-data-using-python-90a5bc214fd2.
I believe I've successfully created my credentials: survivor-points-update#update-survivor-gspread.iam.gserviceaccount.com. When I try to share the google sheets document with this email, I get an email response telling me delivery failed because the domain couldn't be found. More specifically:
DNS Error: 19491 DNS type 'mx' lookup of update-survivor-gspread.iam.gserviceaccount.com responded with code NXDOMAIN Domain name not found: update-survivor-gspread.iam.gserviceaccount.com
I've found this alternative but I got a 401 error: The OAuth client was not found.:
client_id=<CLIENTID_FROM_GOOGLE_DEVLOPER_CONSOLE>
redirect_uri=http://localhost:8080/
scope=https://spreadsheets.google.com/feeds
access_type=offline
response_type=code
I've tried setting up my credentials a second time, but those don't work either. Thanks in advance for any help.
EDIT: On a hunch, I tried to share other documents with my two service accounts, and that didn't help.
EDIT 2: I tried a few different settings and scope as suggested in the git hub issue.
My main code is:
scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name(cred_path, scope)
client = gspread.authorize(creds)
sheet = client.open(gsheet).sheet1
With this scope, I get the following error:
Traceback (most recent call last):
File "<ipython-input-8-b2d4b9300652>", line 1, in <module>
sheet = client.open(gsheet).sheet1
File "C:\Users\User\Anaconda3\lib\site-packages\gspread\client.py", line 123, in open
self.list_spreadsheet_files()
File "C:\Users\User\Anaconda3\lib\site-packages\gspread\client.py", line 96, in list_spreadsheet_files
res = self.request('get', url, params=params).json()
File "C:\Users\User\Anaconda3\lib\site-packages\gspread\client.py", line 79, in request
raise APIError(response)
APIError: {
"error": {
"errors": [
{
"domain": "global",
"reason": "insufficientPermissions",
"message": "Insufficient Permission: Request had insufficient authentication scopes."
}
],
"code": 403,
"message": "Insufficient Permission: Request had insufficient authentication scopes."
}
}
If I change:
scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
then I get:
Traceback (most recent call last):
File "C:\Users\User\Anaconda3\lib\site-packages\gspread\client.py", line 123, in open
self.list_spreadsheet_files()
File "C:\Users\User\Anaconda3\lib\site-packages\gspread\utils.py", line 37, in finditem
return next((item for item in seq if func(item)))
StopIteration
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<ipython-input-22-b2d4b9300652>", line 1, in <module>
sheet = client.open(gsheet).sheet1
File "C:\Users\User\Anaconda3\lib\site-packages\gspread\client.py", line 131, in open
raise SpreadsheetNotFound
SpreadsheetNotFound
I've tried enabling the google sheets API to both my projects and run the code again with both the scope options.
If I run client.openall() it returns only the following list: [<Spreadsheet 'Access Rights Spreadsheet' id:1TwF6neGtogHAsqu4vjIiGWpfUPiQmVFgF5kR8k_8jC0>]
Is there another scope I can add or API I can enable?
EDIT 3: I don't know how [<Spreadsheet 'Access Rights Spreadsheet' id:1TwF6neGtogHAsqu4vjIiGWpfUPiQmVFgF5kR8k_8jC0>] was shared with me. I thought it was just one that everybody had.
When I run this:
bk = client.open_by_key('1TwF6neGtogHAsqu4vjIiGWpfUPiQmVFgF5kR8k_8jC0')
sht = bk.get_worksheet(0)
l = sht.get_all_values()
l is an empty list. bk.worksheets() returns [<Worksheet 'Sheet1' id:0>] so I don't see any other data.

Related

Python3 firebase storage SDK cannot upload to Emulator

I have the following code snippet:
import firebase_admin
from firebase_admin import credentials
from firebase_admin import storage
from google.cloud import storage
class firebase_storage():
def __init__(self, path_to_sak, root_bucket):
try:
self.cred = credentials.Certificate(path_to_sak)
firebase_admin.initialize_app(self.cred)
except Exception as e:
print("Firebase App may have already been initialized")
self.bucket = firebase_admin.storage.bucket(root_bucket)
def upload(self, key, file_path):
blob = storage.Blob(key, self.bucket)
blob.upload_from_filename(file_path)
def download(self, key, file_path):
blob = storage.Blob(key, self.bucket)
blob.download_to_filename(file_path)
def upload_string(self, key, string, mime_type):
blob = storage.Blob(key, self.bucket)
blob.upload_from_string(string, content_type=mime_type)
I'm using Firebase Emulators for Storage, I have verified that downloads work using the method call firebase_storage.download().
However, when I try to call upload() the following exception is thrown:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/google/cloud/storage/blob.py", line 2348, in upload_from_file
created_json = self._do_upload(
File "/usr/local/lib/python3.8/dist-packages/google/cloud/storage/blob.py", line 2170, in _do_upload
response = self._do_multipart_upload(
File "/usr/local/lib/python3.8/dist-packages/google/cloud/storage/blob.py", line 1732, in _do_multipart_upload
response = upload.transmit(
File "/usr/local/lib/python3.8/dist-packages/google/resumable_media/requests/upload.py", line 149, in transmit
self._process_response(response)
File "/usr/local/lib/python3.8/dist-packages/google/resumable_media/_upload.py", line 116, in _process_response
_helpers.require_status_code(response, (http_client.OK,), self._get_status_code)
File "/usr/local/lib/python3.8/dist-packages/google/resumable_media/_helpers.py", line 99, in require_status_code
raise common.InvalidResponse(
google.resumable_media.common.InvalidResponse: ('Request failed with status code', 400, 'Expected one of', <HTTPStatus.OK: 200>)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "boot.py", line 55, in <module>
run()
File "boot.py", line 35, in run
fb_storage.upload(key, file)
File "/root/python_db_client/src/firebase_storage.py", line 20, in upload
blob.upload_from_filename(file_path)
File "/usr/local/lib/python3.8/dist-packages/google/cloud/storage/blob.py", line 2475, in upload_from_filename
self.upload_from_file(
File "/usr/local/lib/python3.8/dist-packages/google/cloud/storage/blob.py", line 2364, in upload_from_file
_raise_from_invalid_response(exc)
File "/usr/local/lib/python3.8/dist-packages/google/cloud/storage/blob.py", line 3933, in _raise_from_invalid_response
raise exceptions.from_http_status(response.status_code, message, response=response)
google.api_core.exceptions.BadRequest: 400 POST http://myserver.com:9194/upload/storage/v1/b/xxxxxx.appspot.com/o?uploadType=multipart: Bad Request: ('Request failed with status code', 400, 'Expected one of', <HTTPStatus.OK: 200>)
My storage.rules look like this:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow write, read: if true;
}
}
}
And so, it would appear that public read/write access is allowed.
Everything is working, I have other emulators (Firestore, Auth) that is working fine, but Storage uploads refuse to work :(
Any help would be greatly appreciated thank you!
Maybe there is a problem initializing your app. I see your are taking granted that the app is initialized if there is an error while initializing. Try checking connection first! It may help...
Python Admin SDK does not currently support the Storage emulator according to the documentation
https://firebase.google.com/docs/emulator-suite/install_and_configure#admin_sdk_availability

How decipher what the terminal Is saying and possibly offer a solution as to how to get the code to work?

I am brand new to coding on python as well as coding in general. I am doing a project that takes information from google sheets and displays it in the terminal. It doesn't seem to be working but as a beginner (this is my first project), I have no clue what to do. I used google api to get the credentials. Here is the code I wrote:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('sheetsproject-765dzxv233334.json',scope)
client = gspread.authorize(creds)
sheet = client.open('python test').sheet1
options = sheet.get_all_records()
print(options)
this is the result in the terminal:
Traceback (most recent call last):
File "/Users/jordanmaggin/Desktop/python/sheets.py", line 9, in <module>
sheet = client.open('python test').sheet1 File "/Users/jordan/opt/anaconda3/lib/python3.7/site-packages/gspread/client.py", line 123, in open
self.list_spreadsheet_files()
File "/Users/jordanmaggin/opt/anaconda3/lib/python3.7/site-packages/gspread/client.py", line 96, in list_spreadsheet_files
res = self.request('get', url, params=params).json()
File "/Users/jordan/opt/anaconda3/lib/python3.7/site-packages/gspread/client.py", line 79, in request
raise APIError(response) gspread.exceptions.APIError: {'errors': [{'domain': 'global', 'reason': 'insufficientPermissions', 'message': 'Insufficient Permission: Request had insufficient authentication scopes.'}], 'code': 403, 'message': 'Insufficient Permission: Request had insufficient authentication scopes.'}
here is the tutorial I'm using:
tutorial
it looks like it is an authentication issue. you should verify if this user has the right permissions to open this file.

Not able to run google sheet api from composer

I'm running a google sheet extract python api from compute engine and its works fine
and I'm running the same from the composer, but its not working
In composer, I'm login to the vm with the same user and running ssh command and I'm using the same service account in google sheet & vm
The following is the error message I'm getting
File "/home/utetwork_multiplier.py", line 15, in sheet_reade
result = service.spreadsheets().values().get(spreadsheetId=spreadsheet_id, range=range_name).execute(
File "/hsite-packages/googleapiclient/_helpers.py", line 130, in positional_wrappe
return wrapped(*args, **kwargs
File "/on3.5/site-packages/googleapiclient/http.py", line 849, in execut
raise HttpError(resp, content, uri=self.uri
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://sheets.googleapis.com/v4/spreadsheets/14r0cQ1RCXhLyd7i0VCrmddXOFiugFnfioRb6cYI_BWQ/values/Master%21A%3AJ?alt=json returned "Request had insufficient authentication scopes."
Traceback (most recent call last)
File "/usr/local/lib/airflow/airflow/contrib/operators/ssh_operator.py", line 164, in execut
.format(self.command, error_msg)
airflow.exceptions.AirflowException: error running cmd: set -e;cd src/digital_platform && ../../venvs/bdp/bin/python -m.marketing.adnetwork_multiplier, error: Traceback (most recent call last)
File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_mai
"__main__", mod_spec
File "/usr/lib/python3.5/runpy.py", line 85, in _run_cod
exec(code, run_globals
File "/home/tt/src/digital_platform//marketing/adnetwork_multiplier.py", line 74, in <module
main(
File "/home/tt/src/digital_platform//marketing/adnetwork_multiplier.py", line 28, in mai
data = sheet_reader(range_name
File "/home/tt/src/digital_platform//marketing/adnetwork_multiplier.py", line 15, in sheet_reade
result = service.spreadsheets().values().get(spreadsheetId=spreadsheet_id, range=range_name).execute(
File "/home//venvs/bdp/lib/python3.5/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrappe
return wrapped(*args, **kwargs
File "/home//venvs/bdp/lib/python3.5/site-packages/googleapiclient/http.py", line 849, in execut
raise HttpError(resp, content, uri=self.uri
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://sheets.googleapis.com/v4/spreadsheets/14r0cQ1RCXhLydcYI_BWQ/values/Master%21A%3AJ?alt=json returned "Request had insufficient authentication scopes."
During handling of the above exception, another exception occurred
Scopes
scope = [
'https://www.googleapis.com/auth/bigquery',
'https://www.googleapis.com/auth/spreadsheets.readonly',
'https://www.googleapis.com/auth/spreadsheets'
]
home = os.path.expanduser('~')
csf = os.path.join(home, '.client_secret.json')
token_filename = os.path.join(home, '.google_auth.dat')
flow = flow_from_clientsecrets(csf, scope=scope, message="%s is missing" % csf)
storage = Storage(token_filename)
credentials = storage.get()
if credentials is None or credentials.invalid:
flags = tools.argparser.parse_args(args=[])
flags.noauth_local_webserver = True
credentials = tools.run_flow(flow, storage, flags=flags)
From the error message you apear to be running Method: spreadsheets.values.get
That method requires one of the following scopes to access
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.readonly
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/spreadsheets
https://www.googleapis.com/auth/spreadsheets.readonly
The error message states
403 when requesting https://sheets.googleapis.com/v4/spreadsheets/14r0cQ1RCXhLydcYI_BWQ/values/Master%21A%3AJ?alt=json returned "Request had insufficient authentication scopes.
Which means that the user you have authenticated with was not authenticated with one of the required scopes above. I suggest that you log out the user delete the credentials for that user and run your code again making sure that when it pops up the consent screen it is asking for one of the required scopes. I suspect you are running your script with outdated grants from the user.

Instagram API access token generation throws "You must provide a client_id" error even though I provided one

Generating an access token for the Python Instagram API requires running this file and then entering a Client ID, Client Secret, Redirect URI, and Scope. The console then outputs a URL to follow to authorize the app and asks for the code generated afterwards. Theoretically after this process it should return an access token.
Instead, it's throwing an error:
Traceback (most recent call last):
File "get_access_token.py", line 40, in <module>
access_token = api.exchange_code_for_access_token(code)
File "C:\Users\Daniel Leybzon\Anaconda2\lib\site-packages\instagram\oauth2.py", line 48, in exchange_code_for_access_token
return req.exchange_for_access_token(code=code)
File "C:\Users\Daniel Leybzon\Anaconda2\lib\site-packages\instagram\oauth2.py", line 115, in exchange_for_access_token
raise OAuth2AuthExchangeError(parsed_content.get("error_message", ""))
instagram.oauth2.OAuth2AuthExchangeError: You must provide a client_id
Screenshot provided for context:

getUser return EDAMSystemException errorCode=8

I'm trying to get my account information by evernote api on python, however, I got errors :
Traceback (most recent call last): File "", line 1, in
File "build/bdist.linux-x86_64/egg/evernote/api/client.py", line 148, in delegate_method
File "build/bdist.linux-x86_64/egg/evernote/edam/userstore/UserStore.py", line 1033, in getUser
File "build/bdist.linux-x86_64/egg/evernote/edam/userstore/UserStore.py", line 1058, in recv_getUser
evernote.edam.error.ttypes.EDAMSystemException: EDAMSystemException(errorCode=8, rateLimitDuration=None, _message='authenticationToken')
My python code as below:
from evernote.api.client import EvernoteClient
dev_token="my develop token"
client = EvernoteClient(token=dev_token)
userStore = client.get_user_store()
user = userStore.getUser()
I'm sure I've generated a valid developer token for my Evernote account, as shown in the picture, I have a develop token in my account link
Is there anything I missed?
By the way, I use the code above and replace the key with another develop token generated by Evernote sandbox account, it's ok.
If you are not on sandbox, try:
client = EvernoteClient(token=dev_token, sandbox=False)

Categories