Isaiahensley / Aquatic-RIG

Our Senior Capstone project focuses on developing a Streamlit website dedicated to the visualization of aquatic NetCDF datasets. Aquatic data is inherently complex, being both spatiotemporal—capturing information over different times and space. Our website will let users alternate through time and depth to give comprehensive visuals for their data.
https://aquaticrig-develop.streamlit.app/
0 stars 1 forks source link

DropBox Integration for Feedback Storage #58

Closed spaude11 closed 5 months ago

spaude11 commented 7 months ago

We will use this ticket for uploading our feedback pdf file into specific dropbox file

The initial plan of using google drive is changed to dropbox.

spaude11 commented 7 months ago

credentials = service_account.Credentials.from_service_account_file('service_account_key.json') drive_service = build('drive', 'v3', credentials=credentials)

def upload_to_google_drive(pdf_filename): file_metadata = { 'name': pdf_filename } media = MediaFileUpload(pdf_filename, mimetype='application/pdf') file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute() return file.get('id').

I will work on integrating the google api and setup this is just the basic code that i will use. 
Isaiahensley commented 7 months ago

credentials = service_account.Credentials.from_service_account_file('service_account_key.json') drive_service = build('drive', 'v3', credentials=credentials)

def upload_to_google_drive(pdf_filename): file_metadata = { 'name': pdf_filename } media = MediaFileUpload(pdf_filename, mimetype='application/pdf') file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute() return file.get('id').

I will work on integrating the google api and setup this is just the basic code that i will use.

spaude11 commented 7 months ago

BadInputError: BadInputError('e77c8dd5b46242f2845404cc0535306b', 'Error in call to API function "files/upload": Your app is not permitted to access this endpoint because it does not have the required scope \'files.content.write\'. The owner of the app can enable the scope for the app using the Permissions tab on the App Console.') Traceback: File "/Users/sudeeppaudel/Documents/GitHub/Aquatic-RIG/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 535, in _run_script exec(code, module.dict) File "/Users/sudeeppaudel/Documents/GitHub/Aquatic-RIG/aquaticrig_gui.py", line 42, in app.run() File "/Users/sudeeppaudel/Documents/GitHub/Aquatic-RIG/aquaticrig_gui.py", line 30, in run feedback_page() File "/Users/sudeeppaudel/Documents/GitHub/Aquatic-RIG/feedback_page.py", line 84, in feedback_page upload_to_dropbox(pdf_filename) File "/Users/sudeeppaudel/Documents/GitHub/Aquatic-RIG/feedback_page.py", line 16, in upload_to_dropbox dbx.files_upload(file_contents, dropbox_path, mode=dropbox.files.WriteMode.overwrite) File "/Users/sudeeppaudel/Documents/GitHub/Aquatic-RIG/venv/lib/python3.9/site-packages/dropbox/base.py", line 3210, in files_upload r = self.request( File "/Users/sudeeppaudel/Documents/GitHub/Aquatic-RIG/venv/lib/python3.9/site-packages/dropbox/dropbox_client.py", line 326, in request res = self.request_json_string_with_retry(host, File "/Users/sudeeppaudel/Documents/GitHub/Aquatic-RIG/venv/lib/python3.9/site-packages/dropbox/dropbox_client.py", line 476, in request_json_string_with_retry return self.request_json_string(host, File "/Users/sudeeppaudel/Documents/GitHub/Aquatic-RIG/venv/lib/python3.9/site-packages/dropbox/dropbox_client.py", line 596, in request_json_string self.raise_dropbox_error_for_resp(r) File "/Users/sudeeppaudel/Documents/GitHub/Aquatic-RIG/venv/lib/python3.9/site-packages/dropbox/dropbox_client.py", line 632, in raise_dropbox_error_for_resp raise BadInputError(request_id, res.text)

dropbox_token = 'token'
dbx = dropbox.Dropbox(dropbox_token)

def upload_to_dropbox(pdf_filename):
    with open(pdf_filename, 'rb') as f:
        file_contents = f.read()
    dropbox_path = f'/Feedbackfiles/{pdf_filename}'  # Specify the path in Dropbox where you want to upload the file
    dbx.files_upload(file_contents, dropbox_path, mode=dropbox.files.WriteMode.overwrite)

Description: Met with the error while getting access to the dropbox app from streamlit code. I was working on the feedback reports that are generated. I wanted to store the pdf into a pdf for later reference.

spaude11 commented 7 months ago
Screenshot 2024-02-13 at 9 06 40 pm

BadInputError: BadInputError('d02f7149882a486d93f493de0057e4bd', 'Error in call to API function "files/upload": Your app is not permitted to access this endpoint because it does not have the required scope \'files.content.write\'. The owner of the app can enable the scope for the app using the Permissions tab on the App Console.')

Expected vs. Actual Behavior Same error after trying for few times after changing the permission on dropbox. I made change on dropbox/developers/apps permission. I was supposed to give permission for files and folder.

Environment: Pycharm IDE, Streamlit Cloud(WebHosting), DropBox(File Storage), Pythov 3.9

spaude11 commented 7 months ago

This "Your app is not permitted to access this endpoint" error indicates that the access token you're using to make the call does not have the necessary scope needed for calling that particular endpoint. As indicated by the error message in this case, the app would need the "files.metadata.read" scope.

You can add it via the "Permissions" tab on the app's info page on the App Console. Then you can get a new access token with that scope added and use that new access token to make this API call. https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Integrated-creation-folder/td-p/470963

Issue Resolution: Since the Accept cookies was hiding the submit button, I wasn't able to figure out why my change was not updated. When I accepted the cookie and submitted the change through the popup submit button, The change was saved and I regenerated the token. That worked and feedback file I generated was going into the dropbox folder I assigned.

spaude11 commented 7 months ago
Screenshot 2024-02-13 at 8 51 27 pm

After solving and successfully able to upload it into dropbox.

spaude11 commented 7 months ago

This "Your app is not permitted to access this endpoint" error indicates that the access token you're using to make the call does not have the necessary scope needed for calling that particular endpoint. As indicated by the error message in this case, the app would need the "files.metadata.read" scope.

You can add it via the "Permissions" tab on the app's info page on the App Console. Then you can get a new access token with that scope added and use that new access token to make this API call. https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Integrated-creation-folder/td-p/470963

This helped me solve the issue The access token is still visible into code, work on hiding the token for security.

Isaiahensley commented 7 months ago

This is great and I'm glad we are formatting our issues this way. It makes understanding everyone's progress a lot easier. I'll look at this with you later today.

spaude11 commented 5 months ago

Description: The objective of this issue was to upload the feedback file name as current date time rather than email address.

Outcome Expective the file named should be in Y_M_D_H_M_S format.

Screenshot 2024-04-03 at 12 37 40 PM

Code Snippets:

   def upload_file(self, file_data, file_name, st):
        try:
            current_time = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
            # Construct the new filename with the user's email
            new_file_name = f"feedback_report_{current_time}.pdf"

            # Upload the file to Dropbox
            upload_path = f'/Apps/Feedbackfiles/{new_file_name}'
            self.dbx.files_upload(file_data, upload_path, mode=dropbox.files.WriteMode.overwrite)

            st.success(f"Feedback PDF '{new_file_name}' uploaded successfully to Dropbox.")
        except dropbox.exceptions.ApiError as e:
            st.error(f"Error uploading '{file_name}' to Dropbox: {e}")