|
| 1 | +**Getting Started with the OneDrive Python SDK** |
| 2 | + |
| 3 | +------------------------------------------------------------------------ |
| 4 | +[](https://ci.appveyor.com/project/OneDrive/vroom-client-python) |
| 5 | + |
| 6 | +Installation |
| 7 | +============ |
| 8 | + |
| 9 | +To install the OneDrive Python SDK, open a command prompt and type: |
| 10 | + |
| 11 | +<pre><code>pip install onedrivesdk |
| 12 | +</code></pre> |
| 13 | +Next, include the SDK in your python project by adding |
| 14 | + |
| 15 | +<pre><code>import onedrivesdk</code></pre> |
| 16 | + |
| 17 | +Authentication |
| 18 | +============== |
| 19 | + |
| 20 | +To interact with the OneDrive API, you must authenticate. You can use the following code sample to do so. |
| 21 | + |
| 22 | +<pre><code>import onedrivesdk |
| 23 | +from onedrivesdk.helpers import GetAuthCodeServer |
| 24 | + |
| 25 | +redirect_uri = "http://localhost:8080/" |
| 26 | +client_secret = "your_app_secret" |
| 27 | + |
| 28 | +client = onedrivesdk.get_default_client(client_id='your_client_id', |
| 29 | + scopes=['wl.signin', |
| 30 | + 'wl.offline_access', |
| 31 | + 'onedrive.readwrite']) |
| 32 | + |
| 33 | +auth_url = client.auth_provider.get_auth_url(redirect_uri) |
| 34 | + |
| 35 | +#this will block until we have the code |
| 36 | +code = GetAuthCodeServer.get_auth_code(auth_url, redirect_uri) |
| 37 | + |
| 38 | +client.auth_provider.authenticate(code, redirect_uri, client_secret) |
| 39 | +</code></pre> |
| 40 | + |
| 41 | +Once you are authenticated, you should have access to the OneDrive API, and |
| 42 | +can begin making calls using the SDK! |
| 43 | + |
| 44 | +Examples |
| 45 | +======== |
| 46 | + |
| 47 | +NOTE: All examples assume that the |
| 48 | +[Authentication](#authentication) has already occured. |
| 49 | + |
| 50 | +Upload an Item |
| 51 | +-------------- |
| 52 | + |
| 53 | +<pre><code>returned_item = client.item(drive="me", id="root").children["newfile.txt"].upload("./path_to_file.txt")</code></pre> |
| 54 | + |
| 55 | +Download an Item |
| 56 | +---------------- |
| 57 | + |
| 58 | +<pre><code>root_folder = client.item(drive="me", id="root").children.get() |
| 59 | +id_of_file = root_folder[0].id |
| 60 | + |
| 61 | +client.item(drive="me", id=id_of_file).download("./path_to_download_to.txt") |
| 62 | +</code></pre> |
| 63 | + |
| 64 | +Add a Folder |
| 65 | +------------ |
| 66 | + |
| 67 | +<pre><code>f = onedrivesdk.Folder() |
| 68 | +i = onedrivesdk.Item() |
| 69 | +i.name = "New Folder" |
| 70 | +i.folder = f |
| 71 | + |
| 72 | +returned_item = client.item(drive="me", id="root").children.add(i) |
| 73 | +</code></pre> |
| 74 | + |
| 75 | +Copying an Item |
| 76 | +--------------- |
| 77 | + |
| 78 | +<pre><code>from onedrivesdk.item_reference import ItemReference |
| 79 | + |
| 80 | +ref = ItemReference() |
| 81 | +ref.id = "yourparent!id" #path also supported |
| 82 | + |
| 83 | +copy_operation = client.item(drive="me", id="youritemtocopy!id").copy(name="new copied name", parent_reference=ref).post() |
| 84 | + |
| 85 | +#copy_operation.item will return None until the copy has completed. |
| 86 | +#If you would like to block until the operation has been completed |
| 87 | +#and copy_operation.item is no longer None |
| 88 | +copy_operation.poll_until_complete() |
| 89 | + |
| 90 | +</code></pre> |
| 91 | + |
| 92 | +Renaming an Item |
| 93 | +---------------- |
| 94 | + |
| 95 | +<pre><code>renamed_item = onedrivesdk.Item() |
| 96 | +renamed_item.name = "NewItemName" |
| 97 | +renamed_item.id = "youritemtorename!id" |
| 98 | + |
| 99 | +new_item = client.item(drive="me", id=renamed_item.id).update(renamed_item) |
| 100 | +</code></pre> |
| 101 | + |
| 102 | +Paging through a Collection |
| 103 | +--------------------------- |
| 104 | + |
| 105 | +<pre><code>#get the top 3 elements of root, leaving the next page for more elements |
| 106 | +collection = client.item(drive="me", id="root").children.request(top=3).get() |
| 107 | + |
| 108 | +#get the first item in the collection |
| 109 | +item = collection[0] |
| 110 | + |
| 111 | +#get next page of 3 elements, if none exist, returns None |
| 112 | +collection2 = collection.next_page_request.get() |
| 113 | +</code></pre> |
| 114 | + |
| 115 | +Async operations |
| 116 | +---------------- |
| 117 | + |
| 118 | +<p> |
| 119 | +For async operations, you create an asyncio.coroutine which |
| 120 | +implements asyncio.ascompleted, and execute it with |
| 121 | +loop.run\_until\_complete. |
| 122 | + |
| 123 | +<pre><code>import asyncio |
| 124 | + |
| 125 | +@asyncio.coroutine |
| 126 | +def run_gets(client): |
| 127 | + coroutines = [client.drive("me").request().get_async() for i in range(3)] |
| 128 | + for future in asyncio.as_completed(coroutines): |
| 129 | + drive = yield from future |
| 130 | + print(drive.id) |
| 131 | + |
| 132 | +loop = asyncio.get_event_loop() |
| 133 | +loop.run_until_complete(run_gets(client)) |
| 134 | +</code></pre> |
0 commit comments