Hi I am high school senior and I want to do a small program for a school project.
I need to get information from a google sheet into my code. I followed this guide to try to do this. I followed every step and put in all my info properly, but I keep getting this error.
Traceback (most recent call last):
File "C:/Users/Conner Boggan/Desktop/Senior Project/Python Code/Google Sheets/Sheets.py", line 11, in
sheet = client.open("tpol2").sheet1 # Open the spreadhseet
File "C:\ProgramData\Anaconda3\envs\Android app\lib\site-packages\gspread\client.py", line 119, in open
self.list_spreadsheet_files(title),
File "C:\ProgramData\Anaconda3\envs\Android app\lib\site-packages\gspread\client.py", line 95, in list_spreadsheet_files
res = self.request('get', url, params=params).json()
File "C:\ProgramData\Anaconda3\envs\Android app\lib\site-packages\gspread\client.py", line 67, in request
headers=headers,
File "C:\ProgramData\Anaconda3\envs\Android app\lib\site-packages\requests\sessions.py", line 543, in get
return self.request('GET', url, **kwargs)
File "C:\ProgramData\Anaconda3\envs\Android app\lib\site-packages\google\auth\transport\requests.py", line 440, in request
self.credentials.before_request(auth_request, method, url, request_headers)
File "C:\ProgramData\Anaconda3\envs\Android app\lib\site-packages\google\auth\credentials.py", line 124, in before_request
self.refresh(request)
File "C:\ProgramData\Anaconda3\envs\Android app\lib\site-packages\google\oauth2\service_account.py", line 334, in refresh
access_token, expiry, _ = _client.jwt_grant(request, self._token_uri, assertion)
File "C:\ProgramData\Anaconda3\envs\Android app\lib\site-packages\google\oauth2_client.py", line 153, in jwt_grant
response_data = _token_endpoint_request(request, token_uri, body)
File "C:\ProgramData\Anaconda3\envs\Android app\lib\site-packages\google\oauth2_client.py", line 124, in _token_endpoint_request
_handle_error_response(response_body)
File "C:\ProgramData\Anaconda3\envs\Android app\lib\site-packages\google\oauth2_client.py", line 60, in _handle_error_response
raise exceptions.RefreshError(error_details, response_body)
google.auth.exceptions.RefreshError: ('invalid_grant: Invalid JWT: Token must be a short-lived token (60 minutes) and in a reasonable timeframe. Check your iat and exp values in the JWT claim.', '{"error":"invalid_grant","error_description":"Invalid JWT: Token must be a short-lived token (60 minutes) and in a reasonable timeframe. Check your iat and exp values in the JWT claim."}')
This is my code for reference:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from pprint import pprint
scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name("TPOL2.json", scope)
client = gspread.authorize(creds)
sheet = client.open("tpol2").sheet1 # Open the spreadhseet
data = sheet.get_all_records() # Get a list of all records
print(data)
I can run each line except the last 3 which leads me to believe the code is able to get online properly, but there is something in the way between the code and the google sheet.
Any help would very much appreciated.
Related
Hi i am updating Google sheet by using google service account,
google drive api and google sheet api.
initially code run succesfully but aftet some time
returning errno110 retry exceeded
My code is
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import json
import pandas as pd
scopes = [
'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive'
]
credentials
=ServiceAccountCredentials.from_json_keyfile_name
("client_secret1.json"
, scopes)
file = gspread.authorize(credentials) # authenticate the JSON key
with gspread
wb = file.open("gw_data")
sh = wb.worksheet('dt_trial')
df = pd.DataFrame([[1, 2, 3, 4, 5]],columns = ['Date', 'Time',
'LTP' , 'Ce', 'Pe'])
sh.update([df.columns.values.tolist()] + df.values.tolist())
PLZZ help me i am new at coding
Error
/
File
"/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.9/site-packages/gspread/client.py",
line 121, in open
self.list_spreadsheet_files(title, folder_id), File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.9/site-packages/gspread/client.py",
line 96, in list_spreadsheet_files
res = self.request("get", url, params=params).json() File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.9/site-packages/gspread/client.py",
line 59, in request
response = getattr(self.session, method)( File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.9/site-packages/requests/sessions.py",
line 555, in get
return self.request('GET', url, **kwargs) File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.9/site-packages/google/auth/transport/requests.py",
line 476, in request
self.credentials.before_request(auth_request, method, url, request_headers) File
"/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.9/site-packages/google/auth/credentials.py",
line 133, in before_request
self.refresh(request) File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.9/site-packages/google/oauth2/service_account.py",
line 410, in refresh
access_token, expiry, _ = _client.jwt_grant( File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.9/site-packages/google/oauth2/_client.py",
line 193, in jwt_grant
response_data = _token_endpoint_request(request, token_uri, body) File
"/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.9/site-packages/google/oauth2/_client.py",
line 161, in _token_endpoint_request
response_status_ok, response_data = _token_endpoint_request_no_throw( File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.9/site-packages/google/oauth2/_client.py",
line 115, in _token_endpoint_request_no_throw
response = request(method="POST", url=token_uri, headers=headers, body=body) File
"/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.9/site-packages/google/auth/transport/requests.py",
line 189, in call
six.raise_from(new_exc, caught_exc) File "", line 3, in raise_from google.auth.exceptions.TransportError:
HTTPSConnectionPool(host='oauth2.googleapis.com', port=443): Max
retries exceeded with url: /token (Caused by
NewConnectionError('<urllib3.connection.HTTPSConnection object at
0x7f68f5d0a0>: Failed to establish a new connection: [Errno 110]
Connection timed out'))
ok i found solution
Actually i was running this code from android mobile IDE
and in my google sheet setting transfer files only over wi-fi was enabled. And i havent connected to wifi using net through sim card.
So I think bot was trying to transfer data but wifi setting not allowing it so time out error was generated.
Now i changed the setting and code is running smoothly
Recently, I started a PRAW project aiming to scrape from the r/todayilearned subreddit. Browsing through the docs, if found that the best way to load up the client-id, client secret, username, and password was to store it in the praw.ini file.
This is the format I used where the ".........." were filled by the respective inputs.
[TIL]
client_id="´............"
client_secret="............"
password="............"
username=".........."
user_agent="TIL by u/........"
I executed this code and I get
import praw
reddit = praw.Reddit("TIL")
subreddit = reddit.subreddit('learnpython')
Traceback (most recent call last):
File "C:\Users\HP\Desktop\python\TIL\src\main.py", line 7, in <module>
for submission in subreddit.get_hot():
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\praw\models\reddit\base.py", line 34, in __getattr__
self._fetch()
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\praw\models\reddit\subreddit.py", line 584, in _fetch
data = self._fetch_data()
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\praw\models\reddit\subreddit.py", line 581, in _fetch_data
return self._reddit.request("GET", path, params)
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\praw\reddit.py", line 849, in request
return self._core.request(
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\prawcore\sessions.py", line 328, in request
return self._request_with_retries(
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\prawcore\sessions.py", line 226, in _request_with_retries
response, saved_exception = self._make_request(
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\prawcore\sessions.py", line 183, in _make_request
response = self._rate_limiter.call(
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\prawcore\rate_limit.py", line 33, in call
kwargs["headers"] = set_header_callback()
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\prawcore\sessions.py", line 281, in _set_header_callback
self._authorizer.refresh()
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\prawcore\auth.py", line 379, in refresh
self._request_token(
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\prawcore\auth.py", line 155, in _request_token
response = self._authenticator._post(url, **data)
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\prawcore\auth.py", line 38, in _post
raise ResponseException(response)
prawcore.exceptions.ResponseException: received 401 HTTP response
But, when I do this, It works.
import praw
reddit = praw.Reddit(
client_id="´............",
client_secret="............",
password="............",
username="..........",
user_agent="TIL by u/........"
)
subreddit = reddit.subreddit('learnpython')
How can I fix this?
Thanks in Advance.
I tested it and it has to be without " "
[TIL]
client_id=2Ca......Mh4
client_secret=Bq7............X0z
password=SeCrEtPaSsWoRd
username=james_bond
user_agent=TIL by u/james_bond
but it can use spaces to make it more readable
[TIL]
client_id = 2Ca......Mh4
client_secret = Bq7............X0z
password = SeCrEtPaSsWoRd
username = james_bond
user_agent = TIL by u/james_bond
BTW:
It may also use : instead of =
[TIL]
client_id:2Ca......Mh4
client_secret:Bq7............X0z
password:SeCrEtPaSsWoRd
username:james_bond
user_agent:TIL by u/james_bond
[TIL]
client_id : 2Ca......Mh4
client_secret : Bq7............X0z
password : SeCrEtPaSsWoRd
username : james_bond
user_agent : TIL by u/james_bond
EDIT:
I checked documentation praw.ini Files and it shows examples also without " "
I am building a python client-side application that uses Firestore. I have successfully used Google Identity Platform to sign up and sign in to the Firebase project, and created a working Firestore client using google.cloud.firestore.Client which is authenticated as a user:
import json
import requests
import google.oauth2.credentials
from google.cloud import firestore
request_url = f"https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key={self.__api_key}"
headers = {"Content-Type": "application/json; charset=UTF-8"}
data = json.dumps({"email": self.__email, "password": self.__password, "returnSecureToken": True})
response = requests.post(request_url, headers=headers, data=data)
try:
response.raise_for_status()
except (HTTPError, Exception):
content = response.json()
error = f"error: {content['error']['message']}"
raise AuthError(error)
json_response = response.json()
self.__token = json_response["idToken"]
self.__refresh_token = json_response["refreshToken"]
credentials = google.oauth2.credentials.Credentials(self.__token,
self.__refresh_token,
client_id="",
client_secret="",
token_uri=f"https://securetoken.googleapis.com/v1/token?key={self.__api_key}"
)
self.__db = firestore.Client(self.__project_id, credentials)
I have the problem, however, that when the token has expired, I get the following error:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/google/api_core/grpc_helpers.py", line 57, in error_remapped_callable
return callable_(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/grpc/_channel.py", line 826, in __call__
return _end_unary_response_blocking(state, call, False, None)
File "/usr/local/lib/python3.7/dist-packages/grpc/_channel.py", line 729, in _end_unary_response_blocking
raise _InactiveRpcError(state)
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
status = StatusCode.UNAUTHENTICATED
details = "Missing or invalid authentication."
debug_error_string = "{"created":"#1613043524.699081937","description":"Error received from peer ipv4:172.217.16.74:443","file":"src/core/lib/surface/call.cc","file_line":1055,"grpc_message":"Missing or invalid authentication.","grpc_status":16}"
>
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/home/my_app/src/controllers/im_alive.py", line 20, in run
self.__device_api.set_last_updated(utils.device_id())
File "/home/my_app/src/api/firestore/firestore_device_api.py", line 21, in set_last_updated
"lastUpdatedTime": self.__firestore.SERVER_TIMESTAMP
File "/home/my_app/src/api/firestore/firestore.py", line 100, in update
ref.update(data)
File "/usr/local/lib/python3.7/dist-packages/google/cloud/firestore_v1/document.py", line 382, in update
write_results = batch.commit()
File "/usr/local/lib/python3.7/dist-packages/google/cloud/firestore_v1/batch.py", line 147, in commit
metadata=self._client._rpc_metadata,
File "/usr/local/lib/python3.7/dist-packages/google/cloud/firestore_v1/gapic/firestore_client.py", line 1121, in commit
request, retry=retry, timeout=timeout, metadata=metadata
File "/usr/local/lib/python3.7/dist-packages/google/api_core/gapic_v1/method.py", line 145, in __call__
return wrapped_func(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/google/api_core/retry.py", line 286, in retry_wrapped_func
on_error=on_error,
File "/usr/local/lib/python3.7/dist-packages/google/api_core/retry.py", line 184, in retry_target
return target()
File "/usr/local/lib/python3.7/dist-packages/google/api_core/timeout.py", line 214, in func_with_timeout
return func(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/google/api_core/grpc_helpers.py", line 59, in error_remapped_callable
six.raise_from(exceptions.from_grpc_error(exc), exc)
File "<string>", line 3, in raise_from
google.api_core.exceptions.Unauthenticated: 401 Missing or invalid authentication.
I have tried omitting the token and only specifying the refresh token, and then calling credentials.refresh(), but the expires_in in the response from the https://securetoken.googleapis.com/v1/token endpoint is a string instead of a number (docs here), which makes _parse_expiry(response_data) in google.oauth2._client.py:257 raise an exception.
Is there any way to use the firestore.Client from either google.cloud or firebase_admin and have it automatically handle refreshing tokens, or do I need to switch to the manually calling the Firestore RPC API and refreshing tokens at the correct time?
Note: There are no users interacting with the python app, so the solution must not require user interaction.
Can't you just pass the string cast as integer _parse_expiry(int(float(response_data))) ?
If it is not working you could try to make a call and refresh token after getting and error 401, see my answer for the general idea on how to handle tokens.
As mentioned by #Marco, it is recommended that you use a service account if it's going to be used in an environment without user. When you use service account, you can just set GOOGLE_APPLICATION_CREDENTIALS environment variable to location of service account json file and just instantiate the firestore Client without any credentials (The credentials will be picked up automatically):
import firestore
client = firestore.Client()
and run it as (assuming Linux):
$ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json
$ python file.py
Still, if you really want to use user credentials for the script, you can install the Google Cloud SDK, then:
$ gcloud auth application-default login
This will open browser and for you to select account and login. After logging in, it creates a "virtual" service account file corresponding to your user account (that will also be loaded automatically by clients). Here too, you don't need to pass any parameters to your client.
See also: Difference between “gcloud auth application-default login” and “gcloud auth login”
I'm trying to access google cloud storage bucket from cloud functions (python) instance and it's throwing mystic 500 error.
I have given the service account editor role too. It didn't make any change.
I also checked if any of the quota is going off limit. The limits were not even close.
Please, anyone can help me find cause of this error?
here is the code
from google.cloud import storage
import os
import base64
storage_client = storage.Client()
def init_analysis(event, context):
print("event", event)
pubsub_message = base64.b64decode(event['data']).decode('utf-8')
print(pubsub_message)
bucket_name = 'my-bucket'
bucket = storage_client.get_bucket(bucket_name)
blobs = bucket.list_blobs()
for blob in blobs:
print(blob.name)
Error:
Traceback (most recent call last): File "/env/local/lib/python3.7/site-packages/google/auth/compute_engine/credentials.py", line 99, in refresh service_account=self._service_account_email) File "/env/local/lib/python3.7/site-packages/google/auth/compute_engine/_metadata.py", line 208, in get_service_account_token 'instance/service-accounts/{0}/token'.format(service_account)) File "/env/local/lib/python3.7/site-packages/google/auth/compute_engine/_metadata.py", line 140, in get url, response.status, response.data), response) google.auth.exceptions.TransportError: ("Failed to retrieve http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/my-project#appspot.gserviceaccount.com/token from the Google Compute Enginemetadata service. Status: 500 Response:\nb'Could not fetch URI /computeMetadata/v1/instance/service-accounts/my-project#appspot.gserviceaccount.com/token\\n'", <google.auth.transport.requests._Response object at 0x2b0ef9edf438>) The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 383, in run_background_function _function_handler.invoke_user_function(event_object) File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 217, in invoke_user_function return call_user_function(request_or_event) File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 214, in call_user_function event_context.Context(**request_or_event.context)) File "/user_code/main.py", line 21, in init_analysis bucket = storage_client.get_bucket(bucket_name) File "/env/local/lib/python3.7/site-packages/google/cloud/storage/client.py", line 227, in get_bucket bucket.reload(client=self) File "/env/local/lib/python3.7/site-packages/google/cloud/storage/_helpers.py", line 130, in reload _target_object=self, File "/env/local/lib/python3.7/site-packages/google/cloud/_http.py", line 315, in api_request target_object=_target_object, File "/env/local/lib/python3.7/site-packages/google/cloud/_http.py", line 192, in _make_request return self._do_request(method, url, headers, data, target_object) File "/env/local/lib/python3.7/site-packages/google/cloud/_http.py", line 221, in _do_request return self.http.request(url=url, method=method, headers=headers, data=data) File "/env/local/lib/python3.7/site-packages/google/auth/transport/requests.py", line 205, in request self._auth_request, method, url, request_headers) File "/env/local/lib/python3.7/site-packages/google/auth/credentials.py", line 122, in before_request self.refresh(request) File "/env/local/lib/python3.7/site-packages/google/auth/compute_engine/credentials.py", line 102, in refresh six.raise_from(new_exc, caught_exc) File "<string>", line 3, in raise_from google.auth.exceptions.RefreshError: ("Failed to retrieve http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/my-project#appspot.gserviceaccount.com/token from the Google Compute Enginemetadata service. Status: 500 Response:\nb'Could not fetch URI /computeMetadata/v1/instance/service-accounts/my-project#appspot.gserviceaccount.com/token\\n'", <google.auth.transport.requests._Response object at 0x2b0ef9edf438>)
google.auth.exceptions.TransportError: ("Failed to retrieve http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/my-project#appspot.gserviceaccount.com/token from the Google Compute Enginemetadata service. Status: 500 Response:\nb'Could not fetch URI /computeMetadata/v1/instance/service-accounts/my-project#appspot.gserviceaccount.com/token\\n'"
The error you are getting it's because your Cloud Functions service account doesn't have the cloudfunctions.serviceAgent role. As you can see on the documentation:
Authenticating as the runtime service account from inside your function may fail if you change the Cloud Functions service account's permissions.
However, I found that sometimes you can not add this role as it doesn't show up as an option. I have reported this issue to the Google Cloud Functions engineering team and they are working to solve it.
Nevertheless, you can add the role again using this gcloud command:
gcloud projects add-iam-policy-binding <project_name> --role=roles/cloudfunctions.serviceAgent --member=serviceAccount:service-<project_number>#gcf-admin-robot.iam.gserviceaccount.com
Im making a reddit bot that replies something to a specific comment.
But Im getting this error : invalid_grant error processing request
and I can't find the solution.
here is my code, Im using Python.
import praw
import time
import config
def login():
r = praw.Reddit(user_agent = "test bot",
username = config.username,
password = config.password,
client_id = config.client_id,
client_secret = config.client_secret)
print("logged in")
return r
cache = []
def run_bot(r):
subreddit = r.subreddit("Test")
comments = subreddit.get_comments(limit=25)
for comment in comments:
comment_text = comment.body.lower()
if "xD" in comment_text and comment.id not in cache:
comment.reply("xD")
cache.append(comment.id)
while True:
r = login()
run_bot(r)
time.sleep(5)
traceback:
logged in
Traceback (most recent call last):
File "xdbot.py", line 28, in <module>
run_bot(r)
File "xdbot.py", line 19, in run_bot
comments = subreddit.get_comments(limit=25)
File "D:\Programming\Phyton\lib\site-packages\praw\models\reddit\base.py", line 31, in __getattr__
self._fetch()
File "D:\Programming\Phyton\lib\site-packages\praw\models\reddit\base.py", line 66, in _fetch
params=self._info_params)
File "D:\Programming\Phyton\lib\site-packages\praw\reddit.py", line 367, in get
data = self.request('GET', path, params=params)
File "D:\Programming\Phyton\lib\site-packages\praw\reddit.py", line 451, in request
params=params)
File "D:\Programming\Phyton\lib\site-packages\prawcore\sessions.py", line 174, in request
params=params, url=url)
File "D:\Programming\Phyton\lib\site-packages\prawcore\sessions.py", line 108, in _request_with_retries
data, files, json, method, params, retries, url)
File "D:\Programming\Phyton\lib\site-packages\prawcore\sessions.py", line 93, in _make_request
params=params)
File "D:\Programming\Phyton\lib\site-packages\prawcore\rate_limit.py", line 32, in call
kwargs['headers'] = set_header_callback()
File "D:\Programming\Phyton\lib\site-packages\prawcore\sessions.py", line 134, in _set_header_callback
self._authorizer.refresh()
File "D:\Programming\Phyton\lib\site-packages\prawcore\auth.py", line 328, in refresh
password=self._password)
File "D:\Programming\Phyton\lib\site-packages\prawcore\auth.py", line 142, in _request_token
payload.get('error_description'))
prawcore.exceptions.OAuthException: invalid_grant error processing request
Double check your credentials as this note says.
Remember that the username is your reddit's account name, not the bot's name.
Another possibility is your bot has been timed out for too many login attempts.
An easy way to check this by trying to log into Reddit manually and checking if you're blocked.
You might be using two-factor authentication with this account.
You have to disable it, or pass the 2FA code with the password in one stiring separated by a colon, like this:
r = praw.Reddit(user_agent = "test bot",
username = config.username,
password = f"{config.password}:{2FA-Code}",
client_id = config.client_id,
client_secret = config.client_secret)
See this in the docs:
https://praw.readthedocs.io/en/latest/getting_started/authentication.html#two-factor-authentication
Also, your code won't work because you use the "lower" command and there's an uppercase letter in the string you compare.