'Importing GSpread File in Google Cloud Console Conflicts With Flask App; JSON Api Key Present

I have been able to deploy a flask app relatively easily with the proper docker configuration in Google Cloud Console. I am running this within a docker container. However, using gspread (with relevant authorization json file) does not permit me to deploy use flask app. When I run the same code from a Python script in the same container without Flask, I can verify Gspread script is ok.

I show the Dockerfile below:

# Python image to use.
FROM python:3.8

# Set the working directory to /app
WORKDIR /app

# copy the requirements file used for dependencies
COPY requirements.txt .

# Install any needed packages specified in requirements.txt

RUN pip3 install --trusted-host pypi.python.org -r requirements.txt

# Copy the rest of the working directory contents into the container at /app
COPY . .

# Run app.py when the container launches
ENTRYPOINT ["python", "app.py"]

However, when I attempt to add an import of Gspread, with the relevant permissions in code like this:


import flask  


import gspread
scopes = ['https://www.googleapis.com/auth/spreadsheets','https://www.googleapis.com/auth/drive']
gauth = "XXX.json"
credentials = ServiceAccountCredentials.from_json_keyfile_name("/home/XXX.json", scopes) 
#access the json key you downloaded earlier 
client = gspread.authorize(credentials) 
# https://developers.google.com/drive/api/v3/quickstart/python
api_service = build('drive', 'v3', credentials=credentials)

# Call the Drive v3 API
results = api_service.files().list( 
                            spaces = 'drive',\
                            supportsAllDrives = True,\
                                fields='nextPageToken, files(webViewLink, id, name)').execute()
items = results.get('files', [])

...
app = Flask(__name__)

@app.route('/', methods=['POST'])
def sms():

    return str(items)


    
if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8080, debug=True)

When I try to make reference to any gspread code within a Cloud Console, then I get an error; the error says something to the effect that port 8080 is no longer available. This error surfaces when I deploy to cloud run.

. Error: Deploying container to Cloud Run service [hello-world] in project [XXX] region [us-central1] ,Deploying... Creating Revision..............................................................................................................failed Deployment failed ERROR: (gcloud.run.deploy) Cloud Run error: The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source