Running into a TypeError: string indices must be integers

Hello,

I’m currently navigating my way through a post someone made, here. It’s about using Python along side a couple of other libraries in order to pull information from the Google My Business API and then sort it and save it. I’m tweaking and working on (expanding) this to do what I would like it to do, but before that can happen i’m running into a “TypeError: string indices must be integers” issue among a couple of others but they don’t seem to be major I don’t believe.

Traceback (most recent call last):
  File "gmb.py", line 49, in <module>
    main(sys.argv)
  File "gmb.py", line 32, in main
    'accounts/XXXXXX/locations/XXXXXX',
TypeError: string indices must be integers

Above is the full block of errors and below is the listed code.

def main(argv):
    # Use the discovery doc for auth and grabbing account info
    service, flags = sample_tools.init(argv, "mybusiness", "v4", __doc__, __file__, scope="https://www.googleapis.com/auth/business.manage", discovery_filename=discovery_doc)

    # Get the list of accounts user has access to
    output = service.accounts().list().execute()
    print("List of Accounts:\n")
    print(json.dumps(output, indent=2) + "\n")

    firstAccount = output["accounts"][0]["name"]

    # Get the list of locations for the first account in list
    print("List of Locations for Account " + firstAccount)
    body = {
        "locationNames" [
                'accounts/XXXXXX/locations/XXXXXX',
                'accounts/XXXXXX/locations/XXXXXX'
                ]
        }
    locationsList = service.accounts().locations().batchGetReviews(name=firstAccount,body=body).execute()
    print(json.dumps(locationsList, indent=2))
    with open('gmb_batchreviews.json', 'w') as json_file:
        json.dump(locationsList, json_file)
    #load json file
    with open('gmb_batchreviews.json') as f:
        d = json.load(f)
    #Clean the exported json file
    df = json_normalize(d['locationReviews'])
    df.to_csv('gmb_batchreviews.csv')
    outfile.close()

if __name__ == "__main__":
  main(sys.argv)

#filter results.  Currnet date - 1
df2 = pd.read_csv('gmb_batchreviews.csv')
df2 = df2.drop(['Unnamed: 0'], axis=1)
df2['review.createTime'] = pd.to_datetime(df2['review.createTime'])
df2 = df2.set_index(['review.createTime'])
end_range = datetime.now().date()
d = d = datetime.today() - timedelta(days=90)
start_range = d.date()
df3 = df2[end_range:start_range]

#clean data for export
df3 = df3.drop(columns=['review.name',  'review.reviewer.profilePhotoUrl'])
df3.columns = ['location','comment','reply','reply_date','reviewer','review.reviewId','star_rating','date']
export_name = 'gmb_reviews_export_' + str(end_range) +'.xlsx'
df3.to_excel(export_name, index=False)

It’s my intention to eventually pull review.reviewId from the csv or json files, have that be put into one of say, 8, responses. then pick a response at random and send it as a reply.

I copied code by hand and made a mistake

solution: ctrl c ctrl v?
or read the error message and don’t do the thing which it is describing is being done, because it describes something that you should probably agree with shouldn’t be done, and it says where … so …

even downloading the code specifically will return the same error after fixing it.

no, it will not

Traceback (most recent call last):
  File "test11.py", line 58, in <module>
    main(sys.argv)
  File "test11.py", line 45, in main
    locationsList = service.accounts().locations().batchGetReviews(name=firstAccount,body=body).execute()
  File "/home/alex/anaconda3/lib/python3.7/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/alex/anaconda3/lib/python3.7/site-packages/googleapiclient/http.py", line 856, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 500 when requesting https://mybusiness.googleapis.com/v4/accounts/XXXXXX/locations:batchGetReviews?alt=json returned "Internal error encountered.">

When I download it this is what I get, I get this because of this below

# Get the list of locations for the first account in the list
    print("List of Locations for Account " + firstAccount)
    body = {
        "locationNames": [
                "accounts/XXXXXX/locations/XXXXX",
                "accounts/XXXXXX/locations/XXXXX"
                ]
        }

the semi-colon after “locationNames” ends up being added into the https://mybusiness.googleapis.com/v4/accounts/XXXXXX/locations:batchGetReviews?alt=json url which breaks it.

See after “locations” there is “:batchGetReviews”?

removing the semi colon gives the exact issue i’m facing and made the post in reference to.

You’re not fixing anything you’re just adding more problems and asking about the problem you added instead of the problem you had, and that problem is something the blog post you’re linking to already says how to deal with.

May I ask where in the blog post it mentions how to fix the original issue?

The whole paragraph above the code.

I’m sorry if i’m not seeing what you clearly are. Everything has been done to the T in that paragraph, i’ve added the

import csv
from pandas.io.json 
import json_normalize

the location ID and account ID are also populated when I remove the : so it leads me to believe there isn’t an issue populating the location nor account ID.

Wanted to follow up with you and apologize for wasting your time, it wasn’t that the script wasn’t working. It was that the current business listing isn’t verified and that request won’t work otherwise.

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.