OneDrive API Python SDK - points to login.live.com

2019-05-25 23:43发布

问题:

I am trying to use the OneDrive API Python SDK to upload files to an Office 365 E3 account SharePoint folder.

As described for OneDrive for business / SharePoint files I am using Azure AD that is included in my Office 365 E3 account for auth and have created a native client application app in Azure AD management.

I would expect that I need to point auth to Office 365:

mydomain.sharepoint.com

However, it appears the OneDrive API Python SDK (auth_provider.py) points auth to:

AUTH_SERVER_URL = "https://login.live.com/oauth20_authorize.srf"
AUTH_TOKEN_URL = "https://login.live.com/oauth20_token.srf"

This Github issue discussion indicates OneDrive API Business is still in beta but just changing base urls to mydomain.sharepoint.com urls is all that is needed to use SDK for OneDrive API Business eg:

AUTH_SERVER_URL = "https://mydomain.sharepoint.com/oauth20_authorize.srf"
AUTH_TOKEN_URL = "https://mydomain.sharepoint.com/oauth20_token.srf"

Is this correct?

Edited to ensure related additional questions are addressed too:

Is there anything else other than the auth urls that needs to be modified in the OneDrive API Python SDK to be used for OneDrive for Business / Sharepoint?

The Github README includes sample code for authentication which requires client_secret and scopes to be identified.

However the Azure Active Directory app creation process includes scope identification, and native client app doesn't require client_secret.

For my native client app authorization, I have just left client_secret and scopes blank in the sample code eg:

client_secret = ""
client = onedrivesdk.get_default_client(client_id='xxxxxetc', 
                                            scopes=[])

回答1:

Auth for OneDrive for Business is handled by AAD, which means you need to point to the AAD OAuth 2 end points, which are:

AUTH_SERVER_URL = "https://login.microsoftonline.com/common/oauth2/authorize"
AUTH_TOKEN_URL = "https://login.microsoftonline.com/common/oauth2/token"

This is roughly documented here, https://dev.onedrive.com/auth/aad_oauth.htm, although since that is describing the authentication flow the details are a bit hidden if you were just looking for the two URLs to use with the SDK.



回答2:

If you take a look at the onedrivesdk_helpers module, you'll see that it defaults to api.onedrive.com. I would recommend using the following code in place of get_default_client:

http_provider = HttpProvider()
auth_provider = AuthProvider(http_provider=http_provider,
                             client_id="your_app_id",
                             scopes=[])
client = OneDriveClient("mydomain.sharepoint.com",
                        auth_provider,
                        http_provider)

Make sure to import those classes with from onedrivesdk import HttpProvider, AuthProvider, OneDriveClient