用于Python的Office 365库
office365-rest-client的Python项目详细描述
关于
Office 365和Microsoft Python图形库
使用
- Installation
- Working with SharePoint API
- Working with Outlook API
- Working with OneDrive API
- Working with Microsoft Teams API
状态
安装
使用pip:
pip install Office365-REST-Python-Client
使用SharePoint API
支持的API版本列表:
- SharePoint 2013 REST API及以上
- SharePoint Online和OneDrive for Business REST API
身份验证
支持以下身份验证流:
- 应用程序主体流:
AuthenticationContext.ctx_auth.acquire_token_for_app(client_id, client_secret)
(有关详细信息,请参阅Granting access using SharePoint App-Only) - 用户凭据流:
AuthenticationContext.ctx_auth.acquire_token_for_user(username, password)
- 证书凭据流
ClientContext.connect_with_certificate(site_url, client_id,thumbprint, certificate_path)
示例
有两种方法可用于执行API查询:
ClientContext class
-指向SharePoint资源的位置,如Web
、ListItem
等(推荐)
或者通过方法链接(也称为Fluent接口):
fromoffice365.sharepoint.client_contextimportClientContextctx=ClientContext(site_url).with_credentials(UserCredential(username,password))web=ctx.web.load().execute_query()print("Web title: {0}".format(web.properties['Title']))
- 在
RequestOptions class
-在这里构造REST查询(不涉及模型)该示例演示如何读取
在Web
属性:
importjsonfromoffice365.runtime.auth.UserCredentialimportUserCredentialfromoffice365.runtime.http.request_optionsimportRequestOptionsfromoffice365.sharepoint.client_contextimportClientContextctx=ClientContext(site_url).with_credentials(UserCredential(username,password))request=RequestOptions("{0}/_api/web/".format(site_url))response=ctx.execute_request_direct(request)json=json.loads(response.content)web_title=json['d']['Title']print("Web title: {0}".format(web_title))
使用Outlook API
支持的API列表:
由于Outlook REST API在Microsoft Graph和Outlook API终结点中都可用, 以下客户端可用:
GraphClient
目标是Outlookv2.0
版本(preferred现在,请参考transition to Microsoft Graph-based Outlook REST API以获取详细信息)OutlookClient
,目标是Outlookv1.0
版本(不建议使用,因为v1.0
版本已被弃用)
身份验证
ADAL Python 库用于向Active Directory(AD)验证用户身份并获取令牌
示例
这个例子演示了如何通过Microsoft Graph endpoint发送电子邮件。在
Note: access token is getting acquired via Client Credential flow
defget_token(auth_ctx):token=auth_ctx.acquire_token_with_client_credentials("https://graph.microsoft.com",client_id,client_secret)returntokentenant_name="contoso.onmicrosoft.com"client=GraphClient(tenant_name,get_token)message_json={"Message":{"Subject":"Meet for lunch?","Body":{"ContentType":"Text","Content":"The new cafeteria is open."},"ToRecipients":[{"EmailAddress":{"Address":"jdoe@contoso.onmicrosoft.com"}}]},"SaveToSentItems":"false"}login_name="mdoe@contoso.onmicrosoft.com"client.users[login_name].send_mail(message_json)client.execute_query()
使用OneDrive API
文档
身份验证
ADAL Python 库用于向Active Directory(AD)验证用户身份并获取令牌
示例
示例:列出可用驱动器
这个例子演示了如何枚举和打印驱动器的url
对应于^{
Note: access token is getting acquired via Client Credential flow
defget_token(auth_ctx):"""Acquire token via client credential flow (ADAL Python library is utilized)"""token=auth_ctx.acquire_token_with_client_credentials("https://graph.microsoft.com",client_id,client_secret)returntokentenant_name="contoso.onmicrosoft.com"client=GraphClient(tenant_name,get_token)drives=client.drivesclient.load(drives)client.execute_query()fordriveindrives:print("Drive url: {0}".format(drive.web_url))
示例:下载DriveItem(folder facet)的内容
# retrieve drive properties (source)drive=client.users[user_id_or_principal_name].driveclient.load(drive)client.execute_query()# download files from OneDrive into local folder withtempfile.TemporaryDirectory()aspath:download_files(drive.root,path)
在哪里
defdownload_files(remote_folder,local_path):drive_items=remote_folder.childrenclient.load(drive_items)client.execute_query()fordrive_itemindrive_items:ifnotdrive_item.file.is_server_object_null:# is file?# download file contentwithopen(os.path.join(local_path,drive_item.name),'wb')aslocal_file:drive_item.download(local_file)client.execute_query()
更多示例请参考OneDrive examples section。在
使用Microsoft Team API
身份验证
ADAL Python 库用于向Active Directory(AD)验证用户身份并获取令牌
示例
示例:在一个组下创建一个新团队
这个例子演示了如何在一个组下创建一个新的团队
对应于^{
tenant_name="contoso.onmicrosoft.com"client=GraphClient(tenant_name,get_token)new_team=client.groups[group_id].add_team()client.execute_query()
在哪里
defget_token(auth_ctx):"""Acquire token via client credential flow (ADAL Python library is utilized) :type auth_ctx: adal.AuthenticationContext """token=auth_ctx.acquire_token_with_client_credentials("https://graph.microsoft.com",client_id,client_secret)returntoken
第三方库和依赖项
安装客户端库时将安装以下库:
- 项目
标签: