google api 字典没有授权属性

google api dict has no attribute authorize

Traceback (most recent call last):
  File "DarthyBot.py", line 49, in <module>
    service = build('sheets', 'v4', credentials=credentials)
  File "/usr/local/lib/python3.5/dist-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/googleapiclient/discovery.py", line 288, in build
    adc_key_path=adc_key_path,
  File "/usr/local/lib/python3.5/dist-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/googleapiclient/discovery.py", line 540, in build_from_document
    http = _auth.authorized_http(credentials)
  File "/usr/local/lib/python3.5/dist-packages/googleapiclient/_auth.py", line 119, in authorized_http
    return credentials.authorize(build_http())
AttributeError: 'dict' object has no attribute 'authorize' 

尝试使用 python 编写的 discord 机器人从 google 表格文档中获取一些信息并将其 post 用于聊天。我正在尝试使用 googlepapi 和 gspread,但出了点问题,我没有任何线索。

我的代码如下(post编辑了必须处理表格内容的块,省略了我的 google 服务帐户 api 键等)

import discord
import os
import requests
import json
import random
from jokeapi import Jokes
import gspread
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

creedentials={has the appropriate stuff in it}

sh_id = "order_order_and_progress"
gc = gspread.service_account_from_dict(creedentials)
sh = gc.open(sh_id)
service = build('sheets', 'v4', credentials=creedentials)
sheet = service.spreadsheets()

def get_progress(rescue_ranges, sh_id):
  result_shit = sheet.values().get(spreadsheetId=sh_id, range=rescue_ranges).execute()
  value_dental = result_shit.get('values', [])
  return(value_dental)

还有一个问题,但还没到那一步

if message.content.startswith('$progress'):
    beans = get_gif("working")
    beaned = beans["results"][dummy]["url"]
#    listed = get_progress(rescue_ranges, sh_id)
    await (message.channel.send(TERG + ", Here\'s how things are looking \nhttp://alinktosomewhere.neat \n" + beaned))

关于its saying its this line service = build('sheets', 'v4', credentials=creedentials),当我看到你的脚本时,service = build('sheets', 'v4', credentials=creedentials)creedentialscreedentials={has the appropriate stuff in it}。如果{has the appropriate stuff in it}是服务帐号的JSON数据,包括private_keyclient_email等,很遗憾,该值不能直接用于service = build('sheets', 'v4', credentials=creedentials)。我认为这就是您遇到问题的原因。

当你想用服务账号使用service = build('sheets', 'v4', credentials=creedentials)并且creedentials={has the appropriate stuff in it}是服务账号的JSON数据包括private_key,client_email和等等,下面的修改怎么样?在这种情况下,请同时设置范围。

修改后的脚本 1:

from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient.discovery import build

creedentials = {has the appropriate stuff in it}

credentials = ServiceAccountCredentials.from_json_keyfile_dict(creedentials, scopes=['https://www.googleapis.com/auth/spreadsheets'])
service = build('sheets', 'v4', credentials=credentials)
sheet = service.spreadsheets()

修改脚本 2:

from google.oauth2 import service_account
from googleapiclient.discovery import build

creedentials = {has the appropriate stuff in it}

credentials = service_account.Credentials.from_service_account_info(creedentials, scopes=['https://www.googleapis.com/auth/spreadsheets'])
service = build('sheets', 'v4', credentials=credentials)

注:

  • 您可以使用这两个样本。所以请选择其中之一。
  • 在此修改中,它是一个简单的示例脚本,用于使用 service = build('sheets', 'v4', credentials=credentials) 和服务帐户的 JSON 数据,包括 private_keyclient_email 等。所以请根据自己的实际情况修改。
  • 并且,此示例脚本使用 https://www.googleapis.com/auth/spreadsheets 的范围。如果您想使用其他范围,请添加它们。

参考文献: