Reputation: 53
Thanks for reading this. I am trying to build a Streamlit app and I would like to be able to connect to Google Drive and retrieve some files in my Drive within the streamlit app.
On my local machine I am able to do this running the following lines:
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
#Download files in sheet names
drive_service = build('drive', 'v3', credentials=gauth.credentials)
for file_name in sheet_names:
file = drive.ListFile({ "q":"title='example",includeItemsFromAllDrives":"True",supportsAllDrives":"True", "corpora":"allDrives"}).GetList()
The above code would login and download a file titled example from my Google Drive. I want to reproduce this in Streamlit. Undortunately, I can not login the same way as I do on my local machine and as far as I understood, I should use service account. If you could share some code with me, that would be ideal.
THanks!
Upvotes: 0
Views: 1687
Reputation: 1
I have tried that too setting up and using oauth2 user credentials, and I have tried service accounts and in both cases I get a blank list. I have also tried the drive.mount, but if you do that in the same cell as the streamlit it will not authenticate. So in all cases my folder list is empty...
def authenticate_gdrive():
gauth = GoogleAuth()
# Load client secrets from the provided path.
gauth.LoadClientConfigFile(auth_user_credentials_path)
# Use the following line for authentication in Google Colab
gauth.CommandLineAuth()
return GoogleDrive(gauth)
I have also tried setting up and using a service account.
def get_drive_service():
creds = None
st.write(f'google_credentials_path = {google_credentials_path}')
if google_credentials_path:
creds = service_account.Credentials.from_service_account_file(google_credentials_path)
st.write(f"Credentials = {creds}")
st.write(f'Credentials expired: {creds.expired}')
else:
st.warning("Credentials path not found in environment variables.")
return None
service = build('drive', 'v3', credentials=creds)
return service
All I want to be able to do is save the history of interaction with my streamlit app in a word document. So I just want the following to work but the only debug statement that works is the Debug drive path, because the drive path is hard coded. The debug folders does not show up, because the os.walk does not respond.
with st.sidebar:
st.sidebar.header("Select Save Folder")
# Define the path to your Google Drive
drive_path = '/content/drive/My Drive'
st.write(f"Debug drive path - {drive_path}")
# List the subdirectories in your Google Drive
#folders = [d for d in os.listdir(drive_path) if os.path.isdir(os.path.join(drive_path, d))]
# Using os.walk to list directories
folders = next(os.walk(drive_path))[1]
st.write(f"Debug folders - {folders}") # Debug statement to print the folders list
if folders:
selected_folder_name = st.selectbox('Select folder:', folders, index=0)
selected_folder_path = os.path.join(drive_path, selected_folder_name)
else:
st.error('No folders found.')
if st.button('Save Conversation to Word',key='save_button_outside_function'):
save_conversation_to_word(st.session_state.chat_history)
Upvotes: 0