Running into a TypeError: string indices must be integers


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 "", line 49, in <module>
  File "", line 32, in main
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="", 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" [
    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'])

if __name__ == "__main__":

#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 =
d = d = - timedelta(days=90)
start_range =
df3 = df2[end_range:start_range]

#clean data for export
df3 = df3.drop(columns=['',  '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 "", line 58, in <module>
  File "", line 45, in main
    locationsList = service.accounts().locations().batchGetReviews(name=firstAccount,body=body).execute()
  File "/home/alex/anaconda3/lib/python3.7/site-packages/googleapiclient/", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/alex/anaconda3/lib/python3.7/site-packages/googleapiclient/", line 856, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 500 when requesting 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": [

the semi-colon after “locationNames” ends up being added into the 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
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.