下载文件失败:404 - {"error":{"code":"ResourceNotFound","message":"未找到用户","innerError":

问题描述 投票:0回答:1

我尝试使用 Graph API 下载位于 OneDrive 中的特定文件。我创建了一个 Azure 应用程序来下载和上传文件。因为这是我第一次使用此 API,所以我不确定为什么会收到“未找到用户”的响应消息。我已准备好租户 ID、客户端密钥和客户端 ID。

代码:

import time

import requests
import os

# Azure AD credentials
client_id = '7827d4d5-e06e-4dc3-b4df-92fde865d9fd'
client_secret = 'LqE8Q~yQ2QWmyPL62pZE~pwIRukrq.nUefaRic7g'
tenant_id = 'a38bf430-8119-4d00-b314-1bf3d9eadd6b'

def get_access_token():
    # Get an access token using client credentials flow
    token_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"
    payload = {
        'grant_type': 'client_credentials',
        'client_id': client_id,
        'client_secret': client_secret,
        'scope': 'https://graph.microsoft.com/.default'
    }
    response = requests.post(token_url, data=payload).json()
    return response.get('access_token')

def download_file_from_onedrive(file_path_onedrive, local_file_path):
    access_token = get_access_token()
    if access_token:
        # Construct the URL for the file using the user ID
        user_id = 'your-user-id'  # Replace 'your-user-id' with the actual user ID or principal name
        file_url = f'https://graph.microsoft.com/v1.0/users/{user_id}/drive/root:{file_path_onedrive}:/content'

        # Download the file from OneDrive
        headers = {
            'Authorization': 'Bearer ' + access_token
        }
        response = requests.get(file_url, headers=headers)

        if response.status_code == 200:
            # Save the file locally
            with open(local_file_path, 'wb') as f:
                f.write(response.content)
            print(f"File downloaded successfully to {local_file_path}")
        else:
            print(f"Failed to download file: {response.status_code} - {response.text}")
    else:
        print("Failed to obtain access token")

def upload_file_to_onedrive(local_file_path, file_path_onedrive):
    access_token = get_access_token()
    print()
    if access_token:
        # Construct the URL for the upload
        upload_url = f'https://graph.microsoft.com/v1.0/me/drive/root:{file_path_onedrive}:/content'

        # Upload the file to OneDrive
        headers = {
            'Authorization': 'Bearer ' + access_token,
            'Content-Type': 'application/octet-stream'
        }
        with open(local_file_path, 'rb') as f:
            response = requests.put(upload_url, headers=headers, data=f)

        if response.status_code == 201:
            print(f"File uploaded successfully to OneDrive: {file_path_onedrive}")
        else:
            print(f"Failed to upload file: {response.status_code} - {response.text}")
    else:
        print("Failed to obtain access token")

# Example usage:
onedrive_file_path = '/Details.xlsx'
local_file_path = 'Details.xlsx'

# Download file from OneDrive to local directory
download_file_from_onedrive(onedrive_file_path, local_file_path)
time.sleep(10)
# Upload file from local directory to OneDrive
# upload_file_to_onedrive(local_file_path, onedrive_file_path)

权限: enter image description here

错误:

Failed to download file: 404 - {"error":{"code":"ResourceNotFound","message":"User not        found","innerError":{"date":"2024-04-17T21:26:07","request-id":"56a4e23e-3e58-406a-8981-       6e01230356eb","client-request-id":"56a4e23e-3e58-406a-8981-6e01230356eb"}}}

权限: (https://i.stack.imgur.com/aTjmG.png)

有办法下载文件并解决这个问题吗?

python azure api
1个回答
0
投票

您需要将一个值传递给

user_id

在您的示例中,您的 download_file_from_onedrive 函数不接受 user_id,并且您尚未在函数内定义它。因此,它是空的,从而产生用户未找到响应。

© www.soinside.com 2019 - 2024. All rights reserved.