0

Got one shared link from OneDrive hosted by someone that's not me - https://1drv.ms/v/s!AjonToUPWqXmgjO3RqDbhRaSMrOM?e=69dccx

While investigating a solution that would enable to download that file from OneDrive programatically with Python, stumbled accross Office365-REST-Python-Client.

In their GitHub page they've got a specific section dedicated to OneDrive and also some examples of what one can do with it where one closely matches the desired goal.

Installed it using

pip install Office365-REST-Python-Client

but after comparing the code between the example that closely matched my goal realized that had to run instead

pip install office365-rest-client

Changed the initial part to

import os
import tempfile
from office365.graph.graph_client import GraphClient
from office365.settings import settings

which required to create a file in root with the name settings.py

import os

secure_vars = os.environ['office365_python_sdk_securevars'].split(';')
tenant = os.environ.get('office365_python_sdk_tenant', 'mediadev8')

settings = {
    'url': 'https://{tenant}.sharepoint.com/'.format(tenant=tenant),
    'tenant': '{tenant}.onmicrosoft.com'.format(tenant=tenant),
    'redirect_url': 'https://github.com/vgrem/Office365-REST-Python-Client/',
    'user_credentials': {
        'username': secure_vars[0],
        'password': secure_vars[1]
    },
    'client_credentials': {
        'client_id': secure_vars[2],
        'client_secret': secure_vars[3],
    }
}

Then, adapted the function get_token() to

def get_token(auth_ctx):
    """Acquire token via client credential flow (ADAL Python library is utilized)
    :type auth_ctx: adal.AuthenticationContext
    """

    url = 'https://1drv.ms/v/s!AjonToUPWqXmgjO3RqDbhRaSMrOM?e=69dccx'
    username = 'email'
    password = 'password'

    token = auth_ctx.acquire_token_for_user(url, username, password)
    return token

So, this is the current code

import os
import tempfile
from office365.graph.graph_client import GraphClient
from office365.settings import settings

def get_token(auth_ctx):
    """Acquire token via client credential flow (ADAL Python library is utilized)
    :type auth_ctx: adal.AuthenticationContext
    """

    url = 'https://1drv.ms/v/s!AjonToUPWqXmgjO3RqDbhRaSMrOM?e=69dccx'
    username = 'email' #with my email
    password = 'password' #with my password

    token = auth_ctx.acquire_token_for_user(url, username, password)
    return token


def download_files(remote_folder, local_path):
    drive_items = remote_folder.children
    client.load(drive_items)
    client.execute_query()
    for drive_item in drive_items:
        if not drive_item.file.is_server_object_null:  # is file?
            # download file content
            with open(os.path.join(local_path, drive_item.name), 'wb') as local_file:
                drive_item.download(local_file)
                client.execute_query()
            print("File '{0}' has been downloaded".format(local_file.name))


# example demonstrates how to export OneDrive files into local file system

# connect
client = GraphClient(settings['tenant'], get_token)

# load drive properties
drive = client.users["tiagomartinsperes@gmail.com"].drive
client.load(drive)
client.execute_query()

# download files from OneDrive
with tempfile.TemporaryDirectory() as path:
    download_files(drive.root, path)
    print("Done")

Even though these adjustments fixed various problems, still didn't manage to make it work.

When running the code, this is the error I'm getting

File "C:\Users\tiago\Anaconda3\lib\site-packages\office365\settings.py", line 3, in secure_vars = os.environ['office365_python_sdk_securevars'].split(';')

File "C:\Users\tiago\Anaconda3\lib\os.py", line 678, in getitem raise KeyError(key) from None

KeyError: 'office365_python_sdk_securevars'

Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145
  • 1
    I can download this video without being logged in to Microsoft, so you also should be able to access it without any access_tokens. – mirik Jul 06 '20 at 15:47

0 Answers0