How can oauth2client.service_account import ServiceAccountCredentials be used with secrets.toml and secret app settings?

Hello. Actually I use oauth2client.service_account import ServiceAccountCredentials for GSpread with the following code:

# Set up the credentials
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name('PATH_TO_JSON.json', scope)
client = gspread.authorize(creds)

# Open the Google Sheet using its name
sheet = client.open("NAME_OF_SHEET").worksheet('NAME_OF_WORKSHEET')

But the json file is not very practical for localhost and cloud app.
So for local I need to emplement it in the local secrets.toml file and for the app to integrate it in the secrets app setting.
How can I do this?
Thanks!

Hi @roland71,

Thanks for your question.

You can find all the details about secrets management in our documentation:

It should be pretty straightforward to set up with Gspread. Let me know if you’re facing any issues, and I will guide you.

Best,
Charly

2 Likes

Hi @roland71

In continuation of @Charly_Wargnier’s suggestion on using the Secrets management, you can open up the json file, then copy and paste it into the text box of the Secrets management on Community Cloud, finally format the credentials from JSON format to be in TOML format.

For example, going from JSON format

"variable": "value",

Change to:

variable = "value"
2 Likes

Thanks! I think I found a solution.

  1. I’ve created a dictionary
    google_service_account = {"type" = "service_account", "project_id", AND_SO_ON}
    and added this to the secrets.toml and secrets area
  2. In my phyton file I added
google_service_account_info = st.secrets['google_service_account']
creds = ServiceAccountCredentials.from_json_keyfile_dict(google_service_account_info, scope)

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