Code Monkey home page Code Monkey logo

onedrive-sdk-python's Introduction

Getting started with the OneDrive SDK for Python


Build status

Installation

Once you've downloaded the OneDrive SDK for Python, open a command prompt and type the following to install it:

pip install onedrivesdk

Next, include the SDK in your Python project by adding:

import onedrivesdk

Authentication

To interact with the OneDrive API, your app must authenticate. You can use the following code sample to do so.

import onedrivesdk
from onedrivesdk.helpers import GetAuthCodeServer

redirect_uri = "http://localhost:8080/"
client_secret = "your_app_secret"

client = onedrivesdk.get_default_client(client_id='your_client_id',
                                       	scopes=['wl.signin',
                                               	'wl.offline_access',
                                               	'onedrive.readwrite'])

auth_url = client.auth_provider.get_auth_url(redirect_uri)

#this will block until we have the code
code = GetAuthCodeServer.get_auth_code(auth_url, redirect_uri)

client.auth_provider.authenticate(code, redirect_uri, client_secret)

Once your app is authenticated, you should have access to the OneDrive API, and can begin making calls using the SDK.

Examples

Note: All examples assume that your app has already been Authenticated.

Upload an Item

returned_item = client.item(drive="me", id="root").children["newfile.txt"].upload("./path_to_file.txt")

Download an Item

root_folder = client.item(drive="me", id="root").children.get()
id_of_file = root_folder[0].id

client.item(drive="me", id=id_of_file).download("./path_to_download_to.txt")

Add a folder

f = onedrivesdk.Folder()
i = onedrivesdk.Item()
i.name = "New Folder"
i.folder = f

returned_item = client.item(drive="me", id="root").children.add(i)

Copy an Item

from onedrivesdk.item_reference import ItemReference

ref = ItemReference()
ref.id = "yourparent!id" #path also supported

copy_operation = client.item(drive="me", id="youritemtocopy!id").copy(name="new copied name", parent_reference=ref).post()

#copy_operation.item will return None until the copy has completed.
#If you would like to block until the operation has been completed
#and copy_operation.item is no longer None
copy_operation.poll_until_complete()

Rename an Item

renamed_item = onedrivesdk.Item()
renamed_item.name = "NewItemName"
renamed_item.id = "youritemtorename!id"

new_item = client.item(drive="me", id=renamed_item.id).update(renamed_item)

Paging through a collection

#get the top three elements of root, leaving the next page for more elements
collection = client.item(drive="me", id="root").children.request(top=3).get()

#get the first item in the collection
item = collection[0]

#get the next page of three elements, if none exist, returns None
collection2 = collection.next_page_request.get()

Async operations

For async operations, you create an asyncio.coroutine which implements asyncio.ascompleted, and execute it with loop.run\_until\_complete.

import asyncio

@asyncio.coroutine
def run_gets(client):
    coroutines = [client.drive("me").request().get_async() for i in range(3)]
    for future in asyncio.as_completed(coroutines):
        drive = yield from future
        print(drive.id)

loop = asyncio.get_event_loop()
loop.run_until_complete(run_gets(client))   

onedrive-sdk-python's People

Contributors

anadorr avatar cdmayer avatar mimisasouvanh avatar rgregg avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.