使用Python访问OneDrive时使用令牌代码

问题描述 投票:0回答:1

我正在编写一些代码来将文件移到OneDrive(企业帐户)。我的应用程序在Azure AD中进行了身份验证,并且应该具有正确的访问权限(MS Graph中的Files.ReadWrite.All,Office365 SPO中的Sites.ReadWrite.All和Azure AD中的User.Read)。

接收应用程序令牌的代码工作正常:

import msal

client_id = 'dc185bb*************6bcda94'
authority_host_uri = 'https://login.microsoftonline.com'
discovery_uri = 'https://api.office.com/discovery/'
client_secret = 'VsY7vV**************ToiA0='
tenant = '4a6*********************65079'
authority_uri = authority_host_uri + '/' + tenant
scopes=['https://graph.microsoft.com/.default']

app = msal.ConfidentialClientApplication(
    client_id=client_id, authority=authority_uri,
    client_credential=client_secret)

result = app.acquire_token_for_client(scopes=scopes)
print(result)

但是,当我尝试将此令牌与OneDrive SDK库一起使用时,我似乎无法通过它:

def __init__(self, http_provider, client_id=None, scopes=None, access_token=None, session_type=None, loop=None,
             auth_server_url=None, auth_token_url=None):
    """Initialize the authentication provider for authenticating
    requests sent to OneDrive

    Args:
        http_provider (:class:`HttpProviderBase<onedrivesdk.http_provider_base>`):
            The HTTP provider to use for all auth requests
        client_id (str): Defaults to None, the client id for your
            application
        scopes (list of str): Defaults to None, the scopes 
            that are required for your application
        access_token (str): Defaults to None. Not used in this implementation.

以上内容来自onedrivesdk的auth_provider.py部分,并明确指出access_token未在实现中使用。

还有另一种方法吗?还是要使用其他库?

python azure-active-directory onedrive azure-ad-graph-api msal
1个回答
0
投票

您可以尝试使用OneDrive for Business的身份验证。

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)

上传项目:

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

有关更多示例,请参阅此link

© www.soinside.com 2019 - 2024. All rights reserved.