|
| 1 | +# CommandLineFileExplorer sample |
| 2 | + |
| 3 | +The CommanLineFileExplorer sample is a sample app written in Python and uses the OneDrive SDK for Python. |
| 4 | +The sample shows you how to work with a user's files and folders on OneDrive. In this sample, you will learn how to upload or download a file, get a sharing link, explore files and folder, and more. |
| 5 | + |
| 6 | +## Set up |
| 7 | + |
| 8 | +1. If you don't have Python installed, go to [Python.org](http://python.org) and scroll over **Downloads** to choose the install for your platform. For example, choose **Download for Windows** | **Python 3.5.0** to download Python for Windows. Follow the instructions in [Using the Python Interpreter](https://docs.python.org/3/tutorial/interpreter.html) to complete your Python set up. |
| 9 | +2. Download the [OneDrive SDK for Python](https://github.com/OneDrive/onedrive-sdk-python/) to get the sample. |
| 10 | +3. Open a command prompt and type `pip install requests` to install [Requests](http://docs.python-requests.org/en/latest/). |
| 11 | +4. In the command prompt, type `pip install pillow` to install [Pillow](https://pypi.python.org/pypi/Pillow/3.0.0). |
| 12 | +5. Next, type `pip install onedrivesdk` to install the OneDrive SDK for Python. |
| 13 | + |
| 14 | +## Run the sample |
| 15 | + |
| 16 | +1. Open a command prompt and type `py -3` to make sure that you are running Python 3.5. |
| 17 | +2. Press **CTRL**+**D** and **Enter** to exit _interactive mode_. |
| 18 | +3. Navigate to where you downloaded the CommandLineFileExplorer.py sample. If you installed a .zip of the OneDrive SDK for Python, the file should be located in ../onedrive-sdk-python/examples/. |
| 19 | +4. Type `py CommandLineFileExplorer.py` to run the sample. |
| 20 | +5. The app needs permissions to access your OneDrive. Click **Accept** in the browser to grant permissions access to your OneDrive. |
| 21 | + |
| 22 | +## API features |
| 23 | + |
| 24 | +### Sign in and authentication |
| 25 | + |
| 26 | +This sample uses the [code flow](https://dev.onedrive.com/auth/msa_oauth.htm#code-flow) to sign the user in and authenticate the app. `get_default_client` is called to get a OneDrive client. `get_auth_url` returns the `redirect_uri` that contains the authorization code. Next, `get_auth_code` is called to process the authorization code from the `auth_url`. Finally, `authenticate` is called to authenticate the app with the `code`. |
| 27 | + |
| 28 | +```python |
| 29 | +client = onedrivesdk.get_default_client(client_id='00000000481695BB', |
| 30 | + scopes=['wl.signin', |
| 31 | + 'wl.offline_access', |
| 32 | + 'onedrive.readwrite']) |
| 33 | + auth_url = client.auth_provider.get_auth_url(redirect_uri) |
| 34 | + |
| 35 | + # Block thread until we have the code |
| 36 | + code = GetAuthCodeServer.get_auth_code(auth_url, redirect_uri) |
| 37 | + # Finally, authenticate! |
| 38 | + client.auth_provider.authenticate(code, redirect_uri, client_secret) |
| 39 | +``` |
| 40 | + |
| 41 | +### List item children |
| 42 | + |
| 43 | +The sample displays all items in the user's OneDrive, starting with the root: |
| 44 | + |
| 45 | +```python |
| 46 | +item_id = "root" |
| 47 | +... |
| 48 | +items = navigate(client, item_id) |
| 49 | +... |
| 50 | +def navigate(client, item_id): |
| 51 | + items = client.item(id=item_id).children.get() |
| 52 | + return items |
| 53 | +``` |
| 54 | +### View thumbnails for an item |
| 55 | + |
| 56 | +Once an item is selected, the thumbnail for the item is downloaded into a temporary JPG file, and then displayed: |
| 57 | + |
| 58 | +```python |
| 59 | +client.item(id=item_id).thumbnails[0].small.download("./tmp_thumb.jpg") |
| 60 | +image = Image.open("./tmp_thumb.jpg") |
| 61 | +image.show() |
| 62 | +``` |
| 63 | + |
| 64 | +### Delete an item |
| 65 | + |
| 66 | +To delete an item, call `delete()` with the `item_id`: |
| 67 | + |
| 68 | +```python |
| 69 | +def delete(client, item_id): |
| 70 | + confirm = input("Confirm delete? Y/N: ") |
| 71 | + if confirm == "Y": |
| 72 | + client.item(id=item_id).delete() |
| 73 | +``` |
| 74 | + |
| 75 | +### Copy an item |
| 76 | + |
| 77 | +In this example, a new `ItemReference()` object is created, and then the item is copied to the ItemReference. |
| 78 | + |
| 79 | +```python |
| 80 | +def paste(client, item_id, copy_item_ids): |
| 81 | + ref = onedrivesdk.ItemReference() |
| 82 | + ref.id = item_id |
| 83 | + for id in copy_item_ids: |
| 84 | + client.item(id=id).copy(parent_reference=ref).post() |
| 85 | +``` |
| 86 | + |
| 87 | +### Get a sharing link |
| 88 | + |
| 89 | +This example gets a sharing link by calling the `create_link` method with `post()`. The type of link requires an input from the user. |
| 90 | + |
| 91 | +```python |
| 92 | +def get_sharing_link(client, item_id): |
| 93 | + action = int(input("Type? 1:View 2:Edit... ")) |
| 94 | + permission = client.item(id=item_id).create_link("view" if action == 1 else "edit").post() |
| 95 | + print("\n{}\n".format(permission.link.web_url)) |
| 96 | +``` |
| 97 | + |
| 98 | +### List changes for an item |
| 99 | + |
| 100 | +OneDrive keeps track of changes for an item. In this example, the `delta` method is called on an item to list all changes for that item. The token represents the last sync token you got from the call to 'delta'. |
| 101 | + |
| 102 | +```python |
| 103 | +def list_changes(client, item_id, token): |
| 104 | + collection_page = client.item(id=item_id).delta(token).get() |
| 105 | + for item in collection_page: |
| 106 | + print(item.name) |
| 107 | + |
| 108 | + print("TOKEN: {}".format(collection_page.token)) |
| 109 | +``` |
| 110 | + |
| 111 | +## More resources |
| 112 | + |
| 113 | +* [OneDrive SDK for Python](https://github.com/OneDrive/onedrive-sdk-python/) |
| 114 | +* [OneDrive API](https://dev.onedrive.com) |
| 115 | +* [Python](https://python.org) |
| 116 | + |
| 117 | +## Copyright |
| 118 | + |
| 119 | +Copyright (c) Microsoft. All rights reserved. |
0 commit comments