无法使用Google.auth.default凭据从部署的云功能读取/写入Google Sheets



我想使用Google Cloud Function使用Google Drive API读/写Google Sheet。我已经尝试将下面的代码部署为云功能(由Pub/Sub触发(:

def hello_pubsub(event, context):
from googleapiclient.discovery import build
import google.auth
credentials, project_id = google.auth.default(scopes=['https://www.googleapis.com/auth/spreadsheets'])
service = build('sheets', 'v4', credentials=credentials)
spreadsheet_id = 'my_spreadsheet_id_goes_here'
# Call the Sheets API
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=spreadsheet_id,range="A1:B4").execute()
values = result.get('values', [])
for value in values:
print(value)
range_ = 'A7'
request = service.spreadsheets().values().append(spreadsheetId=spreadsheet_id, range=range_, valueInputOption='RAW', insertDataOption='OVERWRITE', body={"values":[['test cell input']]})
response = request.execute()
print(response)
if __name__ == '__main__':
hello_pubsub('a', 'b')

我在谷歌云平台>日志记录中遇到了几个错误:使用oauth2client>=4.0时,file_cache不可用文件"/env/local/lib/python3.7/site packages/google_api_python_client-1.8.0-py3.7.eegg/googleapiclient/didiscovery_cache/init.py",第36行,在autodetect中从google.appengine.api导入memcache ModuleNotFoundError:没有名为"google.appegine"的模块

我可以从终端本地运行这个相同的脚本,它运行得很好。

任何帮助或想法都将不胜感激。再次,我想从云函数中读/写谷歌表单。这是我自己的谷歌工作表通过同一个谷歌云帐户运行。

为了回答我自己的问题,我错误地部署了云函数。为了让它正常工作,我必须把我的函数main.py、requirements.txt和service_account.json放在同一个文件夹中,然后从终端部署它。

我发现有两种读/写纸张的方法:

# METHOD 1 -- using googleapiclient.discovery.build
def hello_pubsub(event, context):
from googleapiclient.discovery import build
import google.auth
credentials, project_id = google.auth.default(scopes=['https://www.googleapis.com/auth/spreadsheets'])
service = build('sheets', 'v4', credentials=None)
spreadsheet_id = 'redacted'
# Call the Sheets API
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=spreadsheet_id,range="A1:B4").execute()
values = result.get('values', [])
# Print the values in the sheet in range A1:B4
for value in values:
print(value)
# Insert values into the sheet in cell A7
range_ = 'A7'
request = service.spreadsheets().values().append(
spreadsheetId=spreadsheet_id, 
range=range_, 
valueInputOption='RAW', 
insertDataOption='OVERWRITE', 
body={"values":[['This value gets inserted into the cell A7']]})
response = request.execute()
print(response)
# METHOD 2 -- using gspread (I prefer this because it's cleaner & does all I need)
import gspread
def hello_pubsub(event, context):
credentials_filepath = 'default_app_engine_service_account_key.json'
gc = gspread.service_account(filename=credentials_filepath)
sh = gc.open("the-title-of-your-sheet-goes-here")
# Print all values in the sheet
print(sh.sheet1.get_all_records())
sh.sheet1.append_row(['This value gets appended to the last row in the sheet'], 
value_input_option='RAW', 
insert_data_option=None, 
table_range=None)

注:使用部署main.py

gcloud functions deploy hello_pubsub --runtime python37 --trigger-topic test-topic

不要忘记你的requirements.txt文件

我认为您已经全局安装了依赖项并使用了它们。但是,当您在Cloud Function上部署时,您的本地上下文将丢失,所需的库也将丢失。

在具有此内容的函数的根目录中添加一个requirements.txt文件

google-api-python-client==1.8.2
google-auth==1.14.1

然后再次部署。

相关内容

  • 没有找到相关文章

最新更新