1
votes

I'm looking to list files that have been shared with me on OneDrive for business with the Python OneDrive SDK (onedrive-python-sdk)

I've successfully authenticated and been able to list files I have using the following code

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)

#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()
# print files
print collection

However I'm not sure how to request the files that have been shared with me, I've seen references in the OneDrive API to use the following request, but I'm not sure how to make the call

GET /drive/view.sharedWithMe

Any help would be appreciated

1

1 Answers

0
votes

I'll prefix this answer with a disclaimer that I don't have an environment set up to validate this python solution, but I believe it should work (or worst case provide you with an idea to solve it).

You're correct in that the current SDK does not expose the view.sharedWithMe, however it thankfully contains the tools you need to do it yourself. You'll want to use the append_to_request_url function in conjunction with the ItemsCollectionRequestBuilder like so:

collection = ItemsCollectionRequestBuilder(client.drive.append_to_request_url("view.sharedWithMe"), client).request(top=3).get()

Hopefully this will give you the results that you're after.