{"id":13442323,"url":"https://github.com/OneDrive/onedrive-sdk-python","last_synced_at":"2025-03-20T13:33:22.644Z","repository":{"id":37602493,"uuid":"41940260","full_name":"OneDrive/onedrive-sdk-python","owner":"OneDrive","description":"OneDrive SDK for Python! https://dev.onedrive.com ","archived":true,"fork":false,"pushed_at":"2023-11-05T07:23:09.000Z","size":337,"stargazers_count":1087,"open_issues_count":1,"forks_count":188,"subscribers_count":50,"default_branch":"master","last_synced_at":"2025-02-24T01:40:09.819Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/OneDrive.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2015-09-04T22:23:01.000Z","updated_at":"2025-02-07T04:44:41.000Z","dependencies_parsed_at":"2024-01-14T15:24:47.772Z","dependency_job_id":"a7c0c66e-f925-4dea-a53a-baa6fd021fc0","html_url":"https://github.com/OneDrive/onedrive-sdk-python","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OneDrive%2Fonedrive-sdk-python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OneDrive%2Fonedrive-sdk-python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OneDrive%2Fonedrive-sdk-python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OneDrive%2Fonedrive-sdk-python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/OneDrive","download_url":"https://codeload.github.com/OneDrive/onedrive-sdk-python/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244619276,"owners_count":20482390,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T03:01:44.338Z","updated_at":"2025-03-20T13:33:22.279Z","avatar_url":"https://github.com/OneDrive.png","language":"Python","funding_links":[],"categories":["API","Python"],"sub_categories":["Windows Manager"],"readme":"# Getting started with the OneDrive SDK for Python\n\n------------------------------------------------------------------------\n[![Build status](https://ci.appveyor.com/api/projects/status/x1cjahp817w6r455?svg=true)](https://ci.appveyor.com/project/OneDrive/vroom-client-python)\n\n## Installation\n\nOnce you've downloaded the OneDrive SDK for Python, open a command prompt and type the following to install it:\n\n\u003cpre\u003e\u003ccode\u003epip install onedrivesdk\u003c/code\u003e\u003c/pre\u003e\n\nNext, include the SDK in your Python project by adding:\n\n\u003cpre\u003e\u003ccode\u003eimport onedrivesdk\u003c/code\u003e\u003c/pre\u003e\n\n## Authentication\n\n### OneDrive\n\nTo interact with the OneDrive API, your app must authenticate. You can use the following code sample to do so.\n\n```python\nimport onedrivesdk\n\nredirect_uri = 'http://localhost:8080/'\nclient_secret = 'your_client_secret'\nclient_id='your_client_id'\napi_base_url='https://api.onedrive.com/v1.0/'\nscopes=['wl.signin', 'wl.offline_access', 'onedrive.readwrite']\n\nhttp_provider = onedrivesdk.HttpProvider()\nauth_provider = onedrivesdk.AuthProvider(\n    http_provider=http_provider,\n    client_id=client_id,\n    scopes=scopes)\n\nclient = onedrivesdk.OneDriveClient(api_base_url, auth_provider, http_provider)\nauth_url = client.auth_provider.get_auth_url(redirect_uri)\n# Ask for the code\nprint('Paste this URL into your browser, approve the app\\'s access.')\nprint('Copy everything in the address bar after \"code=\", and paste it below.')\nprint(auth_url)\ncode = raw_input('Paste code here: ')\n\nclient.auth_provider.authenticate(code, redirect_uri, client_secret)\n```\n\nThe above code requires copy-pasting into your browser and back into your console. If you want to remove some of\nthat manual work, you can use the helper class `GetAuthCodeServer`. That helper class spins up a webserver, so\nthis method cannot be used on all environments.\n\n```python\nimport onedrivesdk\nfrom onedrivesdk.helpers import GetAuthCodeServer\n\nredirect_uri = 'http://localhost:8080/'\nclient_secret = 'your_app_secret'\nscopes=['wl.signin', 'wl.offline_access', 'onedrive.readwrite']\n\nclient = onedrivesdk.get_default_client(\n    client_id='your_client_id', scopes=scopes)\n\nauth_url = client.auth_provider.get_auth_url(redirect_uri)\n\n#this will block until we have the code\ncode = GetAuthCodeServer.get_auth_code(auth_url, redirect_uri)\n\nclient.auth_provider.authenticate(code, redirect_uri, client_secret)\n```\n\nOnce your app is authenticated, you should have access to the OneDrive API, and\ncan begin making calls using the SDK.\n\n### OneDrive for Business\n\nTo interact with the OneDrive API, your app must authenticate for a specific resource. Your\napp must first use the Resource Discovery helper to find out which service you can access.\nThen, you can build a client to access those resources. This uses a slightly different\nauth flow than the standard code flow - note the use of `redeem_refresh_token` with\nthe `service_resource_id` of the service you want to access.\n\n```python\nimport onedrivesdk\nfrom onedrivesdk.helpers import GetAuthCodeServer\nfrom onedrivesdk.helpers.resource_discovery import ResourceDiscoveryRequest\n\nredirect_uri = 'http://localhost:8080'\nclient_id = your_client_id\nclient_secret = your_client_secret\ndiscovery_uri = 'https://api.office.com/discovery/'\nauth_server_url='https://login.microsoftonline.com/common/oauth2/authorize'\nauth_token_url='https://login.microsoftonline.com/common/oauth2/token'\n\nhttp = onedrivesdk.HttpProvider()\nauth = onedrivesdk.AuthProvider(http,\n                                client_id,\n                                auth_server_url=auth_server_url,\n                                auth_token_url=auth_token_url)\nauth_url = auth.get_auth_url(redirect_uri)\ncode = GetAuthCodeServer.get_auth_code(auth_url, redirect_uri)\nauth.authenticate(code, redirect_uri, client_secret, resource=discovery_uri)\n# If you have access to more than one service, you'll need to decide\n# which ServiceInfo to use instead of just using the first one, as below.\nservice_info = ResourceDiscoveryRequest().get_service_info(auth.access_token)[0]\nauth.redeem_refresh_token(service_info.service_resource_id)\nclient = onedrivesdk.OneDriveClient(service_info.service_resource_id + '/_api/v2.0/', auth, http)\n```\n\n## Examples\n\n**Note:** All examples assume that your app has already been\n[Authenticated](#authentication).\n\n### Upload an Item\n\n```python\nreturned_item = client.item(drive='me', id='root').children['newfile.txt'].upload('./path_to_file.txt')\n```\n\n### Download an Item\n\n```python\nroot_folder = client.item(drive='me', id='root').children.get()\nid_of_file = root_folder[0].id\n\nclient.item(drive='me', id=id_of_file).download('./path_to_download_to.txt')\n```\n\n### Add a folder\n\n```python\nf = onedrivesdk.Folder()\ni = onedrivesdk.Item()\ni.name = 'New Folder'\ni.folder = f\n\nreturned_item = client.item(drive='me', id='root').children.add(i)\n```\n\n### Copy an Item\n\n```python\nfrom onedrivesdk.item_reference import ItemReference\n\nref = ItemReference()\nref.id = 'yourparent!id' #path also supported\n\ncopy_operation = client.item(drive='me', id='youritemtocopy!id').copy(name='new copied name', parent_reference=ref).post()\n\n#copy_operation.item will return None until the copy has completed.\n#If you would like to block until the operation has been completed\n#and copy_operation.item is no longer None\ncopy_operation.poll_until_complete()\n\n```\n\n### Rename an Item\n\n```python\nrenamed_item = onedrivesdk.Item()\nrenamed_item.name = 'NewItemName'\nrenamed_item.id = 'youritemtorename!id'\n\nnew_item = client.item(drive='me', id=renamed_item.id).update(renamed_item)\n```\n\n### Paging through a collection\n\n```python\n#get the top three elements of root, leaving the next page for more elements\ncollection = client.item(drive='me', id='root').children.request(top=3).get()\n\n#get the first item in the collection\nitem = collection[0]\n\n#get the next page of three elements, if none exist, returns None\ncollection2 = onedrivesdk.ChildrenCollectionRequest.get_next_page_request(collection, client).get()\n```\n\n### Async operations\n\nFor async operations, you create an `asyncio.coroutine` which\nimplements `asyncio.ascompleted`, and execute it with\n`loop.run\\_until\\_complete`.\n\n```python\nimport asyncio\n\n@asyncio.coroutine\ndef run_gets(client):\n    coroutines = [client.drive('me').request().get_async() for i in range(3)]\n    for future in asyncio.as_completed(coroutines):\n        drive = yield from future\n        print(drive.id)\n\nloop = asyncio.get_event_loop()\nloop.run_until_complete(run_gets(client))   \n```\n\n## Saving and Loading a Session\n\nYou can save your OAuth session details so that you don't have to go through the full\nOAuth flow every time you start your app. To do so, follow these steps:\n\n```python\nauth_provider = onedrivesdk.AuthProvider(http_provider,\n                                         client_id,\n                                         scopes)\nauth_provider.authenticate(code, redirect_uri, client_secret)\n\n# Save the session for later\nauth_provider.save_session()\n\n#### Next time you start the app ####\nauth_provider = onedrivesdk.AuthProvider(http_provider,\n                                         client_id,\n                                         scopes)\nauth_provider.load_session()\nauth_provider.refresh_token()\nclient = onedrivesdk.OneDriveClient(base_url, auth_provider, http_provider)\n```\n\nAfter the call to `refresh_token()` your `AuthProvider` will be ready to authenticate calls\nto the OneDrive API. This implementation is not complete, though.\n\n1. The default implementation of [Session](\\src\\onedrivesdk\\session.py) saves the session\ninformation in a Pickle file. Session data should be treated with equal protection as a\npassword, so this is not safe for deployment to real users. You should re-implement\n`Session` to fit your app's needs.\n2. Calling `.load_session()` may throw an exception, depending on your implementation\nof `Session`. For example, the default implementation tries to open the file `session.pickle`,\n which may not exist and will raise `FileNotFoundError`. You will need to account for that here\n (or, even better, in your implementation of `Session`).\n\n## Using a Proxy\nIf you need to proxy your requests, you can use the helper class `HttpProviderWithProxy`.\n```python\nimport onedrivesdk\nfrom onedrivesdk.helpers import http_provider_with_proxy\n\nproxy = {\n    'http': 'http://localhost:8888',\n    'https': 'https://localhost:8888'\n}\nhttp = http_provider_with_proxy.HttpProviderWithProxy(proxy, verify_ssl=True)\nauth = onedrivesdk.AuthProvider(http, my_client_id, ['onedrive.readwrite'])\nclient = onedrivesdk.OneDriveClient(my_base_url, auth, http)\n```\n\nAll requests using that client will be proxied.\n\n## Code of Conduct\n\nThis project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FOneDrive%2Fonedrive-sdk-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FOneDrive%2Fonedrive-sdk-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FOneDrive%2Fonedrive-sdk-python/lists"}