Microsoft 365 & Microsoft กราฟกราฟสำหรับ Python
ใช้ PIP:
pip install Office365-REST-Python-Client
อีกทางเลือกหนึ่งสามารถติดตั้งเวอร์ชัน ล่าสุด ได้โดยตรงผ่าน GitHub:
pip install git+https://github.com/vgrem/Office365-REST-Python-Client.git
สำหรับตัวอย่างต่อไปนี้ข้อมูลประจำตัวที่เกี่ยวข้องสามารถพบได้ในพอร์ทัล Azure
ขั้นตอนในการเข้าถึง:
ไคลเอนต์ ClientContext
ให้การสนับสนุนสำหรับการพัก SharePoint แบบดั้งเดิม
สนับสนุนการไหลของการรับรองความถูกต้องต่อไปนี้:
วิธีการรับรองความถูกต้องนี้เข้ากันได้กับ SharePoint ในสถานที่และยังคงเป็นรูปแบบที่เกี่ยวข้องใน SharePoint ในสถานที่เป็น SharePoint Online วิธีการต่อไปนี้มีอยู่:
ClientContext.with_credentials(client_credentials)
ClientContext.with_client_credentials(client_id, client_secret)
การใช้งาน:
client_credentials = ClientCredential('{client_id}','{client_secret}')
ctx = ClientContext('{url}').with_credentials(client_credentials)
เอกสาร:
ตัวอย่าง: connect_with_app_principal.py
การใช้งาน:
user_credentials = UserCredential('{username}','{password}')
ctx = ClientContext('{url}').with_credentials(user_credentials)
ตัวอย่าง: connect_with_user_credential.py
เอกสาร:
ตัวอย่าง: connect_with_client_certificate.py
เพื่อเข้าสู่ระบบแบบโต้ตอบเช่นผ่านเบราว์เซอร์ท้องถิ่น
วิชาบังคับก่อน:
ในพอร์ทัล Azure กำหนดค่า URI ของ "แอปพลิเคชันมือถือและเดสก์ท็อป" ของคุณเป็น
http://localhost
ตัวอย่าง: connect_interactive.py
การใช้งาน:
from office365 . sharepoint . client_context import ClientContext
ctx = ClientContext ( "{site-url}" ). with_interactive ( "{tenant-name-or-id}" , "{client-id}" )
me = ctx . web . current_user . get (). execute_query ()
print ( me . login_name )
มี สองวิธี ในการดำเนินการค้นหา API:
ClientContext class
- ที่ที่คุณกำหนดเป้าหมายทรัพยากร SharePoint เช่น Web
, ListItem
และ ETC (แนะนำ) from office365 . runtime . auth . user_credential import UserCredential
from office365 . sharepoint . client_context import ClientContext
site_url = "https://{your-tenant-prefix}.sharepoint.com"
ctx = ClientContext ( site_url ). with_credentials ( UserCredential ( "{username}" , "{password}" ))
web = ctx . web
ctx . load ( web )
ctx . execute_query ()
print ( "Web title: {0}" . format ( web . properties [ 'Title' ]))
หรือทางเลือกหนึ่งผ่านการผูกมัดวิธีการ (AKA Fluent Interface):
from office365 . runtime . auth . user_credential import UserCredential
from office365 . sharepoint . client_context import ClientContext
site_url = "https://{your-tenant-prefix}.sharepoint.com"
ctx = ClientContext ( site_url ). with_credentials ( UserCredential ( "{username}" , "{password}" ))
web = ctx . web . get (). execute_query ()
print ( "Web title: {0}" . format ( web . properties [ 'Title' ]))
SharePointRequest class
- ที่ที่คุณสร้างแบบสอบถาม REST (และไม่มีแบบจำลองใด ๆ ที่เกี่ยวข้อง)
ตัวอย่างแสดงวิธีการอ่านคุณสมบัติ Web
:
import json
from office365 . runtime . auth . user_credential import UserCredential
from office365 . sharepoint . request import SharePointRequest
site_url = "https://{your-tenant-prefix}.sharepoint.com"
request = SharePointRequest ( site_url ). with_credentials ( UserCredential ( "{username}" , "{password}" ))
response = request . execute_request ( "web" )
json = json . loads ( response . content )
web_title = json [ 'd' ][ 'Title' ]
print ( "Web title: {0}" . format ( web_title ))
รายการตัวอย่าง:
การทำงานกับไฟล์
ทำงานกับรายการและรายการ
ส่วนตัวอย่างสำหรับสถานการณ์อื่น
กำลังดำเนินการสนับสนุนสภาพแวดล้อม SharePoint ที่ไม่ได้มาตรฐานกำลังดำเนินการอยู่ รองรับปัจจุบัน:
ในการเปิดใช้งานการรับรองความถูกต้องไปยังจุดสิ้นสุดระดับสูงของ GCC ให้เพิ่มพารามิเตอร์ environment='GCCH'
เมื่อเรียก ClientContext class
ด้วย .with_user_credentials
, .with_client_credentials
หรือ .with_credentials
ตัวอย่าง:
from office365 . sharepoint . client_context import ClientContext
client_credentials = ClientCredential ( '{client_id}' , '{client_secret}' )
ctx = ClientContext ( '{url}' ). with_credentials ( client_credentials , environment = 'GCCH' )
รายการ API ที่รองรับ:
เนื่องจาก Outlook REST APIs มีให้บริการทั้งใน Microsoft Graph และ Outlook API Endpoint ไคลเอนต์ต่อไปนี้จึงมีอยู่:
GraphClient
ซึ่งกำหนดเป้าหมายไปที่ Outlook API v2.0
เวอร์ชัน ( ดีกว่า ทุกวันนี้ให้อ้างอิงการเปลี่ยนไปยัง Outlook REST API ของ Microsoft Graph สำหรับรายละเอียด)OutlookClient
ซึ่งกำหนดเป้าหมายไปที่ Outlook API v1.0
เวอร์ชัน (ไม่แนะนำสำหรับการใช้งานเนื่องจากเวอร์ชัน v1.0
กำลังเลิกใช้แล้ว)Microsoft Authentication Library (MSAL) สำหรับ Python ซึ่งมาจากการพึ่งพานั้นใช้เป็นไลบรารีเริ่มต้นเพื่อรับโทเค็นเพื่อเรียก Microsoft Graph API
การใช้ Microsoft Authentication Library (MSAL) สำหรับ Python
หมายเหตุ: โทเค็นการเข้าถึงกำลังได้รับผ่านการไหลของลูกค้าในตัวอย่างที่ให้ไว้ รูปแบบอื่น ๆ ของการได้มาซึ่งโทเค็นสามารถพบได้ที่นี่: https://msal-python.readthedocs.io/en/latest/
import msal
from office365 . graph_client import GraphClient
def acquire_token ():
"""
Acquire token via MSAL
"""
authority_url = 'https://login.microsoftonline.com/{tenant_id_or_name}'
app = msal . ConfidentialClientApplication (
authority = authority_url ,
client_id = '{client_id}' ,
client_credential = '{client_secret}'
)
token = app . acquire_token_for_client ( scopes = [ "https://graph.microsoft.com/.default" ])
return token
client = GraphClient ( acquire_token )
แต่ในแง่ของการรับรองความถูกต้องของ Microsoft Graph API จะมีการรองรับไลบรารีที่สอดคล้องกับ OAuth Spec อื่น ๆ เช่น ADAL เช่นกัน
ใช้ Adal Python
การใช้งาน
import adal
from office365 . graph_client import GraphClient
def acquire_token_func ():
authority_url = 'https://login.microsoftonline.com/{tenant_id_or_name}'
auth_ctx = adal . AuthenticationContext ( authority_url )
token = auth_ctx . acquire_token_with_client_credentials (
"https://graph.microsoft.com" ,
"{client_id}" ,
"{client_secret}" )
return token
client = GraphClient ( acquire_token_func )
ตัวอย่างแสดงวิธีส่งอีเมลผ่านจุดสิ้นสุดของกราฟ Microsoft
หมายเหตุ: โทเค็นการเข้าถึงกำลังได้รับผ่านการไหลของลูกค้า
from office365 . graph_client import GraphClient
client = GraphClient ( acquire_token_func )
client . me . send_mail (
subject = "Meet for lunch?" ,
body = "The new cafeteria is open." ,
to_recipients = [ "[email protected]" ]
). execute_query ()
ตัวอย่างและสถานการณ์เพิ่มเติม:
อ้างถึงส่วนตัวอย่างสำหรับสถานการณ์อื่น ๆ
การอ้างอิง API กราฟ OneDrive
Microsoft Authentication Library (MSAL) สำหรับ Python ซึ่งมาจากการพึ่งพาใช้เพื่อรับโทเค็น
import msal
def acquire_token_func ():
"""
Acquire token via MSAL
"""
authority_url = 'https://login.microsoftonline.com/{tenant_id_or_name}'
app = msal . ConfidentialClientApplication (
authority = authority_url ,
client_id = '{client_id}' ,
client_credential = '{client_secret}'
)
token = app . acquire_token_for_client ( scopes = [ "https://graph.microsoft.com/.default" ])
return token
ตัวอย่างแสดงวิธีการแจกแจงและ URL ของ Print Drive ซึ่งสอดคล้องกับ list available drives
หมายเหตุ: โทเค็นการเข้าถึงกำลังได้รับผ่านการไหลของลูกค้า
from office365 . graph_client import GraphClient
tenant_name = "contoso.onmicrosoft.com"
client = GraphClient ( acquire_token_func )
drives = client . drives . get (). execute_query ()
for drive in drives :
print ( "Drive url: {0}" . format ( drive . web_url ))
from office365 . graph_client import GraphClient
client = GraphClient ( acquire_token_func )
# retrieve drive properties
drive = client . users [ "{user_id_or_principal_name}" ]. drive . get (). execute_query ()
# download files from OneDrive into local folder
with tempfile . TemporaryDirectory () as path :
download_files ( drive . root , path )
ที่ไหน
def download_files ( remote_folder , local_path ):
drive_items = remote_folder . children . get (). execute_query ()
for drive_item in drive_items :
if drive_item . file is not None : # is file?
# download file content
with open ( os . path . join ( local_path , drive_item . name ), 'wb' ) as local_file :
drive_item . download ( local_file ). execute_query ()
ตัวอย่างเพิ่มเติม:
อ้างถึงส่วนตัวอย่าง OneDrive สำหรับตัวอย่างเพิ่มเติม
Microsoft Authentication Library (MSAL) สำหรับ Python ซึ่งมาจากการพึ่งพาใช้เพื่อรับโทเค็น
ตัวอย่างแสดงให้เห็นว่าการสร้างทีมใหม่ภายใต้กลุ่มที่สอดคล้องกับ Create team
from office365 . graph_client import GraphClient
client = GraphClient ( acquire_token_func )
new_team = client . groups [ "{group-id}" ]. add_team (). execute_query_retry ()
ตัวอย่างเพิ่มเติม:
อ้างถึงส่วนตัวอย่างสำหรับสถานการณ์อื่น ๆ
ห้องสมุดรองรับ OneNote API ในแง่ของการโทรไปยังสมุดบันทึก OneNote ของผู้ใช้ส่วนและหน้าในบัญชีส่วนบุคคลหรือองค์กร
ตัวอย่าง: สร้างหน้าใหม่
from office365 . graph_client import GraphClient
client = GraphClient ( acquire_token_func )
files = {}
with open ( "./MyPage.html" , 'rb' ) as f ,
open ( "./MyImage.png" , 'rb' ) as img_f ,
open ( "./MyDoc.pdf" , 'rb' ) as pdf_f :
files [ "imageBlock1" ] = img_f
files [ "fileBlock1" ] = pdf_f
page = client . me . onenote . pages . add ( presentation_file = f , attachment_files = files ). execute_query ()
ตัวอย่างแสดงวิธีการสร้างงานวางแผนใหม่ซึ่งสอดคล้องกันเพื่อ Create plannerTask
:
from office365 . graph_client import GraphClient
client = GraphClient ( acquire_token_func )
task = client . planner . tasks . add ( title = "New task" , planId = "--plan id goes here--" ). execute_query ()
ไลบรารีต่อไปนี้จะถูกติดตั้งเมื่อคุณติดตั้งไลบรารีไคลเอ็นต์:
Python Ide Pycharm
ที่ทรงพลังจาก Jetbrains