Skip to content

Cannot upload PDF file to OneDrive, unclear API Error #869

@Mirciulica15

Description

@Mirciulica15

Describe the bug

I am trying to upload a PDF file to OneDrive, but I receive a 400 APIError with an Invalid Request.

Expected behavior

Successful upload of the file.

How to reproduce

files = [item.name for item in await self._get_onedrive_items(
    query_filter="file ne null and file/mimeType eq 'application/pdf'"
)]

folder_id_map = await self._get_folder_ids(source_folders)

for folder in source_folders:
    folder_path = os.path.join('documents', folder)
    if not os.path.exists(folder_path):
        continue

    for file_name in os.listdir(folder_path):
        if file_name in files:
            continue

        file_path = os.path.join(folder_path, file_name)

        with open(file_path, 'rb') as file:
            file_content = file.read()

        folder_id = folder_id_map.get(folder)
        if not folder_id:
            print(f"Folder ID for '{folder}' not found.")
            continue

        parent_reference = ItemReference(
            id=folder_id
        )

        drive_item = DriveItem(
            name=file_name,
            file=File(),
            parent_reference=parent_reference,
            content=file_content
        )

        try:
            response = await self._user_client.drives.by_drive_id(
                os.getenv('ONEDRIVE_DEV_DRIVE_ID')
            ).items.post(drive_item)

            print("Response", response)
        except Exception as e:
            print(f"Failed to upload file '{file_name}': {e}")

SDK Version

msgraph-core~=1.1.2 msgraph-sdk~=1.5.3

Latest version known to work for scenario above?

No response

Known Workarounds

No response

Debug output

Click to expand log ```

APIError
Code: 400
message: None
error: MainError(additional_data={}, code='invalidRequest', details=None, inner_error=InnerError(additional_data={}, client_request_id='13230f64-2c2e-4083-93c1-f7325291333c', date=DateTime(2024, 8, 22, 10, 12, 34, tzinfo=Timezone('UTC')), odata_type=None, request_id='f1815677-f808-46dd-af6d-7882803e975b'), message='Invalid request', target=None)

</details>


### Configuration

- OS: Windows
- architecture: AMD64

### Other information

The 400 APIError could be more robust than just "Invalid request"

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions