Is there any way to download files or folders from the public share URL of OneDrive belonging to other users (external resources)?

I have a requirement to download files from the public URL of any OneDrive file or folder belonging to another user using the Django framework. I have written the code, but it gives me an error when accessing the file_id.

I am trying to access the file info from below code, but it is now working I have also give the and use msal library for the same

  1. #permission

    1. “Sites.Read.All”,

    2. “Sites.ReadWrite.All”,

    3. “Files.ReadWrite.All”,

    4. “Files.Read.All”

#code where the problem occurs

file_info = get_drive_item_id(access_token, encoded_share_url)

#code


    class DownloadView(APIView): 
        def post(self, request): 
            def encode_share_url(share_url): 
                encoded_url = base64.urlsafe_b64encode(share_url.encode()).decode().rstrip("=") 
                return f'u!{encoded_url}'    
            def get_drive_item_id(access_token, share_url):
                url = f"https://graph.microsoft.com/v1.0/shares/{share_url}/driveItem"
                headers = {
                    'Authorization': f'Bearer {access_token}'
                }
        
                response = requests.get(url, headers=headers)
                response.raise_for_status()
                return response.json()
    
            def resolve_shortened_url(short_url):
                response = requests.head(short_url, allow_redirects=True)
                return response.url
    
        AUTHORITY = "https://login.microsoftonline.co`your text`m/tenant-id/"
        CLIENT_ID = "client_id"
        CLIENT_SECRET = "client_secret"
        SCOPES = ["https://graph.microsoft.com/.default"]
    
        link = request.data.get("link")
        
        # Initialize MSAL client application
        app = ConfidentialClientApplication(CLIENT_ID, 
                authority=AUTHORITY,        
                client_credential=CLIENT_SECRET
            )
        result = app.acquire_token_for_client(scopes=SCOPES)
        
        if "access_token" in result:
            access_token = result['access_token']
            headers = {"Authorization": f"Bearer {result['access_token']}"}
    
            encoded_share_url = encode_share_url(link)
            file_info = get_drive_item_id(access_token, encoded_share_url)
    
            # Resolve the shortened URL
            resolved_url = resolve_shortened_url(link)
    
            
            # Parse the file ID from the resolved URL
            parsed_url = urlparse(resolved_url)
            query_params = parse_qs(parsed_url.query)
    
            
         
            
            # Use the file ID to construct the download URL
            url = f"https://graph.microsoft.com/v1.0/drive/items/{file_id}/content"
            print("url: ", url)
            
            # Download the file
            file_response = requests.get(url, headers=headers)
            print("status_code: ", file_response.status_code)
            
            if file_response.status_code == 200:
                # Save the file locally
                file_name = "downloaded_file"  # Customize the file name as needed
                with open(file_name, 'wb') as file:
                    file.write(file_response.content)
                return success_response(data = file_response, message='DFile downloaded successfully.')
            else:
                return failure_response(data=file_response.json(), message="Downloading failed")
                
        else:
            return success_response(data = file_response, message='Downloaded successfully.')

Recognized by Microsoft Azure Collective

New contributor

Rishabh Meena is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

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