导入“msgraph.core”无法解决 - 帮我搜索我的 OneDrive 是否存在重复项 M365 家庭版

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

我正在尝试检查我的 OneDrive 家庭版是否有重复文件。 在https://developer.microsoft.com/en-us/graph/graph-explorer 同意后我可以看到简单查询的结果。是吗?

但是在 Windows 11、python v3.11 上的 VS Code 中不断出现此错误 “无法解析导入“msgraph.core”PylancereportMissingImports”

我不在任何conda环境中。 安装它卸载然后这样做:

PS C:\Users\Test> pip show msgraph-core

名称:msgraph-core 版本:1.0.0 摘要:Microsoft Graph Python SDK 的核心组件 主页: 作者: 作者电子邮件:Microsoft [电子邮件受保护] 许可证:麻省理工学院许可证 .....巴拉巴拉巴拉巴拉 必需者:msgraph-sdk

我还没有填写AZ客户端ID(因为是家庭版,所以我还找不到..) 但它是否仍然会给我 msgraph.core 错误 - 似乎找不到它?

帮助并感谢!

代码:

from msgraph.core import GraphClient
from azure.identity import InteractiveBrowserCredential
import hashlib

# Azure app registration details
CLIENT_ID = 'YOUR_CLIENT_ID'  # Replace with your Azure AD client ID

# Scopes required by the app
SCOPES = ['Files.ReadWrite.All']  # Scope for OneDrive access

# Authentication with Interactive Browser Credential
credential = InteractiveBrowserCredential(client_id=CLIENT_ID)
graph_client = GraphClient(credential=credential, scopes=SCOPES)

# Create an empty dictionary to store the hashes and file names
hashes = {}

# Function to process files and find duplicates in a folder
def process_files(folder_id):
    request_url = f'me/drive/items/{folder_id}/children'
    drive_items = graph_client.get(request_url).json()

    for item in drive_items['value']:
        # Check if the item is a file
        if 'file' in item:
            # Get the file content
            file_content = graph_client.get(item['@microsoft.graph.downloadUrl']).content

            # Create a hash object using md5
            hash_obj = hashlib.md5()
            hash_obj.update(file_content)
            hash_digest = hash_obj.hexdigest()

            if hash_digest in hashes:
                print(f"{item['name']} is a duplicate of {hashes[hash_digest]}")
            else:
                hashes[hash_digest] = item['name']
        elif 'folder' in item:
            # If the item is a folder, process its contents
            process_files(item['id'])

# Start processing from the root folder
root_folder = graph_client.get('me/drive/root').json()
process_files(root_folder['id'])

我期待NOT得到“无法解析导入“msgraph.core”” 也许还有一些代码错误

python api visual-studio-code microsoft-graph-api onedrive
1个回答
0
投票

这意味着在您当前的 Python 解释器中找不到模块

msgraph.core

  1. 您可以尝试使用快捷键Ctrl+Shift+P并输入“Python:选择解释器”来选择您安装此模块的Python解释器。
  2. 如果您只想禁用此警告。您可以将以下代码添加到您的
    settings.json
    :
"python.analysis.diagnosticSeverityOverrides": {
    "reportMissingImports": "none"
}
© www.soinside.com 2019 - 2024. All rights reserved.