Getting 403 Permission Error with MSGraph Sharepoint API Python

I am trying to move sharepoint files, using the patch function from the MSGraph method, to another destination folder, but for some reason I keep getting the error when trying to compute the line, result = await self.user_client.drives.by_drive_id(parent_drive_id).items.by_drive_item_id(src_item_id).patch(request_body) :

msgraph.generated.models.o_data_errors.o_data_error.ODataError:
        APIError
        Code: 403
        message: None
        error: MainError(additional_data={}, code='accessDenied', details=None, inner_error=InnerError(additional_data={}, client_request_id='XXXX', date=DateTime(2024, 5, 23, 8, 5, 34, tzinfo=Timezone('UTC')), odata_type=None, request_id='XXXX'), message='Access denied', target=None)

I think the issue lies in how I set up the request_body and/or how I defined the drive/item id’s in the results variable.

Does anyone have an idea what I might have done incorrectly? I also have the correct rights to move/write/read files in the corresponding Sharepoint site. Am I perhaps using the incorrect destination parent folder id in the request_body? If so, does anyone know how to get the correct id? Thank u in advance 🙂

    async def _move_sharepoint_file(self, url, site_name, src_drive_path, file_name, dst_site_name, dst_drive_path, new_file_name=None):
    # Check permissions and verify authentication - ensure proper scopes are included in token
    
    # Get source site and its drive items
        sites = await self.user_client.sites.with_url(url).get()
        src_site_id = None
        dst_site_id = None
        
        for site in sites.value:
            print(site.name)
            if site.name == site_name:
                src_site_id = site.id
            if site.name == dst_site_name:
                dst_site_id = site.id
        
        # if src_site_id is None:
        #     raise ValueError(f"Source site with name '{site_name}' not found")
        # if dst_site_id is None:
        #     raise ValueError(f"Destination site with name '{dst_site_name}' not found")

        query_params = ItemsRequestBuilder.ItemsRequestBuilderGetQueryParameters(
            filter = True
        )

        request_configuration = ItemsRequestBuilder.ItemsRequestBuilderGetRequestConfiguration(
            query_parameters = query_params,
        )
        
        path_list = src_drive_path.replace("\", "/").split('/')
        print(path_list)
        drive_items = await self.user_client.drives.by_drive_id(src_site_id).items.get(request_configuration=request_configuration)
        drive_items = drive_items.value
        
        partial_dir = ""
        print(f"path_list: {path_list}")
        for dir in path_list:
            if len(path_list) == 1 and path_list[0] == "":
                
                correct_drive_items = drive_items
                
                break
            else:
                partial_dir += f"/{dir}"
            print(f"partial_dir: {partial_dir}") if self.verbose else None
            [print(f"drive: {drive.name}") for drive in drive_items] if self.verbose else None
            print() if self.verbose else None
            for drive_item in drive_items:
                if drive_item.name == dir:
                    break
            else:
                raise ValueError(f"Drive with name '{partial_dir}' not found")
            
            drive_id = drive_item.id
            drive_items = await self.user_client.drives.by_drive_id(src_site_id).items.by_drive_item_id(drive_id).children.get()
            drive_items = drive_items.value
            
            correct_drive_items = []
            for drive_item in drive_items:
                print(f"drive_item.name: {drive_item.name}") if self.verbose else None
                parent_path = drive_item.parent_reference.path
                print(f"drive_item.parent_reference.path: {drive_item.parent_reference.path}") if self.verbose else None
                if parent_path.endswith(partial_dir):
                    correct_drive_items.append(drive_item)
                    print(f"YES") if self.verbose else None
        drives = correct_drive_items
        drives = [drive for drive in drives if drive.name in file_name]
        if len(drives) == 0:    
            raise ValueError(f"Drive with name '{file_name}' not found")
        
        src_item_id = drives[0].id
        parent_drive_id = drives[0].parent_reference.drive_id
        print(parent_drive_id)

        # Find the source item
        #src_item_id = await self.get_item_id(src_site_id, src_drive_path, file_name)

        # Get destination folder ID
        dst_drive_items = await self.user_client.drives.by_drive_id(dst_site_id).items.get(request_configuration=request_configuration)
        dst_folder_id = None
        for drive_item in dst_drive_items.value:
            if drive_item.name == dst_drive_path.split('/')[-1]:
                dst_folder_id = drive_item.id
                
                parent_dst_id = drive_item.parent_reference.drive_id
                print("Not none")
                break
        
        
        if src_item_id is None:
            raise ValueError(f"Item with name '{file_name}' in path '{src_drive_path}' not found")
        if dst_folder_id is None:
            raise ValueError(f"Destination folder '{dst_drive_path}' not found")

        # Get source drive ID
        src_drive_items = await self.user_client.drives.by_drive_id(src_site_id).items.get(request_configuration=request_configuration)
        src_drive_id = None
        for drive_item in src_drive_items.value:
            if drive_item.name == src_drive_path:
                src_drive_id = drive_item.id
                break
        
        if src_drive_id is None:
            raise ValueError(f"Source drive '{src_drive_path}' not found")

       
       
        request_body = DriveItem(
            parent_reference = ItemReference(
                site_id= dst_site_id,
                id = dst_folder_id,
            ),
            name ="new-item-name.pdf",
        )
        
        result = await self.user_client.drives.by_drive_id(parent_drive_id).items.by_drive_item_id(src_item_id).patch(request_body)
        
        if result.is_success:
            print("File moved successfully.")
            return result.content
        else:
            print(f"Failed to move file. Status code: {result.status_code}")
            return None

I have tried several combinations of drive and item id’s, because I thought that would be the issue. But everytime I get the same error. The (almost) same method for downloading a Sharepoint file does work using MSGraph and I do have the correct rights in Azure but also in Sharepoint for the corresponding file.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật