Search code examples
pythonvisual-studio-codemicrosoft-graph-apionedrive

Import "msgraph.core" could not be resolved - Help me search my OneDrive for Duplicates M365 Home Edition


I am trying to check my home edition of OneDrive for duplicate files. On https://developer.microsoft.com/en-us/graph/graph-explorer I can see results for simple queries after giving consent. Yay?

But constantly getting this error in VS Code on Windows 11, Python v3.11:

"Import "msgraph.core" could not be resolvedPylancereportMissingImports"

I am not in any conda environment. Installed it uninstalled then did this:

PS C:\Users\Test> pip show msgraph-core     
>>
**Name: msgraph-core
Version: 1.0.0
Summary: Core component of the Microsoft Graph Python SDK
Home-page: 
Author: 
Author-email: Microsoft <[email protected]>
License: MIT License**
.....blah blahc blah
Required-by: msgraph-sdk

I have not filled out the AZ client ID (because it is a home version, I can't find that..yet) but should it still be giving me the msgraph.core error - it seems it can't find it?

Code:

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'])

I was expecting NOT to get:

"Import "msgraph.core" could not be resolved"

and perhaps some code error.


Solution

  • I had a similar problem when upgrading to the newly released 1.0.0. Change your import to msgraph_core. But be aware that GraphClient is no longer available. You will have to downgrade your installation to 0.2.2 if you want to use that import and class. See the package history for more information.

    In addition, check out this comment by the devs which encourages to use the SDK instead. There is also a migiration guide which lists your case.