Google Drive API : use client_secret_file.json within secrets file

Hello ,
i am working on a project, downloading data from Google Drive and displaying them in my streamlit app, all done by using google drive api which itself utilizes a client_secret_file.json
is there a way to use the data found in this JSON file in my secrets file?
the function uses the whole JSON file as a parameter so im not sure which credentials to take from it.
here’s the function that uses the file
def Create_Service(client_secret_file, api_name, api_version, *scopes):
print(client_secret_file, api_name, api_version, scopes, sep=‘-’)
CLIENT_SECRET_FILE = client_secret_file
API_VERSION = api_version
SCOPES = [scope for scope in scopes[0]]

cred = None

pickle_file = f'token_{API_SERVICE_NAME}_{API_VERSION}.pickle'
# print(pickle_file)

if os.path.exists(pickle_file):
    with open(pickle_file, 'rb') as token:
        cred = pickle.load(token)

if not cred or not cred.valid:
    if cred and cred.expired and cred.refresh_token:
        flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
        cred = flow.run_local_server()

    with open(pickle_file, 'wb') as token:
        pickle.dump(cred, token)

    service = build(API_SERVICE_NAME, API_VERSION, credentials=cred)
    print(API_SERVICE_NAME, 'service created successfully')
    return service
except Exception as e:
    print('Unable to connect.')
    return None

Hi @Kally

If you’re deploying your app to the Streamlit Community Cloud, you can use the built-in secrets management on the platform to store the contents from the secrets file.

Please see

Next, to use it in the app you can call it like so:

user = st.secrets[“db_username”]

Hope this helps!

hello data professor, sadly this did not work
i had a temporary solution which was to place the client_secrets_file within the streamlit secrets just as you suggested, and use the function json.loads to pass the variable as argument instead of the path , it works locally but when the app is hosted it won’t work and i get the error “TypeError: expected str, bytes or os.PathLike object, not dict”

upon further inspections , i tried to pass the client_secrets_file.json directly and still got an error only on the hosted app which is “raise Error(“could not locate runnable browser”)”
does this have to do with streamlit cloud hosting ?
here’s the project github link : VinDazy/Isimm-Study-Hub (
Thank you

Hi @Kally

Have you activated your Google Drive API, which would grant your app access to the Google Drive API, here’s more info on how to:

yes i have already activated the google drive api,
my problem is that the application works just fine locally but when hosted it won’t work, even tho they’re working from the same project files

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