helmet648
helmet648

Reputation: 53

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' 

trying to get a discord bot written in python to grab some info out of a google sheets document and post it into chat. i'm trying to use the googlepapi and gspread but something's wrong and i've not got a clue.

my code is as follows (posted the chunks that have to deal with the sheets stuff, left out my google service account api keys ect)

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)

and there's also the catch, but its not even getting that far

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))

Upvotes: 1

Views: 3839

Answers (1)

Tanaike
Tanaike

Reputation: 201378

About its saying its this line service = build('sheets', 'v4', credentials=creedentials), when I saw your script, creedentials of service = build('sheets', 'v4', credentials=creedentials) is creedentials={has the appropriate stuff in it}. If {has the appropriate stuff in it} is the JSON data of the service account including private_key, client_email and so on, unfortunately, that value cannot be directly used for service = build('sheets', 'v4', credentials=creedentials). I think that this is the reason of your issue.

When you want to use service = build('sheets', 'v4', credentials=creedentials) with the service account and creedentials={has the appropriate stuff in it} is the JSON data of the service account including private_key, client_email and so on, how about the following modification? In this case, please also set the scopes.

Modified script 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()

Modified script 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)

Note:

  • You can use both samples. So please chose one of them.
  • In this modification, it's a simple sample script for using service = build('sheets', 'v4', credentials=credentials) with the JSON data of the service account including private_key, client_email and so on. So please modify it for your actual situation.
  • And, this sample script uses the scope of https://www.googleapis.com/auth/spreadsheets. If you want to use other scopes, please add them.

References:

Upvotes: 3

Related Questions