如何修复:项目中未使用 Google Sheets API

Sun*_*Lin 6 python google-api google-api-python-client google-sheets-api

我想构建一个问卷调查线聊天机器人并将答案传输到 Google 表格。\n这是我的代码:\n\'\'\'\nimport os

\n
from flask import Flask, request, abort\n\nfrom linebot import (\n    LineBotApi, WebhookHandler\n)\nfrom linebot.exceptions import (\n    InvalidSignatureError\n)\nfrom linebot.models import (\n    MessageEvent, TextMessage, TextSendMessage,\n)\nfrom oauth2client.service_account import ServiceAccountCredentials \nimport gspread\nfrom datetime import datetime, date, timedelta, time\nimport time\n\n\ngsp_scopes = [\'https://spreadsheets.google.com/feeds\']\n\nSPREAD_SHEETS_KEY = os.environ.get(\'SPREAD_SHEETS_KEY\')\ncredential_file_path = \'credentials.json\'\ndef auth_gsp_client(file_path, scopes):\n    credentials = ServiceAccountCredentials.from_json_keyfile_name(file_path, scopes)\n    return gspread.authorize(credentials)\n\ndef records(A, B, C, D, E):\n    gsp_client = auth_gsp_client(credential_file_path, gsp_scopes)\n    worksheet = gsp_client.open_by_key(SPREAD_SHEETS_KEY).sheet1\n    worksheet.insert_row([A, B, C, D, E], 2)\n    return True\n\napp = Flask(__name__)\n\nLINE_CHANNEL_ACCESS_TOKEN = os.environ.get(\'LINE_CHANNEL_ACCESS_TOKEN\')\nLINE_CHANNEL_SECRET = os.environ.get(\'LINE_CHANNEL_SECRET\')\nline_bot_api = LineBotApi(LINE_CHANNEL_ACCESS_TOKEN)\nhandler = WebhookHandler(LINE_CHANNEL_SECRET)\n\n@app.route("/", methods=[\'GET\'])\ndef hello():\n    return \'hello heroku\'\n\n@app.route("/callback", methods=[\'POST\'])\ndef callback():\n    signature = request.headers[\'X-Line-Signature\']\n\n    body = request.get_data(as_text=True)\n\n    try:\n        handler.handle(body, signature)\n    except InvalidSignatureError:\n        print("Invalid signature. Please check your channel access token/channel secret.")\n        abort(400)\n\n    return \'OK\'\n\nuser_command_dict = {}\n\n@handler.add(MessageEvent, message=TextMessage)\ndef handle_message(event):\n    user_message = event.message.text\n    user_id = event.source.user_id\n\n    user_command = user_command_dict.get(user_id)\n\n    if user_message == \'@\xe5\x95\x8f\xe5\x8d\xb7\' and user_command == None:\n        print(user_message)\n        reply_message = [\n            TextSendMessage(text=\'\xe9\x80\x99\xe6\x98\xaf\xe5\x95\x8f\xe5\x8d\xb7\'),\n            TextSendMessage(text=\'B\'),\n            TextSendMessage(text=\'\xe9\x96\x8b\xe5\xa7\x8b\')\n            ]\n        user_command_dict[user_id] = \'@\xe5\x95\x8f\xe5\x8d\xb71\'\n    if user_command == \'@\xe5\x95\x8f\xe5\x8d\xb71\':\n        answer = user_message\n        if answer==\'yes\':\n            time.sleep(3)\n            reply_message=TextSendMessage(text=\'\xe5\x95\x8f\xe9\xa1\x8c\xe4\xb8\x80\')\n            user_command_dict[user_id] = \'@\xe5\x95\x8f\xe5\x8d\xb72\'\n    if user_command == \'@\xe5\x95\x8f\xe5\x8d\xb72\':\n        global answer1\n        answer1 = user_message\n        time.sleep(3)\n        reply_message=TextSendMessage(text=\'\xe5\x95\x8f\xe9\xa1\x8c\xe4\xba\x8c\')\n        user_command_dict[user_id] = \'@\xe5\x95\x8f\xe5\x8d\xb73\'\n    if user_command == \'@\xe5\x95\x8f\xe5\x8d\xb73\':\n        global answer2\n        answer2 = user_message\n        time.sleep(3)\n        reply_message=TextSendMessage(text=\'\xe5\x95\x8f\xe9\xa1\x8c\xe4\xb8\x89\')\n        user_command_dict[user_id] = \'@\xe5\x95\x8f\xe5\x8d\xb74\'\n    if user_command == \'@\xe5\x95\x8f\xe5\x8d\xb74\':\n        global answer3\n        answer3 = user_message\n        Date = date.today()\n        today=Date.strftime("%Y/%b/%d")\n        time.sleep(3)\n        \n        print(today, answer1, answer2, answer3)\n        reply_message=TextSendMessage(text=\'\xe5\x95\x8f\xe9\xa1\x8c\xe7\xb5\x90\xe6\x9d\x9f\')\n        records(today, user_id, answer1, answer2, answer3)\n        user_command_dict[user_id] = None\n    #else:\n        #print(user_message)\n        #reply_message=TextSendMessage(text=event.message.text)\n\n    line_bot_api.reply_message(\n        event.reply_token,\n        reply_message)\n\nif __name__ == "__main__":\n    app.run()\n
Run Code Online (Sandbox Code Playgroud)\n

\'\'\'\n我把它推入heroku,但我得到

\n
\n

2021-08-10T06:00:56.303548+00:00 app[web.1]: gspread.exceptions.APIError: {\'code\': 403, \'message\': \'Google Sheets API 尚未使用在项目 10137149515 之前或者它被禁用。通过访问https://console.developers.google.com/apis/api/sheets.googleapis.com/overview?project=10137149515启用它,然后重试。如果您最近启用了此 API,请等待几分钟,以便该操作传播到我们的系统,然后重试。\', \'status\': \'PERMISSION_DENIED\', \'details\': [{\'@type\ ': \'type.googleapis.com/google.rpc.Help\', \'links\': [{\'description\': \'Google 开发者控制台 API 激活\', \'url\': \' https://console.developers.google.com/apis/api/sheets.googleapis.com/overview?project=10137149515\'}]}, {\'@type\': \'type.googleapis.com/google .rpc.ErrorInfo\'、\'原因\':\'SERVICE_DISABLED\'、\'域\':\'googleapis.com\'、\'元数据\':{\'消费者\':\'项目/ 10137149515\', \'服务\': \'sheets.googleapis.com\'}}]}\n2021-08-10T06:00:56.304235+00:00 应用程序[web.1]: 10.1.7.41 - - [ 10/8/2021:14:00:56 +0800]“POST /callback HTTP/1.1”500 290“-”“LineBotWebhook/2.0”

\n
\n

请帮助并告诉我出了什么问题。谢谢

\n

DaI*_*mTo 9

Google Sheets API 之前未曾在项目 10137149515 中使用或被禁用。通过访问启用它

如果您点击项目的链接,这是您的 Google 云控制台帐户中的设置问题当您设置项目 时,您需要告诉 Google 您打算使用哪些 API 您忘记添加您将使用 Google Sheets API 。

启用谷歌表格API

去左边的图书馆

在此输入图像描述

在搜索栏中搜索 Google Sheets api 并选择它,然后单击启用

在此输入图像描述

再次运行代码应该只需要几分钟。