I want to resolve errors that Python 3 may have due to server time and current time differences.

Asked 2 years ago, Updated 2 years ago, 32 views

Error Message Occurring

Traceback (most recent call last):
  File"/Users/username/flilhack/\u540d\u79f0\u672a\u8a2d\u5b9a\u30d5\u30a9\u30bf\u3099/tes.py", line 27, in<module>
    print(get_spreadsheet_data())
  File"/Users/username/flilhack/\u540d\u79f0\u672a\u8a2d\u5b9a\u30d5\u30a9\u30bf\u3099/tes.py", line 23, input_spreadsheet_data
    worksheet=gfile.get_worksheet(0)
  File"/Users/username/anaconda3/lib/python3.6/site-packages/gspread/v4/models.py", line138, inget_worksheet
    sheet_data=self.fetch_sheet_metadata()
  File"/Users/username/anaconda3/lib/python3.6/site-packages/gspread/v4/models.py", line 120, infetch_sheet_metadata
    r=self.client.request('get', url, params=params)
  File"/Users/username/anaconda3/lib/python3.6/site-packages/gspread/v4/client.py", line67, in request
    endpoint, json=json, params=params, data=data, files=files
  File"/Users/username/anaconda3/lib/python 3.6/site-packages/requests/sessions.py", line 521, inget
    return self.request ('GET', url, **kwargs)
  File"/Users/username/anaconda3/lib/python 3.6/site-packages/requests/sessions.py", line 508, in request
    resp=self.send(prep,**send_kwargs)
  File"/Users/username/anaconda3/lib/python 3.6/site-packages/requests/sessions.py", line 618, in send
    r=adapter.send(request,**kwargs)
  File"/Users/username/anaconda3/lib/python 3.6/site-packages/requests/adapters.py", line 407, in send
    self.cert_verify(conn, request.url, verify, cert)
  File"/Users/username/anaconda3/lib/python 3.6/site-packages/requests/adapters.py", line 226, insert_verify
    "invalid path: {0}".format(cert_loc))
OSError: Could not find a sustainable TLS CA certificate bundle, invalid path: /Users/username/anaconda3/lib/python3.6/site-packages/certifi/cacert.pem
Finished in 0.597s
tes.py26:1
LFUTF-8 Python 0 files 3 updates

oauth2client.client.HttpAccessTokenRefreshError

Affected Source Code (Python 3)

import gspread
from oauth2client.service_account import ServiceAccountCredentials

default_spreadsheet_data():
    doc_id = '[spreadsheet URL id ]'
    json_key_path='/Users/username/Downloads/My Project-a5bcde8be455.json'
    scope=['https://spreadsheets.google.com/feeds']
    credentials = ServiceAccountCredentials.from_json_keyfile_name(json_key_path, scope)
    gclient=gspread.authorize (credentials)
    gfile=gclient.open_by_key(doc_id)
    worksheet=gfile.get_worksheet(0)
    records=wsheet.get_all_records(head=1)
    return records

print(get_spreadsheet_data())

Tried

When I checked for similar errors, I found that it was caused by a time shift in the headquarters of the stack overflow.I am currently using Mac 10.13.3, and I set it automatically from the date and time settings. I tried manual settings, but it didn't work.

Environment

  • OS Mac 10.13.3
  • Python3
  • Atom

python

2022-09-30 16:27

1 Answers

I don't have time to write, but please refer to this as well.
https://stackoverflow.com/questions/49356116/mac-py2app-not-finding-cacert-pem
This is a bug in recently fixedrequets, but it has not been fixed in the released version yet.If you are OK with using the development version of requests, you can install it from github repo.

This was a bug in requests that was frequently fixed but not made it to a release version yet.If you are ok with using a development version of requests, you can install from the github repo (upgrading if already present) so when you build your own version:

 pip install-U https://github.com/requests/requests/zipball/master

Also
https://stackoverflow.com/questions/46119901/python-requests-cant-find-a-folder-with-a-certificate-when-converted-to-exe

I ran into this problem as well. It looks like it comes from the certificate bundle cacert.pem not being included in the requests package directory when the program is compiled. The requests module uses the function certifi.core.where to determine the location of cacert.pem. Overriding this function and overriding the variables set by this function seems to fix the problem.

You added this code at the beginning of your program.

I added this code to the starting of my program:

import sys,os


def override_where():
    """ "Overrides certifi.core.where to return actual location of cacert.pem ""
    # change this to match the location of cacert.pem
    returnos.path.abspath("cacert.pem")


# Is the program compiled?
if hasattr(sys, "frozen"):
    import certifi.core

    os.environ ["REQUESTS_CA_BUNDLE"] = override_where()
    certifi.core.where=override_where

    # delay importing until after where() has been replaced
    import requests.utils
    import requests.adaptors
    # replace these variables in case these modules were
    # imported before we replaced certifi.core.where
    requests.utils.DEFAULT_CA_BUNDLE_PATH=override_where()
    requests.adaptors.DEFAULT_CA_BUNDLE_PATH=override_where()

(Previous Post ↓)
In Japanese, [Python] It seems that troubleshooting until you access Google Spreadsheet with gspread is good.

I will update the details again, but I hope it will be resolved.

Everyone says that Access Token and Refresh Token giving invalid grant in Google Plus in Python? has a solution.I don't know because I've never used it, but I guess it's because it seems like a lot of people are saying it.
 There were other answers, but it had a credit rating of 10, so I pulled it.
Do you have anything in mind?

Invalid grant error has two common causes.
US>1. This is because your server's clock does not progress simultaneously with NTP.
(Solution: Check the server's time and fix any discrepancies.)
2. refresh token limit has been exceeded.
(Solution: There's nothing you can do.You cannot have more refresh token for use.)
 Applications can request a variety of refresh token.For example, this works when a user wants to install one application on a variety of machines.In this case, two refresh tokens are required, but each one is used for installation. If the number of refresh tokens exceeds that limit, the old token is disabled.If the application tries to use a disabled refresh token, it receives a certain invalid_granterror.The limit for each single pair, OAuth2.0 clients, is 25refresh tokens.(Keep in mind that this limitation is variable.) If the application requests refresh tokens for the same Client/Account pair, the 26th token will be invalidated.The 27th token is thus the second previously disabled token.
 

Invalid_grant error has two common causes.
1.Your server's clock is not in sync with NTP.
2.The refresh token limit has been exceeded. (Solution: Nothing you can do they cant have more refresh tokens in use) Applications can request multiple refresh tokens. For example, this is useful in situations where a user wants to install an application on multiple machines. In this case, two refresh tokens are required, one for each installation. When the number of refresh tokens exceeds the limit, older tokens become invalid. If the application attempts to use an invalidated refresh token, an invalid_grant error response is returned. The limit for each unique pair of OAuth 2.0 client and is 25 refresh tokens (note that this limit is subject to change). If the application continues to request refresh tokens for the same Client/Account pair, once the 26th token is issued, the 1st refresh token that was previously issued will become invalid.The 27th requested refresh token would validate the 2nd previously issued token and son.

And how can I match it with NTP?I looked into it at home and found out that
 Synchronize the server clock to an NTP server and go to https://stackoverflow.com/questions/21791193/php-microtime-drift-external-ntp-service/2He said he wrote the answer with 1805579#21805579.
php seems to be the original field, but I thought it would be nice if there was a common solution, so I posted it.
I hope it will be helpful.
 
addition:
I think you've already seen it, but it's an explanatory website for adjusting the date and time.
Set Date and Time
or
Use a public NTP server to time your PC

I want to set all the clocks


2022-09-30 16:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.