用于Python的Office 365库

office365-rest-client的Python项目详细描述


关于

Office 365和Microsoft Python图形库

使用

  1. Installation
  2. Working with SharePoint API
  3. Working with Outlook API
  4. Working with OneDrive API
  5. Working with Microsoft Teams API

状态

DownloadsPyPIPyPI pyversionsBuild Status

安装

使用pip:

pip install Office365-REST-Python-Client

使用SharePoint API

支持的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查询:

  1. ClientContext class-指向SharePoint资源的位置,如WebListItem等(推荐)
^{pr2}$

或者通过方法链接(也称为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']))
  1. 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终结点中都可用, 以下客户端可用:

身份验证

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

文档

OneDrive Graph API reference

身份验证

ADAL Python 库用于向Active Directory(AD)验证用户身份并获取令牌

示例

示例:列出可用驱动器

这个例子演示了如何枚举和打印驱动器的url 对应于^{} endpoint

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)验证用户身份并获取令牌

示例

示例:在一个组下创建一个新团队

这个例子演示了如何在一个组下创建一个新的团队 对应于^{} endpoint

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

第三方库和依赖项

安装客户端库时将安装以下库:

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java中最小长度的字数计算   java事务处理JavaEE 6   java如何进行5分钟刷新并检查txt是否被修改?   java可以同时拖动多个对象   变量长、双精度、字节、字符在Java中的用途是什么?   spring将XMLBean配置转换为java配置   java检测不可靠网络上的TCP丢失   Java:TCP加密、SSL和Netty   在java中,如何使用isAssignableFrom的映射避免多个if-else   在J2EE动态Web项目中找不到java CSS文件   java遍历领域查询到RealmList   安卓阅读网站内容Java   java如何修改/自定义/反编译Opera mini jar文件?   java死锁播放框架如何使用控制器中的参数检查@RestrictedResource?   java在MS Excel中导入xml文件我们如何使用Python或任何其他编程语言自动化此流程?   java如何暂停正在运行的线程并在需要时重新启动同一线程?