python - 使用 Python 访问 OneDrive 时使用令牌代码
问题描述
我正在编写一些代码来将文件移动到 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。
还有其他方法吗?或者其他要使用的库?
解决方案
您可以尝试使用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')
有关更多示例,您可以参考此链接。
推荐阅读
- windows - 为什么 CMake 3.16 会寻找 boost_program_options
而不是 libboost_program_options ? - vue.js - Vuetify 数据表中选定行的计算属性未刷新
- r - 使用分类数据创建折线图
- html - 从 Vue 中的 html 和 body 元素中删除边距和填充?
- css - Dompdf对齐两个文本块
- javascript - 将 Treant.js 合并到 Vue.js 应用程序中
- ruby-on-rails - 使用 roo gem 将从 xlsx 提取的哈希存储在 ruby 中的数据库中
- ios - 为什么我不能在模拟器中点击 uibutton 但我可以在我的硬件 iPhone 上点击?
- flutter - 为什么我的异步函数返回一个 Future
每个人都在等待? - django - quizgame 在生产中随机获取重复项