One drive api python

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Simple Microsoft OneDrive Python API

License

locked-fg/onedrive

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

readme.md

Simple Microsoft OneDrive Python API

This is a more basic version of the official OneDrive-Python-API. This project is intended to abstract login etc and make interaction with OneDrive as easy as possible. This library uses the REST endpoints as documented in the OneDrive API Docs.

The library was built with Python3 for pure personal use. So downwards compatibility to Python 2 was and still is not (yet) subject of development.

  • Clone this repo
  • Call python setup.py install to build and install the library
  • Acquire Api key and API through https://apps.dev.microsoft.com
  • Save Api key and secret to onedrive_keys.json

Interact / Examples / How To

See tests/test_api.py for working code. The tests should be pretty self explanatory.

from onedrive import auth auth.login() 

The above code launches a browser window and asks for permission to interact with your 1Drive. According authentication tokens are saved in tokens.json so that you do not need to authenticate in each session.

from onedrive import auth from onedrive import api header = auth.login() # log in (open broeser or reuse login from tokens.json) api.mkdir("/api_test/x", header, parents=True) # create dir content = "1".encode("utf-8") api.upload_simple(data=content, dst="/api_test/foo.tmp", auth=header) # upload a new file res = api.copy("/api_test/foo.tmp", "/api_test/x/foo2.tmp", header) # copy the file 
from onedrive import auth from onedrive import api header = auth.login() api.delete("/api_test", header) 

Other operations (upload, download, copy, move, get meta data)

Please have a look at tests/test_api.py. All methods are documented there.

This part is copied from OneDrive API README: OneDrive has limits in place to make sure that individuals and apps do not adversely affect the experience of other users. When an activity exceeds OneDrive’s limits, API requests will be rejected for a period of time. OneDrive may also return a Retry-After header with the number of seconds your app should wait before sending more requests.

HTTP/1.1 429 Too Many Requests Retry-After: 3600 

The project is licensed under the MIT License.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

License

OneDrive/onedrive-sdk-python

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Getting started with the OneDrive SDK for Python

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

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

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

import onedrivesdk redirect_uri = 'http://localhost:8080/' client_secret = 'your_client_secret' client_id='your_client_id' api_base_url='https://api.onedrive.com/v1.0/' scopes=['wl.signin', 'wl.offline_access', 'onedrive.readwrite'] http_provider = onedrivesdk.HttpProvider() auth_provider = onedrivesdk.AuthProvider( http_provider=http_provider, client_id=client_id, scopes=scopes) client = onedrivesdk.OneDriveClient(api_base_url, auth_provider, http_provider) auth_url = client.auth_provider.get_auth_url(redirect_uri) # Ask for the code print('Paste this URL into your browser, approve the app\'s access.') print('Copy everything in the address bar after "code pl-en">print(auth_url) code = raw_input('Paste code here: ') client.auth_provider.authenticate(code, redirect_uri, client_secret)

The above code requires copy-pasting into your browser and back into your console. If you want to remove some of that manual work, you can use the helper class GetAuthCodeServer . That helper class spins up a webserver, so this method cannot be used on all environments.

import onedrivesdk from onedrivesdk.helpers import GetAuthCodeServer redirect_uri = 'http://localhost:8080/' client_secret = 'your_app_secret' scopes=['wl.signin', 'wl.offline_access', 'onedrive.readwrite'] client = onedrivesdk.get_default_client( client_id='your_client_id', scopes=scopes) 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.

To interact with the OneDrive API, your app must authenticate for a specific resource. Your app must first use the Resource Discovery helper to find out which service you can access. Then, you can build a client to access those resources. This uses a slightly different auth flow than the standard code flow — note the use of redeem_refresh_token with the service_resource_id of the service you want to access.

import onedrivesdk from onedrivesdk.helpers import GetAuthCodeServer from onedrivesdk.helpers.resource_discovery import ResourceDiscoveryRequest redirect_uri = 'http://localhost:8080' client_id = your_client_id client_secret = your_client_secret discovery_uri = 'https://api.office.com/discovery/' auth_server_url='https://login.microsoftonline.com/common/oauth2/authorize' auth_token_url='https://login.microsoftonline.com/common/oauth2/token' http = onedrivesdk.HttpProvider() auth = onedrivesdk.AuthProvider(http, client_id, auth_server_url=auth_server_url, auth_token_url=auth_token_url) auth_url = auth.get_auth_url(redirect_uri) code = GetAuthCodeServer.get_auth_code(auth_url, redirect_uri) auth.authenticate(code, redirect_uri, client_secret, resource=discovery_uri) # If you have access to more than one service, you'll need to decide # which ServiceInfo to use instead of just using the first one, as below. service_info = ResourceDiscoveryRequest().get_service_info(auth.access_token)[0] auth.redeem_refresh_token(service_info.service_resource_id) client = onedrivesdk.OneDriveClient(service_info.service_resource_id + '/_api/v2.0/', auth, http)

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

returned_item = client.item(drive='me', id='root').children['newfile.txt'].upload('./path_to_file.txt')
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')
f = onedrivesdk.Folder() i = onedrivesdk.Item() i.name = 'New Folder' i.folder = f returned_item = client.item(drive='me', id='root').children.add(i)
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()
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 = onedrivesdk.ChildrenCollectionRequest.get_next_page_request(collection, client).get()

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))

Saving and Loading a Session

You can save your OAuth session details so that you don’t have to go through the full OAuth flow every time you start your app. To do so, follow these steps:

auth_provider = onedrivesdk.AuthProvider(http_provider, client_id, scopes) auth_provider.authenticate(code, redirect_uri, client_secret) # Save the session for later auth_provider.save_session() #### Next time you start the app #### auth_provider = onedrivesdk.AuthProvider(http_provider, client_id, scopes) auth_provider.load_session() auth_provider.refresh_token() client = onedrivesdk.OneDriveClient(base_url, auth_provider, http_provider)

After the call to refresh_token() your AuthProvider will be ready to authenticate calls to the OneDrive API. This implementation is not complete, though.

  1. The default implementation of Session saves the session information in a Pickle file. Session data should be treated with equal protection as a password, so this is not safe for deployment to real users. You should re-implement Session to fit your app’s needs.
  2. Calling .load_session() may throw an exception, depending on your implementation of Session . For example, the default implementation tries to open the file session.pickle , which may not exist and will raise FileNotFoundError . You will need to account for that here (or, even better, in your implementation of Session ).

If you need to proxy your requests, you can use the helper class HttpProviderWithProxy .

import onedrivesdk from onedrivesdk.helpers import http_provider_with_proxy proxy = < 'http': 'http://localhost:8888', 'https': 'https://localhost:8888' > http = http_provider_with_proxy.HttpProviderWithProxy(proxy, verify_ssl=True) auth = onedrivesdk.AuthProvider(http, my_client_id, ['onedrive.readwrite']) client = onedrivesdk.OneDriveClient(my_base_url, auth, http)

All requests using that client will be proxied.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Источник

Читайте также:  Html all color tags
Оцените статью