Python Google Sheet API:googleapiclient.errors.UnknownApiNam



我写了一些.py文件来访问和写入我的谷歌工作表上的数据,并使用pyinstaller将这些文件打包到.exe中。然而,在我将.py打包到.exe后,我再也无法执行它了。

它不断显示这个错误:

File "GoogleSheet.py", line 9, in __init__
File "Google_Process.py", line 31, in __init__
File "googleapiclient_helpers.py", line 130, in positional_wrapper
File "googleapiclientdiscovery.py", line 287, in build
File "googleapiclientdiscovery.py", line 404, in _retrieve_discovery_doc
googleapiclient.errors.UnknownApiNameOrVersion: name: sheets  version: v4

我尝试过这种方法:https://github.com/nithinmurali/pygsheets/issues/490

但我不知道如何修改我的代码,也不知道这个方法是否可以修复我的错误。

这是我的。py:

from Google_Process import GoogleAPIClient
import pandas as pd
class GoogleSheets(GoogleAPIClient):
def __init__(self, status = 'DONE', order_number = '') -> None:
self.status = status
self.order_number = order_number
# Call GoogleAPIClient.__init__(),give serviceName, version, scope
super().__init__(
'sheets',
'v4',
['https://www.googleapis.com/auth/spreadsheets'],
)
def appendWorksheet(self):
if self.status == 'DONE':
df=pd.DataFrame(
{'Order': [self.order_number],
'Status': ['DONE'],
'Mark': ['-']}
)
self.googleAPIService.spreadsheets().values().append(
spreadsheetId='<ID>',
range='sheet1',
valueInputOption='USER_ENTERED',
body={
'majorDimension': 'ROWS',
'values': df.values.tolist()
},
).execute()
return

这是另一个文件:

import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
class GoogleAPIClient:
SECRET_PATH = '.\PackingElf\.credentials\client_secrets_file.json'
CREDS_PATH = '.\PackingElf\.credentials\cred.json'

def __init__(self, serviceName: str, version: str, scopes: list) -> None:
self.creds = None
# The file client_secret.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists(self.CREDS_PATH):
self.creds = Credentials.from_authorized_user_file(self.CREDS_PATH, scopes)
# If there are no (valid) credentials available, let the user log in.
if not self.creds or not self.creds.valid:
if self.creds and self.creds.expired and self.creds.refresh_token:
self.creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
self.SECRET_PATH, scopes)
self.creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open(self.CREDS_PATH, 'w') as token:
token.write(self.creds.to_json())

self.googleAPIService = build(serviceName, version, credentials=self.creds)
if __name__ == '__main__':
googleSheetAPI = GoogleAPIClient(
'sheets',
'v4',
['https://www.googleapis.com/auth/spreadsheets'],
)
print(googleSheetAPI.googleAPIService)

有什么办法解决这个问题吗?请

我使用vscode作为IDE

我刚刚在创建一个读取和写入谷歌电子表格的库存应用程序时遇到了这个问题。我尝试了大约4种不同的解决方案,我在网上看到,但没有成功。我将把你重定向到github上的这个线程。这似乎是一个持续的问题。

相关内容

最新更新