Hướng dẫn python-o365 - python-o365

Hướng dẫn python-o365 - python-o365

O365 - API Microsoft Graph và Office 365 được thực hiện dễ dàng

Tài liệu sử dụng chi tiết vẫn đang được tiến hành

Dự án này nhằm mục đích làm cho việc tương tác với Microsoft Graph và Office 365 dễ thực hiện theo cách Pythonic. Truy cập vào email, lịch, danh bạ, OneDrive, v.v ... rất dễ thực hiện theo cách cảm thấy dễ dàng và thẳng thắn về phía trước và cảm thấy đúng với lập trình viên Python dày dạn.

Dự án hiện đang được phát triển và duy trì bởi Janscas.

Các nhà phát triển cốt lõi

  • Janscas
  • Toben Archer
  • Geethanadh

Chúng tôi luôn sẵn sàng cho các yêu cầu kéo mới!

Xây dựng lại tài liệu HTML

  • Cài đặt thư viện Python

    @route('/stepone')
    def auth_step_one():
    
        callback = 'my absolute url to auth_step_two_callback'
        account = Account(credentials)
        url, state = account.con.get_authorization_url(requested_scopes=my_scopes,
                                                       redirect_uri=callback)
        
        # the state must be saved somewhere as it will be needed later
        my_db.store_state(state) # example...
        
        return redirect(url)
    
    @route('/steptwo')
    def auth_step_two_callback():
        account = Account(credentials)
        
        # retreive the state saved in auth_step_one
        my_saved_state = my_db.get_state()  # example...
        
        # rebuild the redirect_uri used in auth_step_one
        callback = 'my absolute url to auth_step_two_callback'
        
        result = account.con.request_token(request.url, 
                                           state=my_saved_state,
                                           redirect_uri=callback)
        # if result is True, then authentication was succesful 
        #  and the auth token is stored in the token backend
        if result:
            return render_template('auth_complete.html')
        # else ....
    0

    @route('/stepone')
    def auth_step_one():
    
        callback = 'my absolute url to auth_step_two_callback'
        account = Account(credentials)
        url, state = account.con.get_authorization_url(requested_scopes=my_scopes,
                                                       redirect_uri=callback)
        
        # the state must be saved somewhere as it will be needed later
        my_db.store_state(state) # example...
        
        return redirect(url)
    
    @route('/steptwo')
    def auth_step_two_callback():
        account = Account(credentials)
        
        # retreive the state saved in auth_step_one
        my_saved_state = my_db.get_state()  # example...
        
        # rebuild the redirect_uri used in auth_step_one
        callback = 'my absolute url to auth_step_two_callback'
        
        result = account.con.request_token(request.url, 
                                           state=my_saved_state,
                                           redirect_uri=callback)
        # if result is True, then authentication was succesful 
        #  and the auth token is stored in the token backend
        if result:
            return render_template('auth_complete.html')
        # else ....
    1

  • Chạy tập lệnh shell

    @route('/stepone')
    def auth_step_one():
    
        callback = 'my absolute url to auth_step_two_callback'
        account = Account(credentials)
        url, state = account.con.get_authorization_url(requested_scopes=my_scopes,
                                                       redirect_uri=callback)
        
        # the state must be saved somewhere as it will be needed later
        my_db.store_state(state) # example...
        
        return redirect(url)
    
    @route('/steptwo')
    def auth_step_two_callback():
        account = Account(credentials)
        
        # retreive the state saved in auth_step_one
        my_saved_state = my_db.get_state()  # example...
        
        # rebuild the redirect_uri used in auth_step_one
        callback = 'my absolute url to auth_step_two_callback'
        
        result = account.con.request_token(request.url, 
                                           state=my_saved_state,
                                           redirect_uri=callback)
        # if result is True, then authentication was succesful 
        #  and the auth token is stored in the token backend
        if result:
            return render_template('auth_complete.html')
        # else ....
    2 hoặc sao chép lệnh từ tệp khi sử dụng trên Windows

Ví dụ nhanh về việc gửi tin nhắn:

from O365 import Account

credentials = ('client_id', 'client_secret')

account = Account(credentials)
m = account.new_message()
m.to.add('')
m.subject = 'Testing!'
m.body = "George Best quote: I've stopped drinking, but only while I'm asleep."
m.send()

Tại sao chọn O365?

  • Gần như hỗ trợ đầy đủ cho API MSGRAPH và Office 365 REST.
  • Lớp trừu tượng tốt giữa mỗi API. Thay đổi API (biểu đồ vs office365) và đừng lo lắng về việc triển khai nội bộ API.
  • Hỗ trợ OAuth đầy đủ với việc xử lý tự động các mã thông báo làm mới.
  • Xử lý tự động giữa các bộ dữ liệu cục bộ và các máy chủ máy chủ. Làm việc với DateTime địa phương của bạn và để thư viện này thực hiện phần còn lại.
  • Thay đổi giữa các tài nguyên khác nhau một cách dễ dàng: Truy cập hộp thư được chia sẻ, tài nguyên người dùng khác, tài nguyên SharePoint, v.v.
  • Hỗ trợ phân trang thông qua một trình lặp tùy chỉnh tự động xử lý các yêu cầu trong tương lai. Yêu cầu các mặt hàng vô hạn!
  • Trình trợ giúp truy vấn để giúp bạn xây dựng các truy vấn ODATA tùy chỉnh (bộ lọc, đặt hàng, chọn và tìm kiếm).
  • Apicomponents mô -đun có thể được tạo và xây dựng để đạt được chức năng hơn nữa.

Dự án này cũng là một nguồn tài nguyên học tập cho chúng tôi. Đây là danh sách các thành ngữ Python không phổ biến được sử dụng trong dự án này:

  • Kỹ thuật giải nén mới:
    @route('/stepone')
    def auth_step_one():
    
        callback = 'my absolute url to auth_step_two_callback'
        account = Account(credentials)
        url, state = account.con.get_authorization_url(requested_scopes=my_scopes,
                                                       redirect_uri=callback)
        
        # the state must be saved somewhere as it will be needed later
        my_db.store_state(state) # example...
        
        return redirect(url)
    
    @route('/steptwo')
    def auth_step_two_callback():
        account = Account(credentials)
        
        # retreive the state saved in auth_step_one
        my_saved_state = my_db.get_state()  # example...
        
        # rebuild the redirect_uri used in auth_step_one
        callback = 'my absolute url to auth_step_two_callback'
        
        result = account.con.request_token(request.url, 
                                           state=my_saved_state,
                                           redirect_uri=callback)
        # if result is True, then authentication was succesful 
        #  and the auth token is stored in the token backend
        if result:
            return render_template('auth_complete.html')
        # else ....
    3
  • Enums:
    @route('/stepone')
    def auth_step_one():
    
        callback = 'my absolute url to auth_step_two_callback'
        account = Account(credentials)
        url, state = account.con.get_authorization_url(requested_scopes=my_scopes,
                                                       redirect_uri=callback)
        
        # the state must be saved somewhere as it will be needed later
        my_db.store_state(state) # example...
        
        return redirect(url)
    
    @route('/steptwo')
    def auth_step_two_callback():
        account = Account(credentials)
        
        # retreive the state saved in auth_step_one
        my_saved_state = my_db.get_state()  # example...
        
        # rebuild the redirect_uri used in auth_step_one
        callback = 'my absolute url to auth_step_two_callback'
        
        result = account.con.request_token(request.url, 
                                           state=my_saved_state,
                                           redirect_uri=callback)
        # if result is True, then authentication was succesful 
        #  and the auth token is stored in the token backend
        if result:
            return render_template('auth_complete.html')
        # else ....
    4
  • Mô hình nhà máy
  • Tổ chức gói
  • Chuyển đổi timezone và thời gian dữ liệu nhận thức múi giờ
  • Vv (xem mã!)

Những gì tiếp theo là một loại wiki ...

Mục lục

  • Cài đặt
  • Cách sử dụng
  • Xác thực
  • Giao thức
  • Lớp tài khoản và mô -đun
  • Hộp thư
  • Sổ địa chỉ
  • Thư mục và người dùng
  • Lịch
  • Nhiệm vụ
  • Một ổ đĩa
  • Excel
  • Điểm chia sẻ
  • Người lập kế hoạch
  • Danh mục triển vọng
  • Sử dụng

Cài đặt

Cách sử dụng

Xác thực

Giao thức

  • Lớp tài khoản và mô -đun
  • requests-oauthlib
  • beatifulsoup4
  • Hộp thư
  • python-dateutil
  • Sổ địa chỉ
  • Thư mục và người dùng

Cách sử dụng

Xác thực

Giao thức

Lớp tài khoản và mô -đun

Hộp thư

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...

Xác thực

Giao thức

Lớp tài khoản và mô -đun

  • Hộp thưauthorization code grant flow. This is the default authentication method used by this library.

  • Sổ địa chỉ

  • Thư mục và người dùngclient credentials grant flow.

    LịchMicrosoft Personal accounts.

Nhiệm vụ

Một ổ đĩaExcelĐiểm chia sẻNgười lập kế hoạch
Danh mục triển vọng Sử dụngSử dụngSử dụng
O365 có sẵn trên pypi.org. Chỉ cần chạy
@route('/stepone')
def auth_step_one():

    callback = 'my absolute url to auth_step_two_callback'
    account = Account(credentials)
    url, state = account.con.get_authorization_url(requested_scopes=my_scopes,
                                                   redirect_uri=callback)
    
    # the state must be saved somewhere as it will be needed later
    my_db.store_state(state) # example...
    
    return redirect(url)

@route('/steptwo')
def auth_step_two_callback():
    account = Account(credentials)
    
    # retreive the state saved in auth_step_one
    my_saved_state = my_db.get_state()  # example...
    
    # rebuild the redirect_uri used in auth_step_one
    callback = 'my absolute url to auth_step_two_callback'
    
    result = account.con.request_token(request.url, 
                                       state=my_saved_state,
                                       redirect_uri=callback)
    # if result is True, then authentication was succesful 
    #  and the auth token is stored in the token backend
    if result:
        return render_template('auth_complete.html')
    # else ....
5 để cài đặt nó.
Yêu cầu:> = Python 3.4Yêu cầu:> = Python 3.4Phụ thuộc dự án được cài đặt bởi PIP:
yêu cầu ChuỗiChuỗiTzlocal
pytz Bước đầu tiên để có thể làm việc với thư viện này là đăng ký một ứng dụng và truy xuất mã thông báo auth. Xem xác thực.Rất khuyến khích thêm quyền "Offline_access" và yêu cầu phạm vi này khi xác thực. Nếu không, thư viện sẽ chỉ có quyền truy cập vào tài nguyên người dùng trong 1 giờ. Xem quyền và phạm vi.Với mã thông báo truy cập được truy xuất và lưu trữ, bạn sẽ có thể thực hiện các cuộc gọi API đến dịch vụ.
Xác thực Giao thứcGiao thứcLớp tài khoản và mô -đun
Hộp thư Sử dụngSử dụngO365 có sẵn trên pypi.org. Chỉ cần chạy
@route('/stepone')
def auth_step_one():

    callback = 'my absolute url to auth_step_two_callback'
    account = Account(credentials)
    url, state = account.con.get_authorization_url(requested_scopes=my_scopes,
                                                   redirect_uri=callback)
    
    # the state must be saved somewhere as it will be needed later
    my_db.store_state(state) # example...
    
    return redirect(url)

@route('/steptwo')
def auth_step_two_callback():
    account = Account(credentials)
    
    # retreive the state saved in auth_step_one
    my_saved_state = my_db.get_state()  # example...
    
    # rebuild the redirect_uri used in auth_step_one
    callback = 'my absolute url to auth_step_two_callback'
    
    result = account.con.request_token(request.url, 
                                       state=my_saved_state,
                                       redirect_uri=callback)
    # if result is True, then authentication was succesful 
    #  and the auth token is stored in the token backend
    if result:
        return render_template('auth_complete.html')
    # else ....
5 để cài đặt nó.
Yêu cầu:> = Python 3.4 Phụ thuộc dự án được cài đặt bởi PIP:Phụ thuộc dự án được cài đặt bởi PIP:yêu cầu
Chuỗi TzlocalpytzVô hạn
Tài nguyên Truy cập tài nguyên người dùng và bất kỳ tài nguyên được chia sẻ nàoTruy cập tài nguyên người dùng và bất kỳ tài nguyên được chia sẻ nàoTất cả người dùng Azure AD mà ứng dụng có quyền truy cập
Loại tài khoản Microsoft Không tí nàoKhông tí nàoKhông được phép cho các tài khoản cá nhân
ID người thuê cần thiết Mặc định là "chung"Mặc định là "chung"Bắt buộc (không thể là "phổ biến")

*O365 sẽ tự động làm mới mã thông báo cho bạn trên phương thức xác thực. Mã thông báo làm mới kéo dài 90 ngày nhưng nó được làm mới trên mỗi kết nối để bạn kết nối trong vòng 90 ngày, bạn có thể có quyền truy cập không giới hạn.

Lớp

@route('/stepone')
def auth_step_one():

    callback = 'my absolute url to auth_step_two_callback'
    account = Account(credentials)
    url, state = account.con.get_authorization_url(requested_scopes=my_scopes,
                                                   redirect_uri=callback)
    
    # the state must be saved somewhere as it will be needed later
    my_db.store_state(state) # example...
    
    return redirect(url)

@route('/steptwo')
def auth_step_two_callback():
    account = Account(credentials)
    
    # retreive the state saved in auth_step_one
    my_saved_state = my_db.get_state()  # example...
    
    # rebuild the redirect_uri used in auth_step_one
    callback = 'my absolute url to auth_step_two_callback'
    
    result = account.con.request_token(request.url, 
                                       state=my_saved_state,
                                       redirect_uri=callback)
    # if result is True, then authentication was succesful 
    #  and the auth token is stored in the token backend
    if result:
        return render_template('auth_complete.html')
    # else ....
6 xử lý xác thực.

Xác thực OAuth

Phần này được giải thích bằng giao thức Microsoft Graph, gần như giống nhau áp dụng cho API Office 365 REST.

Các bước xác thực
  1. Để cho phép xác thực, trước tiên bạn cần đăng ký ứng dụng của mình khi đăng ký ứng dụng Azure.

    1. Đăng nhập tại Azure Portal (Đăng ký ứng dụng)

    2. Tạo một ứng dụng. Đặt tên.

    3. Trong các loại tài khoản được hỗ trợ, hãy chọn "Tài khoản trong bất kỳ thư mục tổ chức và tài khoản cá nhân nào của Microsoft (ví dụ: Skype, Xbox, Outlook.com)", nếu bạn đang sử dụng tài khoản cá nhân.

    4. Đặt URI chuyển hướng (Web) thành:

      @route('/stepone')
      def auth_step_one():
      
          callback = 'my absolute url to auth_step_two_callback'
          account = Account(credentials)
          url, state = account.con.get_authorization_url(requested_scopes=my_scopes,
                                                         redirect_uri=callback)
          
          # the state must be saved somewhere as it will be needed later
          my_db.store_state(state) # example...
          
          return redirect(url)
      
      @route('/steptwo')
      def auth_step_two_callback():
          account = Account(credentials)
          
          # retreive the state saved in auth_step_one
          my_saved_state = my_db.get_state()  # example...
          
          # rebuild the redirect_uri used in auth_step_one
          callback = 'my absolute url to auth_step_two_callback'
          
          result = account.con.request_token(request.url, 
                                             state=my_saved_state,
                                             redirect_uri=callback)
          # if result is True, then authentication was succesful 
          #  and the auth token is stored in the token backend
          if result:
              return render_template('auth_complete.html')
          # else ....
      7 và nhấp vào Đăng ký. Điều này cần phải được chèn vào hộp văn bản "chuyển hướng URI" vì chỉ đơn giản là kiểm tra hộp kiểm bên cạnh liên kết này dường như không rõ ràng. Đây là URI chuyển hướng mặc định được sử dụng bởi thư viện này, nhưng bạn có thể sử dụng bất kỳ UR nào khác nếu bạn muốn.

    5. Viết id Ứng dụng (máy khách) ID. Bạn sẽ cần giá trị này.

    6. Trong "Chứng chỉ & Bí mật", tạo bí mật khách hàng mới. Đặt hết hạn tốt nhất là không bao giờ. Viết ra giá trị của bí mật khách hàng được tạo ra ngay bây giờ. Nó sẽ được ẩn sau này.

    7. Theo quyền API:

      • Khi xác thực "thay mặt cho người dùng":
        1. Thêm các quyền được ủy quyền cho Microsoft Graph bạn muốn (xem phạm vi).delegated permissions for Microsoft Graph you want (see scopes).
        2. Rất khuyến khích thêm quyền "Offline_access". Nếu không phải người dùng, bạn sẽ phải xác thực lại mỗi giờ.
      • Khi xác thực "với danh tính của riêng bạn":
        1. Thêm quyền ứng dụng cho Microsoft Braph bạn muốn.application permissions for Microsoft Graph you want.
        2. Nhấp vào nút chấp thuận quản trị viên cấp (nếu bạn có quyền quản trị viên) hoặc đợi cho đến khi quản trị viên đồng ý với ứng dụng của bạn.

      Ví dụ, để đọc và gửi email sử dụng:

      1. Mail.ReadWrite
      2. Mail.Send
      3. User.Read
  2. Sau đó, bạn cần đăng nhập lần đầu tiên để có được mã thông báo truy cập sẽ cấp quyền truy cập vào tài nguyên người dùng.

    Để xác thực (đăng nhập), bạn có thể sử dụng các giao diện xác thực khác nhau. Trên các ví dụ sau, chúng tôi sẽ sử dụng giao diện dựa trên bảng điều khiển nhưng bạn có thể sử dụng bất kỳ giao diện nào.

    • Khi xác thực thay mặt cho người dùng:

      Quan trọng: Trong trường hợp bạn không thể bảo mật bí mật của máy khách, bạn có thể sử dụng loại luồng chính xác 'công khai' chỉ yêu cầu ID máy khách. In case you can't secure the client secret you can use the auth flow type 'public' which only requires the client id.

      1. Khởi tạo một đối tượng

        @route('/stepone')
        def auth_step_one():
        
            callback = 'my absolute url to auth_step_two_callback'
            account = Account(credentials)
            url, state = account.con.get_authorization_url(requested_scopes=my_scopes,
                                                           redirect_uri=callback)
            
            # the state must be saved somewhere as it will be needed later
            my_db.store_state(state) # example...
            
            return redirect(url)
        
        @route('/steptwo')
        def auth_step_two_callback():
            account = Account(credentials)
            
            # retreive the state saved in auth_step_one
            my_saved_state = my_db.get_state()  # example...
            
            # rebuild the redirect_uri used in auth_step_one
            callback = 'my absolute url to auth_step_two_callback'
            
            result = account.con.request_token(request.url, 
                                               state=my_saved_state,
                                               redirect_uri=callback)
            # if result is True, then authentication was succesful 
            #  and the auth token is stored in the token backend
            if result:
                return render_template('auth_complete.html')
            # else ....
        8 với thông tin đăng nhập (ID máy khách và bí mật của máy khách).

      2. Gọi

        @route('/stepone')
        def auth_step_one():
        
            callback = 'my absolute url to auth_step_two_callback'
            account = Account(credentials)
            url, state = account.con.get_authorization_url(requested_scopes=my_scopes,
                                                           redirect_uri=callback)
            
            # the state must be saved somewhere as it will be needed later
            my_db.store_state(state) # example...
            
            return redirect(url)
        
        @route('/steptwo')
        def auth_step_two_callback():
            account = Account(credentials)
            
            # retreive the state saved in auth_step_one
            my_saved_state = my_db.get_state()  # example...
            
            # rebuild the redirect_uri used in auth_step_one
            callback = 'my absolute url to auth_step_two_callback'
            
            result = account.con.request_token(request.url, 
                                               state=my_saved_state,
                                               redirect_uri=callback)
            # if result is True, then authentication was succesful 
            #  and the auth token is stored in the token backend
            if result:
                return render_template('auth_complete.html')
            # else ....
        9 và vượt qua phạm vi bạn muốn (những điều bạn đã thêm trước đó trên cổng đăng ký ứng dụng).

        Lưu ý: Khi sử dụng xác thực "thay mặt cho người dùng", bạn có thể chuyển phạm vi cho

        @route('/stepone')
        def auth_step_one():
        
            callback = 'my absolute url to auth_step_two_callback'
            account = Account(credentials)
            url, state = account.con.get_authorization_url(requested_scopes=my_scopes,
                                                           redirect_uri=callback)
            
            # the state must be saved somewhere as it will be needed later
            my_db.store_state(state) # example...
            
            return redirect(url)
        
        @route('/steptwo')
        def auth_step_two_callback():
            account = Account(credentials)
            
            # retreive the state saved in auth_step_one
            my_saved_state = my_db.get_state()  # example...
            
            # rebuild the redirect_uri used in auth_step_one
            callback = 'my absolute url to auth_step_two_callback'
            
            result = account.con.request_token(request.url, 
                                               state=my_saved_state,
                                               redirect_uri=callback)
            # if result is True, then authentication was succesful 
            #  and the auth token is stored in the token backend
            if result:
                return render_template('auth_complete.html')
            # else ....
        8 init hoặc phương thức xác thực. Dù bằng cách nào là chính xác.

        Bạn có thể vượt qua "phạm vi giao thức" (như: "https://graph.microsoft.com/calendars.readwrite") cho phương thức hoặc sử dụng "Trợ lý phạm vi" như ("message_all"). Nếu bạn vượt qua phạm vi giao thức, thì ví dụ

        from O365 import Account
        
        credentials = ('client_id', 'client_secret')
        
        scopes = ['https://graph.microsoft.com/Mail.ReadWrite', 'https://graph.microsoft.com/Mail.Send']
        
        account = Account(credentials, scopes=scopes)
        account.authenticate()
        
        # The latter is exactly the same as passing scopes to the authenticate method like so:
        # account = Account(credentials)
        # account.authenticate(scopes=scopes)
        1 phải được khởi tạo với cùng một giao thức được sử dụng bởi phạm vi. Bằng cách sử dụng các trình trợ giúp phạm vi, bạn có thể trừu tượng hóa giao thức từ phạm vi và để thư viện này hoạt động cho bạn. Cuối cùng, bạn có thể trộn và kết hợp "phạm vi giao thức" với "Trợ giúp phạm vi". Đi đến phần Procotol để biết thêm về họ.
        Finally, you can mix and match "protocol scopes" with "scope helpers". Go to the procotol section to know more about them.

        Ví dụ: theo các quyền trước đó được thêm vào):

        from O365 import Account
        credentials = ('my_client_id', 'my_client_secret')
        
        # the default protocol will be Microsoft Graph
        # the default authentication method will be "on behalf of a user"
        
        account = Account(credentials)
        if account.authenticate(scopes=['basic', 'message_all']):
           print('Authenticated!')
        
        # 'basic' adds: 'offline_access' and 'https://graph.microsoft.com/User.Read'
        # 'message_all' adds: 'https://graph.microsoft.com/Mail.ReadWrite' and 'https://graph.microsoft.com/Mail.Send'

        Khi sử dụng phương thức xác thực "thay mặt cho người dùng", cuộc gọi phương thức này sẽ in URL mà người dùng phải truy cập để đồng ý với ứng dụng theo các quyền bắt buộc.

        Sau đó, người dùng phải truy cập URL này và đồng ý với ứng dụng. Khi đồng ý được đưa ra, trang sẽ chuyển hướng đến: "https://login.microsoftonline.com/common/oauth2/nativeclient" theo mặc định (bạn có thể thay đổi điều này) bằng một tham số truy vấn URL có tên là 'mã'.

        Sau đó, người dùng phải sao chép URL trang kết quả và dán lại trên bảng điều khiển. Phương pháp sau đó sẽ trả về đúng nếu nỗ lực đăng nhập là thành công.

    • Khi xác thực bằng danh tính của riêng bạn:

      1. Khởi tạo một đối tượng

        @route('/stepone')
        def auth_step_one():
        
            callback = 'my absolute url to auth_step_two_callback'
            account = Account(credentials)
            url, state = account.con.get_authorization_url(requested_scopes=my_scopes,
                                                           redirect_uri=callback)
            
            # the state must be saved somewhere as it will be needed later
            my_db.store_state(state) # example...
            
            return redirect(url)
        
        @route('/steptwo')
        def auth_step_two_callback():
            account = Account(credentials)
            
            # retreive the state saved in auth_step_one
            my_saved_state = my_db.get_state()  # example...
            
            # rebuild the redirect_uri used in auth_step_one
            callback = 'my absolute url to auth_step_two_callback'
            
            result = account.con.request_token(request.url, 
                                               state=my_saved_state,
                                               redirect_uri=callback)
            # if result is True, then authentication was succesful 
            #  and the auth token is stored in the token backend
            if result:
                return render_template('auth_complete.html')
            # else ....
        8 với thông tin đăng nhập (ID máy khách và bí mật của máy khách), chỉ định tham số
        from O365 import Account
        
        credentials = ('client_id', 'client_secret')
        
        scopes = ['https://graph.microsoft.com/Mail.ReadWrite', 'https://graph.microsoft.com/Mail.Send']
        
        account = Account(credentials, scopes=scopes)
        account.authenticate()
        
        # The latter is exactly the same as passing scopes to the authenticate method like so:
        # account = Account(credentials)
        # account.authenticate(scopes=scopes)
        3 thành "thông tin đăng nhập". Bạn cũng cần cung cấp một 'tenant_id'. Bạn không cần chỉ định bất kỳ phạm vi nào.

      2. Gọi

        @route('/stepone')
        def auth_step_one():
        
            callback = 'my absolute url to auth_step_two_callback'
            account = Account(credentials)
            url, state = account.con.get_authorization_url(requested_scopes=my_scopes,
                                                           redirect_uri=callback)
            
            # the state must be saved somewhere as it will be needed later
            my_db.store_state(state) # example...
            
            return redirect(url)
        
        @route('/steptwo')
        def auth_step_two_callback():
            account = Account(credentials)
            
            # retreive the state saved in auth_step_one
            my_saved_state = my_db.get_state()  # example...
            
            # rebuild the redirect_uri used in auth_step_one
            callback = 'my absolute url to auth_step_two_callback'
            
            result = account.con.request_token(request.url, 
                                               state=my_saved_state,
                                               redirect_uri=callback)
            # if result is True, then authentication was succesful 
            #  and the auth token is stored in the token backend
            if result:
                return render_template('auth_complete.html')
            # else ....
        9. Cuộc gọi này sẽ yêu cầu một mã thông báo cho bạn và lưu trữ nó trong phần phụ trợ. Không cần tương tác của người dùng. Phương thức sẽ lưu trữ mã thông báo trong phần phụ trợ và trả về đúng nếu xác thực thành công.

        Ví dụ:

        from O365 import Account
        
        credentials = ('my_client_id', 'my_client_secret')
        
        # the default protocol will be Microsoft Graph
        
        account = Account(credentials, auth_flow_type='credentials', tenant_id='my-tenant-id')
        if account.authenticate():
           print('Authenticated!')

  3. Tại thời điểm này, bạn sẽ có một mã thông báo truy cập được lưu trữ sẽ cung cấp thông tin đăng nhập hợp lệ khi sử dụng API.

    Mã thông báo truy cập chỉ kéo dài 60 phút, nhưng ứng dụng sẽ tự động yêu cầu mã thông báo truy cập mới.60 minutes, but the app try will automatically request new access tokens.

    Khi sử dụng phương thức xác thực "thay mặt cho người dùng", điều này được thực hiện thông qua các mã thông báo làm mới (nếu và chỉ khi bạn thêm quyền "offline_access"), nhưng lưu ý rằng một mã thông báo làm mới chỉ kéo dài trong 90 ngày. Vì vậy, bạn phải sử dụng nó trước hoặc bạn sẽ cần yêu cầu một mã thông báo truy cập mới (không cần sự đồng ý mới mà người dùng cần, chỉ là một thông tin đăng nhập). Nếu ứng dụng của bạn cần làm việc trong hơn 90 ngày mà không cần người dùng tương tác và không tương tác với API, thì bạn phải thực hiện cuộc gọi định kỳ đến

    from O365 import Account
    
    credentials = ('client_id', 'client_secret')
    
    scopes = ['https://graph.microsoft.com/Mail.ReadWrite', 'https://graph.microsoft.com/Mail.Send']
    
    account = Account(credentials, scopes=scopes)
    account.authenticate()
    
    # The latter is exactly the same as passing scopes to the authenticate method like so:
    # account = Account(credentials)
    # account.authenticate(scopes=scopes)
    5 trước khi 90 ngày trôi qua.

    Chăm sóc: Mã thông báo truy cập (và làm mới) phải được bảo vệ khỏi người dùng trái phép.

    Theo phương thức xác thực "thay mặt cho người dùng", nếu bạn thay đổi phạm vi được yêu cầu, thì mã thông báo hiện tại sẽ không hoạt động và bạn sẽ cần người dùng đồng ý trở lại trên ứng dụng để có quyền truy cập vào phạm vi mới được yêu cầu.

Giao diện xác thực khác nhau

Để thực hiện xác thực, về cơ bản, bạn có thể sử dụng các phương pháp khác nhau. Điều sau đây áp dụng cho phương thức xác thực "thay mặt cho người dùng" vì đây là luồng xác thực 2 bước. Đối với phương thức xác thực "Với danh tính của riêng bạn", bạn chỉ có thể sử dụng

@route('/stepone')
def auth_step_one():

    callback = 'my absolute url to auth_step_two_callback'
    account = Account(credentials)
    url, state = account.con.get_authorization_url(requested_scopes=my_scopes,
                                                   redirect_uri=callback)
    
    # the state must be saved somewhere as it will be needed later
    my_db.store_state(state) # example...
    
    return redirect(url)

@route('/steptwo')
def auth_step_two_callback():
    account = Account(credentials)
    
    # retreive the state saved in auth_step_one
    my_saved_state = my_db.get_state()  # example...
    
    # rebuild the redirect_uri used in auth_step_one
    callback = 'my absolute url to auth_step_two_callback'
    
    result = account.con.request_token(request.url, 
                                       state=my_saved_state,
                                       redirect_uri=callback)
    # if result is True, then authentication was succesful 
    #  and the auth token is stored in the token backend
    if result:
        return render_template('auth_complete.html')
    # else ....
9 vì nó sẽ không yêu cầu đầu vào bảng điều khiển.

  1. Giao diện xác thực dựa trên bảng điều khiển:

    Bạn có thể xác thực bằng cách sử dụng bảng điều khiển. Cách tốt nhất để đạt được điều này là sử dụng phương pháp

    from O365 import Account
    
    credentials = ('client_id', 'client_secret')
    
    scopes = ['https://graph.microsoft.com/Mail.ReadWrite', 'https://graph.microsoft.com/Mail.Send']
    
    account = Account(credentials, scopes=scopes)
    account.authenticate()
    
    # The latter is exactly the same as passing scopes to the authenticate method like so:
    # account = Account(credentials)
    # account.authenticate(scopes=scopes)
    7 của lớp
    @route('/stepone')
    def auth_step_one():
    
        callback = 'my absolute url to auth_step_two_callback'
        account = Account(credentials)
        url, state = account.con.get_authorization_url(requested_scopes=my_scopes,
                                                       redirect_uri=callback)
        
        # the state must be saved somewhere as it will be needed later
        my_db.store_state(state) # example...
        
        return redirect(url)
    
    @route('/steptwo')
    def auth_step_two_callback():
        account = Account(credentials)
        
        # retreive the state saved in auth_step_one
        my_saved_state = my_db.get_state()  # example...
        
        # rebuild the redirect_uri used in auth_step_one
        callback = 'my absolute url to auth_step_two_callback'
        
        result = account.con.request_token(request.url, 
                                           state=my_saved_state,
                                           redirect_uri=callback)
        # if result is True, then authentication was succesful 
        #  and the auth token is stored in the token backend
        if result:
            return render_template('auth_complete.html')
        # else ....
    8.

    account = Account(credentials)
    account.authenticate(scopes=['basic', 'message_all'])

    Phương pháp

    from O365 import Account
    
    credentials = ('client_id', 'client_secret')
    
    scopes = ['https://graph.microsoft.com/Mail.ReadWrite', 'https://graph.microsoft.com/Mail.Send']
    
    account = Account(credentials, scopes=scopes)
    account.authenticate()
    
    # The latter is exactly the same as passing scopes to the authenticate method like so:
    # account = Account(credentials)
    # account.authenticate(scopes=scopes)
    7 sẽ in vào bảng điều khiển một URL mà bạn sẽ phải truy cập để đạt được xác thực. Sau đó, sau khi truy cập liên kết và xác thực, bạn sẽ phải dán lại URL kết quả vào bảng điều khiển. Phương pháp sẽ trả về
    protocol_graph = MSGraphProtocol()
    
    scopes_graph = protocol.get_scopes_for('message all')
    # scopes here are: ['https://graph.microsoft.com/Mail.ReadWrite', 'https://graph.microsoft.com/Mail.Send']
    
    account = Account(credentials, scopes=scopes_graph)
    0 và in một tin nhắn nếu nó thành công.

    Mẹo: Khi sử dụng MacOS, bảng điều khiển được giới hạn ở 1024 ký tự. Nếu URL của bạn có nhiều phạm vi, nó có thể vượt quá giới hạn này. Để giải quyết điều này. Chỉ cần

    protocol_graph = MSGraphProtocol()
    
    scopes_graph = protocol.get_scopes_for('message all')
    # scopes here are: ['https://graph.microsoft.com/Mail.ReadWrite', 'https://graph.microsoft.com/Mail.Send']
    
    account = Account(credentials, scopes=scopes_graph)
    1 một phần trên cùng của kịch bản của bạn. When using MacOs the console is limited to 1024 characters. If your url has multiple scopes it can exceed this limit. To solve this. Just
    protocol_graph = MSGraphProtocol()
    
    scopes_graph = protocol.get_scopes_for('message all')
    # scopes here are: ['https://graph.microsoft.com/Mail.ReadWrite', 'https://graph.microsoft.com/Mail.Send']
    
    account = Account(credentials, scopes=scopes_graph)
    1 a the top of your script.

  2. Giao diện xác thực dựa trên ứng dụng web:

    Bạn có thể xác thực người dùng của mình trong môi trường web bằng cách làm theo các bước này:

    1. Đầu tiên đảm bảo bạn đang sử dụng TokenBackend sắp xếp để lưu trữ mã thông báo auth (xem lưu trữ mã thông báo bên dưới).
    2. Từ một người xử lý chuyển hướng người dùng đến URL đăng nhập Microsoft. Cung cấp một cuộc gọi lại. Lưu trữ trạng thái.
    3. Từ trình xử lý gọi lại hoàn thành xác thực với trạng thái và dữ liệu khác.

    Ví dụ sau đây được thực hiện bằng bình.

    @route('/stepone')
    def auth_step_one():
    
        callback = 'my absolute url to auth_step_two_callback'
        account = Account(credentials)
        url, state = account.con.get_authorization_url(requested_scopes=my_scopes,
                                                       redirect_uri=callback)
        
        # the state must be saved somewhere as it will be needed later
        my_db.store_state(state) # example...
        
        return redirect(url)
    
    @route('/steptwo')
    def auth_step_two_callback():
        account = Account(credentials)
        
        # retreive the state saved in auth_step_one
        my_saved_state = my_db.get_state()  # example...
        
        # rebuild the redirect_uri used in auth_step_one
        callback = 'my absolute url to auth_step_two_callback'
        
        result = account.con.request_token(request.url, 
                                           state=my_saved_state,
                                           redirect_uri=callback)
        # if result is True, then authentication was succesful 
        #  and the auth token is stored in the token backend
        if result:
            return render_template('auth_complete.html')
        # else ....

  3. Các giao diện xác thực khác:

    Cuối cùng, bạn có thể định cấu hình bất kỳ luồng nào khác bằng cách sử dụng

    protocol_graph = MSGraphProtocol()
    
    scopes_graph = protocol.get_scopes_for('message all')
    # scopes here are: ['https://graph.microsoft.com/Mail.ReadWrite', 'https://graph.microsoft.com/Mail.Send']
    
    account = Account(credentials, scopes=scopes_graph)
    2 và
    protocol_graph = MSGraphProtocol()
    
    scopes_graph = protocol.get_scopes_for('message all')
    # scopes here are: ['https://graph.microsoft.com/Mail.ReadWrite', 'https://graph.microsoft.com/Mail.Send']
    
    account = Account(credentials, scopes=scopes_graph)
    3 như bạn muốn.

Quyền và phạm vi:
Quyền

Khi sử dụng OAuth, bạn tạo một ứng dụng và cho phép một số tài nguyên được người dùng truy cập và sử dụng. Các tài nguyên này được quản lý với quyền. Chúng có thể được ủy quyền (thay mặt cho người dùng) hoặc quyền aplication. Cái trước được sử dụng khi phương thức xác thực là "thay mặt cho người dùng". Một số trong số này yêu cầu sự đồng ý của quản trị viên. Cái sau khi sử dụng phương thức xác thực "với danh tính của riêng bạn". Tất cả những điều này yêu cầu sự đồng ý của quản trị viên.

Phạm vi

Phạm vi chỉ quan trọng khi sử dụng phương thức xác thực "thay mặt cho người dùng".

Lưu ý: Bạn chỉ cần phạm vi khi đăng nhập vì chúng được lưu trữ trong mã thông báo trên phụ trợ mã thông báo.

Người dùng của thư viện này sau đó có thể yêu cầu quyền truy cập vào một hoặc nhiều tài nguyên này bằng cách cung cấp phạm vi cho nhà cung cấp OAuth.

Lưu ý: Nếu bạn sau đó thay đổi các phạm vi được yêu cầu, mã thông báo hiện tại sẽ được giao phó và bạn sẽ phải xác thực lại. Người dùng đăng nhập sẽ được yêu cầu đồng ý.

Ví dụ: Ứng dụng của bạn có thể có Lịch.Read, Mail.ReadWrite và Mail.Send quyền, nhưng ứng dụng chỉ có thể yêu cầu quyền truy cập vào Mail.ReadWrite và Mail.Send quyền. Điều này được thực hiện bằng cách cung cấp phạm vi cho phiên bản

@route('/stepone')
def auth_step_one():

    callback = 'my absolute url to auth_step_two_callback'
    account = Account(credentials)
    url, state = account.con.get_authorization_url(requested_scopes=my_scopes,
                                                   redirect_uri=callback)
    
    # the state must be saved somewhere as it will be needed later
    my_db.store_state(state) # example...
    
    return redirect(url)

@route('/steptwo')
def auth_step_two_callback():
    account = Account(credentials)
    
    # retreive the state saved in auth_step_one
    my_saved_state = my_db.get_state()  # example...
    
    # rebuild the redirect_uri used in auth_step_one
    callback = 'my absolute url to auth_step_two_callback'
    
    result = account.con.request_token(request.url, 
                                       state=my_saved_state,
                                       redirect_uri=callback)
    # if result is True, then authentication was succesful 
    #  and the auth token is stored in the token backend
    if result:
        return render_template('auth_complete.html')
    # else ....
8 hoặc phương thức
@route('/stepone')
def auth_step_one():

    callback = 'my absolute url to auth_step_two_callback'
    account = Account(credentials)
    url, state = account.con.get_authorization_url(requested_scopes=my_scopes,
                                                   redirect_uri=callback)
    
    # the state must be saved somewhere as it will be needed later
    my_db.store_state(state) # example...
    
    return redirect(url)

@route('/steptwo')
def auth_step_two_callback():
    account = Account(credentials)
    
    # retreive the state saved in auth_step_one
    my_saved_state = my_db.get_state()  # example...
    
    # rebuild the redirect_uri used in auth_step_one
    callback = 'my absolute url to auth_step_two_callback'
    
    result = account.con.request_token(request.url, 
                                       state=my_saved_state,
                                       redirect_uri=callback)
    # if result is True, then authentication was succesful 
    #  and the auth token is stored in the token backend
    if result:
        return render_template('auth_complete.html')
    # else ....
9 như vậy:

from O365 import Account

credentials = ('client_id', 'client_secret')

scopes = ['https://graph.microsoft.com/Mail.ReadWrite', 'https://graph.microsoft.com/Mail.Send']

account = Account(credentials, scopes=scopes)
account.authenticate()

# The latter is exactly the same as passing scopes to the authenticate method like so:
# account = Account(credentials)
# account.authenticate(scopes=scopes)

Thực hiện phạm vi phụ thuộc vào giao thức được sử dụng. Vì vậy, bằng cách sử dụng dữ liệu giao thức, bạn có thể tự động đặt phạm vi cần thiết. Điều này được thực hiện bằng cách sử dụng 'người trợ giúp phạm vi'. Đó là những người trợ giúp nhỏ mà nhóm phạm vi chức năng và trừu tượng mà Procotol đã sử dụng.

Người trợ giúp phạm viPhạm vi bao gồm
nền tảng'Offline_access' và 'user.read'
hộp thư'Mail.read'
Mailbox_shared'Mail.read.shared'
message_send'Mail.send'
message_send_shared'Mail.send.shared'
message_all'Mail.readwrite' và 'mail.send'
message_all_shared'Mail.readwrite.shared' và 'mail.send.shared'
sổ địa chỉ'Liên hệ.read'
Địa chỉ_book_shared'Liên hệ.read.shared'
Địa chỉ_book_all'Liên hệ.ReadWrite'
Địa chỉ_book_all_shared'Liên hệ.readwrite.shared'
lịch'Lịch.Read'
Lịch_Shared'Lịch.Read.shared'
Lịch_all'Lịch.ReadWrite'
lịch_shared_all'Lịch.ReadWrite.shared'
Nhiệm vụ'Nhiệm vụ.read'
Nhiệm vụ_all'Nhiệm vụ.ReadWrite'
Người dùng'User.readbasic.all'
một ổ đĩa'Files.read.all'
OneDrive_all'Files.ReadWrite.all'
điểm chia sẻ'Trang web.read.all'
SharePoint_DL'Site.ReadWrite.all'

Bạn có thể nhận được phạm vi giống như trước khi sử dụng các giao thức và người trợ giúp phạm vi như thế này:

protocol_graph = MSGraphProtocol()

scopes_graph = protocol.get_scopes_for('message all')
# scopes here are: ['https://graph.microsoft.com/Mail.ReadWrite', 'https://graph.microsoft.com/Mail.Send']

account = Account(credentials, scopes=scopes_graph)

protocol_office = MSOffice365Protocol()

scopes_office = protocol.get_scopes_for('message all')
# scopes here are: ['https://outlook.office.com/Mail.ReadWrite', 'https://outlook.office.com/Mail.Send']

account = Account(credentials, scopes=scopes_office)

Lưu ý: Khi vượt qua các phạm vi tại phần khởi tạo

@route('/stepone')
def auth_step_one():

    callback = 'my absolute url to auth_step_two_callback'
    account = Account(credentials)
    url, state = account.con.get_authorization_url(requested_scopes=my_scopes,
                                                   redirect_uri=callback)
    
    # the state must be saved somewhere as it will be needed later
    my_db.store_state(state) # example...
    
    return redirect(url)

@route('/steptwo')
def auth_step_two_callback():
    account = Account(credentials)
    
    # retreive the state saved in auth_step_one
    my_saved_state = my_db.get_state()  # example...
    
    # rebuild the redirect_uri used in auth_step_one
    callback = 'my absolute url to auth_step_two_callback'
    
    result = account.con.request_token(request.url, 
                                       state=my_saved_state,
                                       redirect_uri=callback)
    # if result is True, then authentication was succesful 
    #  and the auth token is stored in the token backend
    if result:
        return render_template('auth_complete.html')
    # else ....
8 hoặc trên phương pháp
@route('/stepone')
def auth_step_one():

    callback = 'my absolute url to auth_step_two_callback'
    account = Account(credentials)
    url, state = account.con.get_authorization_url(requested_scopes=my_scopes,
                                                   redirect_uri=callback)
    
    # the state must be saved somewhere as it will be needed later
    my_db.store_state(state) # example...
    
    return redirect(url)

@route('/steptwo')
def auth_step_two_callback():
    account = Account(credentials)
    
    # retreive the state saved in auth_step_one
    my_saved_state = my_db.get_state()  # example...
    
    # rebuild the redirect_uri used in auth_step_one
    callback = 'my absolute url to auth_step_two_callback'
    
    result = account.con.request_token(request.url, 
                                       state=my_saved_state,
                                       redirect_uri=callback)
    # if result is True, then authentication was succesful 
    #  and the auth token is stored in the token backend
    if result:
        return render_template('auth_complete.html')
    # else ....
9, Trợ lý phạm vi được chuyển đổi tự động thành hương vị giao thức. Đó là những nơi duy nhất mà bạn có thể sử dụng Trợ giúp Phạm vi. Bất kỳ đối tượng nào khác sử dụng phạm vi (như đối tượng
@route('/stepone')
def auth_step_one():

    callback = 'my absolute url to auth_step_two_callback'
    account = Account(credentials)
    url, state = account.con.get_authorization_url(requested_scopes=my_scopes,
                                                   redirect_uri=callback)
    
    # the state must be saved somewhere as it will be needed later
    my_db.store_state(state) # example...
    
    return redirect(url)

@route('/steptwo')
def auth_step_two_callback():
    account = Account(credentials)
    
    # retreive the state saved in auth_step_one
    my_saved_state = my_db.get_state()  # example...
    
    # rebuild the redirect_uri used in auth_step_one
    callback = 'my absolute url to auth_step_two_callback'
    
    result = account.con.request_token(request.url, 
                                       state=my_saved_state,
                                       redirect_uri=callback)
    # if result is True, then authentication was succesful 
    #  and the auth token is stored in the token backend
    if result:
        return render_template('auth_complete.html')
    # else ....
6) đều mong đợi các phạm vi đã được đặt cho giao thức.

Lưu trữ mã thông báo:

Khi xác thực, bạn sẽ truy xuất mã thông báo OAuth. Nếu bạn không muốn truy cập một lần, bạn sẽ phải lưu trữ mã thông báo ở đâu đó. O365 không đưa ra giả định nào về nơi lưu trữ mã thông báo và cố gắng trừu tượng hóa điều này từ quan điểm sử dụng thư viện.

Bạn có thể chọn nơi và cách lưu trữ mã thông báo bằng cách sử dụng phụ trợ mã thông báo thích hợp.

Chăm sóc: Mã thông báo truy cập (và làm mới) phải được bảo vệ khỏi người dùng trái phép.

Thư viện sẽ gọi (ở các giai đoạn khác nhau), các phương thức phụ trợ mã thông báo để tải và lưu mã thông báo.

Phương pháp tải mã thông báo:

  • Thuộc tính
    protocol_graph = MSGraphProtocol()
    
    scopes_graph = protocol.get_scopes_for('message all')
    # scopes here are: ['https://graph.microsoft.com/Mail.ReadWrite', 'https://graph.microsoft.com/Mail.Send']
    
    account = Account(credentials, scopes=scopes_graph)
    9 sẽ cố gắng tải mã thông báo nếu chưa được tải.
  • protocol_office = MSOffice365Protocol()
    
    scopes_office = protocol.get_scopes_for('message all')
    # scopes here are: ['https://outlook.office.com/Mail.ReadWrite', 'https://outlook.office.com/Mail.Send']
    
    account = Account(credentials, scopes=scopes_office)
    0: Phương thức này được gọi khi không có bộ phiên yêu cầu. Theo mặc định, nó sẽ không cố gắng tải mã thông báo. Đặt
    protocol_office = MSOffice365Protocol()
    
    scopes_office = protocol.get_scopes_for('message all')
    # scopes here are: ['https://outlook.office.com/Mail.ReadWrite', 'https://outlook.office.com/Mail.Send']
    
    account = Account(credentials, scopes=scopes_office)
    1 để tải nó.

Phương pháp lưu trữ mã thông báo:

  • protocol_graph = MSGraphProtocol()
    
    scopes_graph = protocol.get_scopes_for('message all')
    # scopes here are: ['https://graph.microsoft.com/Mail.ReadWrite', 'https://graph.microsoft.com/Mail.Send']
    
    account = Account(credentials, scopes=scopes_graph)
    3: Theo mặc định sẽ lưu trữ mã thông báo, nhưng bạn có thể đặt
    protocol_office = MSOffice365Protocol()
    
    scopes_office = protocol.get_scopes_for('message all')
    # scopes here are: ['https://outlook.office.com/Mail.ReadWrite', 'https://outlook.office.com/Mail.Send']
    
    account = Account(credentials, scopes=scopes_office)
    3 để tránh nó.
  • protocol_office = MSOffice365Protocol()
    
    scopes_office = protocol.get_scopes_for('message all')
    # scopes here are: ['https://outlook.office.com/Mail.ReadWrite', 'https://outlook.office.com/Mail.Send']
    
    account = Account(credentials, scopes=scopes_office)
    4: Theo mặc định sẽ lưu trữ mã thông báo. Để tránh nó thay đổi
    protocol_office = MSOffice365Protocol()
    
    scopes_office = protocol.get_scopes_for('message all')
    # scopes here are: ['https://outlook.office.com/Mail.ReadWrite', 'https://outlook.office.com/Mail.Send']
    
    account = Account(credentials, scopes=scopes_office)
    5 thành sai. Tuy nhiên, đây là một cài đặt toàn cầu (chỉ ảnh hưởng đến phương pháp
    protocol_office = MSOffice365Protocol()
    
    scopes_office = protocol.get_scopes_for('message all')
    # scopes here are: ['https://outlook.office.com/Mail.ReadWrite', 'https://outlook.office.com/Mail.Send']
    
    account = Account(credentials, scopes=scopes_office)
    6). Nếu bạn chỉ muốn hoạt động làm mới tiếp theo không lưu trữ mã thông báo, bạn sẽ phải đặt nó trở lại đúng sau đó.

Để lưu trữ mã thông báo, bạn sẽ phải cung cấp một tokenbackend được cấu hình đúng.

Trên thực tế, chỉ có hai lần thực hiện (nhưng bạn có thể dễ dàng thực hiện nhiều hơn như CookieBackend, Redisbackend, v.v.):

  • protocol_office = MSOffice365Protocol()
    
    scopes_office = protocol.get_scopes_for('message all')
    # scopes here are: ['https://outlook.office.com/Mail.ReadWrite', 'https://outlook.office.com/Mail.Send']
    
    account = Account(credentials, scopes=scopes_office)
    7 (phụ trợ mặc định): Lưu trữ và truy xuất mã thông báo từ hệ thống tệp. Mã thông báo được lưu trữ dưới dạng tệp.
  • protocol_office = MSOffice365Protocol()
    
    scopes_office = protocol.get_scopes_for('message all')
    # scopes here are: ['https://outlook.office.com/Mail.ReadWrite', 'https://outlook.office.com/Mail.Send']
    
    account = Account(credentials, scopes=scopes_office)
    8: Cửa hàng và Retves Token từ Datastore của Google Firestore. Mã thông báo được lưu trữ dưới dạng tài liệu trong một bộ sưu tập.

Ví dụ: sử dụng phụ trợ mã thông báo hệ thống tập tin:

from O365 import Account, FileSystemTokenBackend

credentials = ('id', 'secret')

# this will store the token under: "my_project_folder/my_folder/my_token.txt".
# you can pass strings to token_path or Path instances from pathlib
token_backend = FileSystemTokenBackend(token_path='my_folder', token_filename='my_token.txt')
account = Account(credentials, token_backend=token_backend)

# This account instance tokens will be stored on the token_backend configured before.
# You don't have to do anything more
# ...

Và bây giờ sử dụng cùng một ví dụ bằng cách sử dụng FirestoretokenBackend:

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
0

Để thực hiện một mã thông báo mới:

  1. Lớp con

    protocol_office = MSOffice365Protocol()
    
    scopes_office = protocol.get_scopes_for('message all')
    # scopes here are: ['https://outlook.office.com/Mail.ReadWrite', 'https://outlook.office.com/Mail.Send']
    
    account = Account(credentials, scopes=scopes_office)
    9

  2. Thực hiện các phương pháp sau:

    • from O365 import Account, FileSystemTokenBackend
      
      credentials = ('id', 'secret')
      
      # this will store the token under: "my_project_folder/my_folder/my_token.txt".
      # you can pass strings to token_path or Path instances from pathlib
      token_backend = FileSystemTokenBackend(token_path='my_folder', token_filename='my_token.txt')
      account = Account(credentials, token_backend=token_backend)
      
      # This account instance tokens will be stored on the token_backend configured before.
      # You don't have to do anything more
      # ...
      0 (đừng quên gọi
      from O365 import Account, FileSystemTokenBackend
      
      credentials = ('id', 'secret')
      
      # this will store the token under: "my_project_folder/my_folder/my_token.txt".
      # you can pass strings to token_path or Path instances from pathlib
      token_backend = FileSystemTokenBackend(token_path='my_folder', token_filename='my_token.txt')
      account = Account(credentials, token_backend=token_backend)
      
      # This account instance tokens will be stored on the token_backend configured before.
      # You don't have to do anything more
      # ...
      1)
    • from O365 import Account, FileSystemTokenBackend
      
      credentials = ('id', 'secret')
      
      # this will store the token under: "my_project_folder/my_folder/my_token.txt".
      # you can pass strings to token_path or Path instances from pathlib
      token_backend = FileSystemTokenBackend(token_path='my_folder', token_filename='my_token.txt')
      account = Account(credentials, token_backend=token_backend)
      
      # This account instance tokens will be stored on the token_backend configured before.
      # You don't have to do anything more
      # ...
      2: Điều này sẽ tải mã thông báo từ phụ trợ mong muốn và trả về một thể hiện
      from O365 import Account, FileSystemTokenBackend
      
      credentials = ('id', 'secret')
      
      # this will store the token under: "my_project_folder/my_folder/my_token.txt".
      # you can pass strings to token_path or Path instances from pathlib
      token_backend = FileSystemTokenBackend(token_path='my_folder', token_filename='my_token.txt')
      account = Account(credentials, token_backend=token_backend)
      
      # This account instance tokens will be stored on the token_backend configured before.
      # You don't have to do anything more
      # ...
      3 hoặc không
    • from O365 import Account, FileSystemTokenBackend
      
      credentials = ('id', 'secret')
      
      # this will store the token under: "my_project_folder/my_folder/my_token.txt".
      # you can pass strings to token_path or Path instances from pathlib
      token_backend = FileSystemTokenBackend(token_path='my_folder', token_filename='my_token.txt')
      account = Account(credentials, token_backend=token_backend)
      
      # This account instance tokens will be stored on the token_backend configured before.
      # You don't have to do anything more
      # ...
      4: Điều này sẽ lưu trữ
      from O365 import Account, FileSystemTokenBackend
      
      credentials = ('id', 'secret')
      
      # this will store the token under: "my_project_folder/my_folder/my_token.txt".
      # you can pass strings to token_path or Path instances from pathlib
      token_backend = FileSystemTokenBackend(token_path='my_folder', token_filename='my_token.txt')
      account = Account(credentials, token_backend=token_backend)
      
      # This account instance tokens will be stored on the token_backend configured before.
      # You don't have to do anything more
      # ...
      5 trong phụ trợ mong muốn.
    • Tùy chọn bạn có thể thực hiện:
      from O365 import Account, FileSystemTokenBackend
      
      credentials = ('id', 'secret')
      
      # this will store the token under: "my_project_folder/my_folder/my_token.txt".
      # you can pass strings to token_path or Path instances from pathlib
      token_backend = FileSystemTokenBackend(token_path='my_folder', token_filename='my_token.txt')
      account = Account(credentials, token_backend=token_backend)
      
      # This account instance tokens will be stored on the token_backend configured before.
      # You don't have to do anything more
      # ...
      6,
      from O365 import Account, FileSystemTokenBackend
      
      credentials = ('id', 'secret')
      
      # this will store the token under: "my_project_folder/my_folder/my_token.txt".
      # you can pass strings to token_path or Path instances from pathlib
      token_backend = FileSystemTokenBackend(token_path='my_folder', token_filename='my_token.txt')
      account = Account(credentials, token_backend=token_backend)
      
      # This account instance tokens will be stored on the token_backend configured before.
      # You don't have to do anything more
      # ...
      7 và
      from O365 import Account, FileSystemTokenBackend
      
      credentials = ('id', 'secret')
      
      # this will store the token under: "my_project_folder/my_folder/my_token.txt".
      # you can pass strings to token_path or Path instances from pathlib
      token_backend = FileSystemTokenBackend(token_path='my_folder', token_filename='my_token.txt')
      account = Account(credentials, token_backend=token_backend)
      
      # This account instance tokens will be stored on the token_backend configured before.
      # You don't have to do anything more
      # ...
      8

Phương pháp

from O365 import Account, FileSystemTokenBackend

credentials = ('id', 'secret')

# this will store the token under: "my_project_folder/my_folder/my_token.txt".
# you can pass strings to token_path or Path instances from pathlib
token_backend = FileSystemTokenBackend(token_path='my_folder', token_filename='my_token.txt')
account = Account(credentials, token_backend=token_backend)

# This account instance tokens will be stored on the token_backend configured before.
# You don't have to do anything more
# ...
8 được dự định sẽ được triển khai cho các môi trường nơi nhiều trường hợp kết nối đang chạy trên Paralel. Phương pháp này nên kiểm tra xem có phải là lúc để làm mới mã thông báo hay không. Phần phụ trợ được chọn có thể lưu trữ một lá cờ ở đâu đó để trả lời câu hỏi này. Điều này có thể tránh các điều kiện chủng tộc giữa các trường hợp khác nhau cố gắng làm mới mã thông báo cùng một lúc, khi chỉ có một người nên làm mới. Phương thức sẽ trả về ba giá trị có thể thực hiện:

  • Đúng: Sau đó, kết nối sẽ làm mới mã thông báo.: then the Connection will refresh the token.
  • Sai: Sau đó, kết nối sẽ không làm mới mã thông báo.: then the Connection will NOT refresh the token.
  • Không: Sau đó, phương thức này đã thực hiện làm mới và do đó kết nối không phải.: then this method already executed the refresh and therefore the Connection does not have to.

Theo mặc định, điều này luôn trả về đúng vì nó đang hoạt động, không có kết nối song song nào chạy cùng một lúc.

Có hai ví dụ về phương pháp này trong thư mục ví dụ ở đây.

Giao thức

Các giao thức xử lý các khía cạnh của giao tiếp giữa các API khác nhau. Dự án này sử dụng API Microsoft Graph (theo mặc định) hoặc API Office 365. Nhưng, bạn có thể sử dụng nhiều API Microsoft khác miễn là bạn thực hiện giao thức cần thiết.

Bạn có thể sử dụng cái này hay cái khác:

  • scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)
    
    account = Account(credentials)
    
    if not account.is_authenticated:  # will check if there is a token and has not expired
        # ask for a login
        # console based authentication See Authentication for other flows
        account.authenticate(scopes=scopes)
    
    # now we are autheticated
    # use the library from now on
    
    # ...
    00 để sử dụng API Microsoft Graph
  • scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)
    
    account = Account(credentials)
    
    if not account.is_authenticated:  # will check if there is a token and has not expired
        # ask for a login
        # console based authentication See Authentication for other flows
        account.authenticate(scopes=scopes)
    
    # now we are autheticated
    # use the library from now on
    
    # ...
    01 để sử dụng API Office 365

Cả hai giao thức đều giống nhau nhưng hãy xem xét như sau:

Lý do để sử dụng

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
00:

  • Đây là giao thức được đề xuất bởi Microsoft.
  • Nó có thể truy cập nhiều tài nguyên hơn Office 365 (ví dụ: OneDrive)

Lý do để sử dụng

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
01:

  • Nó có thể gửi email với các tệp đính kèm lên tới 150 MB. MSGRAPH CHỈ cho phép 4MB trên mỗi yêu cầu (Cập nhật: Bắt đầu từ ngày 22 tháng 10, bạn có thể tải lên các tệp lên đến 150MB với phiên bản beta của MSGRAPHPROTOCOL)beta version)

Giao thức mặc định được sử dụng bởi lớp

@route('/stepone')
def auth_step_one():

    callback = 'my absolute url to auth_step_two_callback'
    account = Account(credentials)
    url, state = account.con.get_authorization_url(requested_scopes=my_scopes,
                                                   redirect_uri=callback)
    
    # the state must be saved somewhere as it will be needed later
    my_db.store_state(state) # example...
    
    return redirect(url)

@route('/steptwo')
def auth_step_two_callback():
    account = Account(credentials)
    
    # retreive the state saved in auth_step_one
    my_saved_state = my_db.get_state()  # example...
    
    # rebuild the redirect_uri used in auth_step_one
    callback = 'my absolute url to auth_step_two_callback'
    
    result = account.con.request_token(request.url, 
                                       state=my_saved_state,
                                       redirect_uri=callback)
    # if result is True, then authentication was succesful 
    #  and the auth token is stored in the token backend
    if result:
        return render_template('auth_complete.html')
    # else ....
8 là
scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
00.

Bạn có thể thực hiện các giao thức của riêng mình bằng cách kế thừa từ

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
06 để liên lạc với các API Microsoft khác.

Bạn có thể khởi tạo và sử dụng các giao thức như thế này:

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
1

Resources:

Mỗi điểm cuối API yêu cầu một tài nguyên. Điều này thường xác định chủ sở hữu của dữ liệu. Mỗi giao thức mặc định là tài nguyên 'tôi'. 'Me' là người dùng đã đồng ý, nhưng bạn có thể thay đổi hành vi này bằng cách cung cấp một tài nguyên mặc định khác cho hàm tạo giao thức.

Lưu ý: Khi sử dụng phương thức xác thực "Với danh tính của riêng bạn", tài nguyên 'ME' bị ghi đè để bị trống vì phương thức xác thực đã nói rằng bạn đang đăng nhập bằng danh tính của chính mình.

Ví dụ: khi truy cập hộp thư được chia sẻ:

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
2

Điều này có thể được thực hiện tuy nhiên tại bất kỳ điểm nào. Ví dụ: ở cấp độ giao thức:

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
3

Thay vì xác định tài nguyên được sử dụng ở cấp độ tài khoản hoặc giao thức, bạn có thể cung cấp nó cho mỗi trường hợp sử dụng như sau:

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
4

Thông thường bạn sẽ làm việc với tài nguyên 'Me' mặc định, nhưng bạn cũng có thể sử dụng một trong những điều sau đây:

  • 'Tôi': Người dùng đã đồng ý. mặc định cho mọi giao thức. Ghi đè khi sử dụng phương thức xác thực "Với danh tính của riêng bạn" (chỉ có sẵn trên ủy quyền auth_flow_type).: the user which has given consent. the default for every protocol. Overwritten when using "with your own identity" authentication method (Only available on the authorization auth_flow_type).
  • 'Người dùng:': Hộp thư được chia sẻ hoặc tài khoản người dùng mà bạn có quyền. Nếu bạn không cung cấp 'người dùng:' dù sao cũng sẽ bị suy ra.: a shared mailbox or a user account for which you have permissions. If you don't provide 'user:' will be infered anyways.
  • 'Trang web: SharePoint-site-id': ID trang web SharePoint.: a sharepoint site id.
  • 'Nhóm: Nhóm trang web-ID': A Office365 ID nhóm.: a office365 group id.

Bằng cách đặt tiền tố tài nguyên (chẳng hạn như 'người dùng:' hoặc 'nhóm:'), bạn giúp thư viện hiểu loại tài nguyên. Bạn cũng có thể vượt qua nó như 'người dùng/'. Tương tự áp dụng cho các tiền tố tài nguyên khác.'user:' or 'group:') you help the library understand the type of resource. You can also pass it like 'users/'. Same applies to the other resource prefixes.

Lớp tài khoản và mô -đun

Thông thường bạn sẽ chỉ cần làm việc với lớp

@route('/stepone')
def auth_step_one():

    callback = 'my absolute url to auth_step_two_callback'
    account = Account(credentials)
    url, state = account.con.get_authorization_url(requested_scopes=my_scopes,
                                                   redirect_uri=callback)
    
    # the state must be saved somewhere as it will be needed later
    my_db.store_state(state) # example...
    
    return redirect(url)

@route('/steptwo')
def auth_step_two_callback():
    account = Account(credentials)
    
    # retreive the state saved in auth_step_one
    my_saved_state = my_db.get_state()  # example...
    
    # rebuild the redirect_uri used in auth_step_one
    callback = 'my absolute url to auth_step_two_callback'
    
    result = account.con.request_token(request.url, 
                                       state=my_saved_state,
                                       redirect_uri=callback)
    # if result is True, then authentication was succesful 
    #  and the auth token is stored in the token backend
    if result:
        return render_template('auth_complete.html')
    # else ....
8. Đây là một trình bao bọc xung quanh tất cả các chức năng.

Nhưng bạn cũng chỉ có thể làm việc với các mảnh bạn muốn.

Ví dụ: thay vì:

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
5

Bạn chỉ có thể làm việc với các phần cần thiết:

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
6

Nó cũng dễ dàng thực hiện một lớp tùy chỉnh.

Chỉ cần kế thừa từ

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
08, xác định các điểm cuối và sử dụng kết nối để thực hiện các yêu cầu. Nếu cần cũng kế thừa từ giao thức để xử lý các khía cạnh kết hợp khác nhau với máy chủ API.

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
7

Hộp thư

Hộp thư nhóm chức năng của cả tin nhắn và thư mục email.

Đây là những phạm vi cần thiết để làm việc với các lớp

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
09 và
scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
10.

Phạm vi thôBao gồm trong phạm vi trợ giúpSự mô tả
Mail.readhộp thưChỉ đọc hộp thư của tôi
Mail.read.sharedMailbox_sharedChỉ đọc hộp thư người dùng / người dùng khác
Mail.sendmessage_send, message_allChỉ để gửi tin nhắn
Mail.send.sharedmessage_send_shared, message_all_sharedChỉ để gửi tin nhắn dưới dạng hộp thư khác / người dùng
Mail.ReadWritemessage_allĐể đọc và lưu tin nhắn trong hộp thư của tôi
Mail.readwrite.sharedmessage_all_sharedĐể đọc và lưu tin nhắn trong hộp thư người dùng / người dùng khác

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
8

Thư mục email

Đại diện cho một

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
11 trong hộp thư email của bạn.

Bạn có thể nhận bất kỳ thư mục nào trong hộp thư của mình bằng cách yêu cầu các thư mục con hoặc lọc theo tên.

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
9

Thông điệp

Một đối tượng email với tất cả dữ liệu và phương pháp.

Tạo một thông điệp dự thảo dễ dàng như thế này:

from O365 import Account
credentials = ('my_client_id', 'my_client_secret')

# the default protocol will be Microsoft Graph
# the default authentication method will be "on behalf of a user"

account = Account(credentials)
if account.authenticate(scopes=['basic', 'message_all']):
   print('Authenticated!')

# 'basic' adds: 'offline_access' and 'https://graph.microsoft.com/User.Read'
# 'message_all' adds: 'https://graph.microsoft.com/Mail.ReadWrite' and 'https://graph.microsoft.com/Mail.Send'
0

Làm việc với các email đã lưu cũng dễ dàng:

from O365 import Account
credentials = ('my_client_id', 'my_client_secret')

# the default protocol will be Microsoft Graph
# the default authentication method will be "on behalf of a user"

account = Account(credentials)
if account.authenticate(scopes=['basic', 'message_all']):
   print('Authenticated!')

# 'basic' adds: 'offline_access' and 'https://graph.microsoft.com/User.Read'
# 'message_all' adds: 'https://graph.microsoft.com/Mail.ReadWrite' and 'https://graph.microsoft.com/Mail.Send'
1

Gửi hình ảnh nội tuyến

Bạn có thể gửi hình ảnh nội tuyến bằng cách làm điều này:

from O365 import Account
credentials = ('my_client_id', 'my_client_secret')

# the default protocol will be Microsoft Graph
# the default authentication method will be "on behalf of a user"

account = Account(credentials)
if account.authenticate(scopes=['basic', 'message_all']):
   print('Authenticated!')

# 'basic' adds: 'offline_access' and 'https://graph.microsoft.com/User.Read'
# 'message_all' adds: 'https://graph.microsoft.com/Mail.ReadWrite' and 'https://graph.microsoft.com/Mail.Send'
2

Lấy các tiêu đề tin nhắn

Bạn có thể truy xuất các tiêu đề tin nhắn bằng cách làm điều này:

from O365 import Account
credentials = ('my_client_id', 'my_client_secret')

# the default protocol will be Microsoft Graph
# the default authentication method will be "on behalf of a user"

account = Account(credentials)
if account.authenticate(scopes=['basic', 'message_all']):
   print('Authenticated!')

# 'basic' adds: 'offline_access' and 'https://graph.microsoft.com/User.Read'
# 'message_all' adds: 'https://graph.microsoft.com/Mail.ReadWrite' and 'https://graph.microsoft.com/Mail.Send'
3

Lưu ý rằng chỉ các tiêu đề tin nhắn và các thuộc tính khác được thêm vào câu lệnh Chọn sẽ có mặt.

Tiết kiệm như EML

Tin nhắn và tin nhắn đính kèm có thể được lưu dưới dạng *.eml.

  • Lưu tin nhắn dưới dạng "EML":

    from O365 import Account
    credentials = ('my_client_id', 'my_client_secret')
    
    # the default protocol will be Microsoft Graph
    # the default authentication method will be "on behalf of a user"
    
    account = Account(credentials)
    if account.authenticate(scopes=['basic', 'message_all']):
       print('Authenticated!')
    
    # 'basic' adds: 'offline_access' and 'https://graph.microsoft.com/User.Read'
    # 'message_all' adds: 'https://graph.microsoft.com/Mail.ReadWrite' and 'https://graph.microsoft.com/Mail.Send'
    4

  • Lưu thông báo đính kèm dưới dạng "EML":

    Cẩn thận: Không có cách nào để xác định rằng một tệp đính kèm trên thực tế là một thông điệp. Bạn chỉ có thể kiểm tra xem tệp đính kèm.attachment_type == 'mục'. Nếu thuộc loại "mục" thì nó có thể là một thông báo (hoặc một sự kiện, v.v.). Bạn sẽ phải tự xác định điều này.

    from O365 import Account
    credentials = ('my_client_id', 'my_client_secret')
    
    # the default protocol will be Microsoft Graph
    # the default authentication method will be "on behalf of a user"
    
    account = Account(credentials)
    if account.authenticate(scopes=['basic', 'message_all']):
       print('Authenticated!')
    
    # 'basic' adds: 'offline_access' and 'https://graph.microsoft.com/User.Read'
    # 'message_all' adds: 'https://graph.microsoft.com/Mail.ReadWrite' and 'https://graph.microsoft.com/Mail.Send'
    5

Sổ địa chỉ

Nhóm địa chỉ Nhóm chức năng của cả thư mục liên hệ và liên hệ. Các nhóm phân phối Outlook không được hỗ trợ (bởi API của Microsoft).

Đây là những phạm vi cần thiết để làm việc với các lớp

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
12 và
scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
13.

Phạm vi thôBao gồm trong phạm vi trợ giúpSự mô tả
Mail.readhộp thưChỉ đọc hộp thư của tôi
Mail.read.sharedMailbox_sharedChỉ đọc hộp thư người dùng / người dùng khác
Mail.sendmessage_send, message_allChỉ để gửi tin nhắn
Mail.send.sharedmessage_send_shared, message_all_sharedChỉ để gửi tin nhắn dưới dạng hộp thư khác / người dùng
Mail.ReadWritemessage_allĐể đọc và lưu tin nhắn trong hộp thư của tôi

Liên hệ với các thư mục

Đại diện cho một thư mục trong phần Liên hệ của bạn trong Office 365. Lớp địa chỉ đại diện cho thư mục chính (đó là một thư mục).

Bạn có thể nhận bất kỳ thư mục nào trong sổ địa chỉ của mình bằng cách yêu cầu các thư mục con hoặc lọc theo tên.

from O365 import Account
credentials = ('my_client_id', 'my_client_secret')

# the default protocol will be Microsoft Graph
# the default authentication method will be "on behalf of a user"

account = Account(credentials)
if account.authenticate(scopes=['basic', 'message_all']):
   print('Authenticated!')

# 'basic' adds: 'offline_access' and 'https://graph.microsoft.com/User.Read'
# 'message_all' adds: 'https://graph.microsoft.com/Mail.ReadWrite' and 'https://graph.microsoft.com/Mail.Send'
6

Danh sách địa chỉ toàn cầu

API Office 365 (cũng không phải API biểu đồ MS) không có khái niệm như danh sách địa chỉ toàn cầu Outlook. Tuy nhiên, bạn có thể sử dụng API người dùng để truy cập tất cả người dùng trong tổ chức của bạn.

Không có sự đồng ý của quản trị viên, bạn chỉ có thể truy cập một vài thuộc tính của mỗi người dùng như tên và email và litte nhiều hơn. Bạn có thể tìm kiếm theo tên hoặc truy xuất một liên hệ chỉ định email hoàn chỉnh.

  • Cần có sự cho phép cơ bản là người dùng.Readbasic.all (thông tin giới hạn)
  • Permision đầy đủ là người dùng.read. Tất cả nhưng cần sự đồng ý của quản trị viên.

Để tìm kiếm danh sách địa chỉ toàn cầu (API người dùng):

from O365 import Account
credentials = ('my_client_id', 'my_client_secret')

# the default protocol will be Microsoft Graph
# the default authentication method will be "on behalf of a user"

account = Account(credentials)
if account.authenticate(scopes=['basic', 'message_all']):
   print('Authenticated!')

# 'basic' adds: 'offline_access' and 'https://graph.microsoft.com/User.Read'
# 'message_all' adds: 'https://graph.microsoft.com/Mail.ReadWrite' and 'https://graph.microsoft.com/Mail.Send'
7

Để lấy liên hệ qua email của họ:

from O365 import Account
credentials = ('my_client_id', 'my_client_secret')

# the default protocol will be Microsoft Graph
# the default authentication method will be "on behalf of a user"

account = Account(credentials)
if account.authenticate(scopes=['basic', 'message_all']):
   print('Authenticated!')

# 'basic' adds: 'offline_access' and 'https://graph.microsoft.com/User.Read'
# 'message_all' adds: 'https://graph.microsoft.com/Mail.ReadWrite' and 'https://graph.microsoft.com/Mail.Send'
8

Liên lạc

Mọi thứ được trả lại từ một ví dụ

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
12 là một ví dụ
scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
13. Danh bạ có tất cả các thông tin được lưu trữ dưới dạng thuộc tính

Tạo liên hệ từ

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
12:

from O365 import Account
credentials = ('my_client_id', 'my_client_secret')

# the default protocol will be Microsoft Graph
# the default authentication method will be "on behalf of a user"

account = Account(credentials)
if account.authenticate(scopes=['basic', 'message_all']):
   print('Authenticated!')

# 'basic' adds: 'offline_access' and 'https://graph.microsoft.com/User.Read'
# 'message_all' adds: 'https://graph.microsoft.com/Mail.ReadWrite' and 'https://graph.microsoft.com/Mail.Send'
9

Thư mục và người dùng

Đối tượng thư mục có thể truy xuất người dùng.

Một thể hiện người dùng chứa mặc định các thuộc tính cơ bản của người dùng. Nếu bạn muốn bao gồm nhiều hơn, bạn sẽ phải chọn các thuộc tính mong muốn theo cách thủ công.

Kiểm tra danh sách địa chỉ toàn cầu để biết thêm thông tin.

from O365 import Account

credentials = ('my_client_id', 'my_client_secret')

# the default protocol will be Microsoft Graph

account = Account(credentials, auth_flow_type='credentials', tenant_id='my-tenant-id')
if account.authenticate():
   print('Authenticated!')
0

Lịch

Chức năng lịch và sự kiện là nhóm trong một đối tượng

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
17.

Một ví dụ

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
17 có thể liệt kê và tạo lịch. Nó cũng có thể liệt kê hoặc tạo các sự kiện trên lịch người dùng mặc định. Để sử dụng các lịch khác, hãy sử dụng ví dụ
scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
19.

Đây là những phạm vi cần thiết để làm việc với các lớp

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
17,
scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
19 và
scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
22.

Phạm vi thôBao gồm trong phạm vi trợ giúpSự mô tả
Lịch.ReadlịchChỉ đọc lịch cá nhân của tôi
Lịch.Read.sharedLịch_SharedChỉ đọc lịch hộp thư / người dùng khác
Lịch.ReadWriteLịch_allĐể đọc và lưu lịch cá nhân
Lịch.ReadWrite.sharedlịch_shared_allĐể đọc và lưu lịch từ hộp thư người dùng / người dùng khác

Làm việc với ví dụ

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
17:

from O365 import Account

credentials = ('my_client_id', 'my_client_secret')

# the default protocol will be Microsoft Graph

account = Account(credentials, auth_flow_type='credentials', tenant_id='my-tenant-id')
if account.authenticate():
   print('Authenticated!')
1

Làm việc với các trường hợp

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
19:

from O365 import Account

credentials = ('my_client_id', 'my_client_secret')

# the default protocol will be Microsoft Graph

account = Account(credentials, auth_flow_type='credentials', tenant_id='my-tenant-id')
if account.authenticate():
   print('Authenticated!')
2

Ghi chú về lịch và sự kiện:

  1. Include_recurring=True:

    Điều quan trọng là phải biết rằng khi truy vấn các sự kiện với

    scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)
    
    account = Account(credentials)
    
    if not account.is_authenticated:  # will check if there is a token and has not expired
        # ask for a login
        # console based authentication See Authentication for other flows
        account.authenticate(scopes=scopes)
    
    # now we are autheticated
    # use the library from now on
    
    # ...
    25 (là mặc định), bạn phải cung cấp một tham số truy vấn với các thuộc tính bắt đầu và kết thúc được xác định. Không giống như khi sử dụng
    scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)
    
    account = Account(credentials)
    
    if not account.is_authenticated:  # will check if there is a token and has not expired
        # ask for a login
        # console based authentication See Authentication for other flows
        account.authenticate(scopes=scopes)
    
    # now we are autheticated
    # use the library from now on
    
    # ...
    26, các thuộc tính này sẽ không lọc dữ liệu dựa trên các hoạt động bạn đặt trên truy vấn (Greater_equal, ít hơn, v.v.) nhưng chỉ cần lọc các sự kiện bắt đầu dữ liệu giữa các dữ liệu bắt đầu và kết thúc được cung cấp.

  2. Lịch chia sẻ:

    Có một số vấn đề đã biết khi làm việc với lịch chung trong Microsoft Braph.

  3. Tệp đính kèm sự kiện:

    Vì một số lý do không biết, Microsoft không cho phép tải lên một tệp đính kèm tại thời điểm tạo sự kiện (trái ngược với tệp đính kèm tin nhắn). Xem điều này. Vì vậy, để tải các tệp đính kèm lên các sự kiện, trước tiên hãy lưu sự kiện, sau đó đính kèm tin nhắn và lưu lại.

Nhiệm vụ

Chức năng tác vụ được nhóm trong một đối tượng

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
27. Lưu ý rằng chức năng tác vụ hiện chỉ hoạt động với giao thức
scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
01.

Một ví dụ

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
27 có thể liệt kê và tạo các thư mục nhiệm vụ. Nó cũng có thể liệt kê hoặc tạo các tác vụ trên thư mục người dùng mặc định. Để sử dụng các thư mục khác, sử dụng thể hiện
scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
11.

Đây là những phạm vi cần thiết để làm việc với các lớp

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
27,
scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
11 và
scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
33.

Phạm vi thôBao gồm trong phạm vi trợ giúpSự mô tả
Lịch.ReadlịchChỉ đọc lịch cá nhân của tôi
Lịch.Read.sharedLịch_SharedĐể đọc và lưu lịch cá nhân

Lịch.ReadWrite.shared

from O365 import Account

credentials = ('my_client_id', 'my_client_secret')

# the default protocol will be Microsoft Graph

account = Account(credentials, auth_flow_type='credentials', tenant_id='my-tenant-id')
if account.authenticate():
   print('Authenticated!')
3

lịch_shared_all

from O365 import Account

credentials = ('my_client_id', 'my_client_secret')

# the default protocol will be Microsoft Graph

account = Account(credentials, auth_flow_type='credentials', tenant_id='my-tenant-id')
if account.authenticate():
   print('Authenticated!')
4

Để đọc và lưu lịch từ hộp thư người dùng / người dùng khác

Làm việc với ví dụ

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
17:

Làm việc với các trường hợp

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
19:

Ghi chú về lịch và sự kiện:

Điều quan trọng là phải biết rằng khi truy vấn các sự kiện với

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
25 (là mặc định), bạn phải cung cấp một tham số truy vấn với các thuộc tính bắt đầu và kết thúc được xác định. Không giống như khi sử dụng
scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
26, các thuộc tính này sẽ không lọc dữ liệu dựa trên các hoạt động bạn đặt trên truy vấn (Greater_equal, ít hơn, v.v.) nhưng chỉ cần lọc các sự kiện bắt đầu dữ liệu giữa các dữ liệu bắt đầu và kết thúc được cung cấp.

Phạm vi thôBao gồm trong phạm vi trợ giúpSự mô tả
Lịch.Read lịch
Chỉ đọc lịch cá nhân của tôiLịch.Read.sharedLịch_Shared
Chỉ đọc lịch hộp thư / người dùng khác Lịch.ReadWrite
Lịch_allĐể đọc và lưu lịch cá nhânLịch.ReadWrite.shared

from O365 import Account

credentials = ('my_client_id', 'my_client_secret')

# the default protocol will be Microsoft Graph

account = Account(credentials, auth_flow_type='credentials', tenant_id='my-tenant-id')
if account.authenticate():
   print('Authenticated!')
5

lịch_shared_all

Khi sao chép DriveItem, API có thể trả về một bản sao trực tiếp của mục hoặc con trỏ vào tài nguyên sẽ thông báo về tiến trình của hoạt động sao chép.

from O365 import Account

credentials = ('my_client_id', 'my_client_secret')

# the default protocol will be Microsoft Graph

account = Account(credentials, auth_flow_type='credentials', tenant_id='my-tenant-id')
if account.authenticate():
   print('Authenticated!')
6

Bạn cũng có thể làm việc với quyền chia sẻ:

from O365 import Account

credentials = ('my_client_id', 'my_client_secret')

# the default protocol will be Microsoft Graph

account = Account(credentials, auth_flow_type='credentials', tenant_id='my-tenant-id')
if account.authenticate():
   print('Authenticated!')
7

Bạn cũng có thể:

from O365 import Account

credentials = ('my_client_id', 'my_client_secret')

# the default protocol will be Microsoft Graph

account = Account(credentials, auth_flow_type='credentials', tenant_id='my-tenant-id')
if account.authenticate():
   print('Authenticated!')
8

Excel

Bạn có thể tương tác với các tệp Excel mới (.xlsx) được lưu trữ trong OneDrive hoặc thư viện tài liệu SharePoint. Bạn có thể truy xuất sổ làm việc, bảng tính, bảng và thậm chí dữ liệu di động. Bạn cũng có thể viết thư cho bất kỳ Excel trực tuyến.

Để làm việc với các tệp Excel, trước tiên bạn phải truy xuất một thể hiện

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
45 bằng cách sử dụng chức năng OneDrive hoặc SharePoint.

Các phạm vi cần thiết để làm việc với các lớp liên quan đến

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
46 và Excel giống nhau được sử dụng bởi OneDrive.

Đây là cách bạn cập nhật giá trị ô:

from O365 import Account

credentials = ('my_client_id', 'my_client_secret')

# the default protocol will be Microsoft Graph

account = Account(credentials, auth_flow_type='credentials', tenant_id='my-tenant-id')
if account.authenticate():
   print('Authenticated!')
9

Phiên làm bài tập

Khi tương tác với Excel, bạn có thể sử dụng phiên làm việc để thực hiện các thay đổi một cách hiệu quả theo cách liên tục hoặc không liên tục. Phiên này trở nên hữu ích nếu bạn thực hiện nhiều thay đổi đối với tệp Excel.

Mặc định là sử dụng một phiên một cách liên tục. Phiên hết hạn sau một thời gian không hoạt động. Khi làm việc với các phiên liên tục, các phiên mới sẽ tự động được tạo khi các phiên cũ hết hạn.

Tuy nhiên, bạn có thể thay đổi điều này khi tạo phiên bản

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
47:

account = Account(credentials)
account.authenticate(scopes=['basic', 'message_all'])
0

Các đối tượng có sẵn

Sau khi tạo phiên bản

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
46, bạn sẽ có quyền truy cập vào các đối tượng sau:

  • Bảng tính
  • Phạm vi và được đặt tên
  • Bàn, bảng tính và bảng
  • Rangeformat (để định dạng phạm vi)
  • Biểu đồ (không có sẵn cho bây giờ)

Vài ví dụ:

Đặt định dạng cho một phạm vi đã cho

account = Account(credentials)
account.authenticate(scopes=['basic', 'message_all'])
1

Các cột tự động:

account = Account(credentials)
account.authenticate(scopes=['basic', 'message_all'])
2

Nhận các giá trị từ bảng:

account = Account(credentials)
account.authenticate(scopes=['basic', 'message_all'])
3

Điểm chia sẻ

API SharePoint được thực hiện nhưng chưa có tài liệu nào. Nhìn vào tệp SharePoint.py để có được thông tin chi tiết.

Đây là những phạm vi cần thiết để làm việc với các lớp

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
49 và
scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
50.

Phạm vi thôBao gồm trong phạm vi trợ giúpSự mô tả
Trang web.read.allđiểm chia sẻChỉ để đọc các trang web, danh sách và mục
Trang web.ReadWrite.allSharePoint_DLĐể đọc và lưu các trang web, danh sách và mục

Người lập kế hoạch

API kế hoạch được thực hiện nhưng chưa có tài liệu nào. Nhìn vào tệp Planner.py để có được thông tin chi tiết.

Chức năng kế hoạch yêu cầu sự cho phép của quản trị viên.

Danh mục triển vọng

Bạn có thể rút lại, cập nhật, tạo và xóa các danh mục Outlook. Các danh mục này có thể được sử dụng để phân loại tin nhắn, sự kiện và danh bạ.

Đây là những phạm vi cần thiết để làm việc với các lớp

scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
49 và
scopes = ['my_required_scopes']  # you can use scope helpers here (see Permissions and Scopes section)

account = Account(credentials)

if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    # console based authentication See Authentication for other flows
    account.authenticate(scopes=scopes)

# now we are autheticated
# use the library from now on

# ...
50.

Phạm vi thôBao gồm trong phạm vi trợ giúpSự mô tả
Trang web.read.allđiểm chia sẻChỉ để đọc các trang web, danh sách và mục
Trang web.ReadWrite.allSharePoint_DLĐể đọc và lưu các trang web, danh sách và mục

Example:

account = Account(credentials)
account.authenticate(scopes=['basic', 'message_all'])
4

Người lập kế hoạch

API kế hoạch được thực hiện nhưng chưa có tài liệu nào. Nhìn vào tệp Planner.py để có được thông tin chi tiết.

Chức năng kế hoạch yêu cầu sự cho phép của quản trị viên.

Danh mục triển vọng

Bạn có thể rút lại, cập nhật, tạo và xóa các danh mục Outlook. Các danh mục này có thể được sử dụng để phân loại tin nhắn, sự kiện và danh bạ.

account = Account(credentials)
account.authenticate(scopes=['basic', 'message_all'])
5

MailboxSinstall.read

Bạn có thể rút lại, cập nhật, tạo và xóa các danh mục Outlook. Các danh mục này có thể được sử dụng để phân loại tin nhắn, sự kiện và danh bạ.

account = Account(credentials)
account.authenticate(scopes=['basic', 'message_all'])
6

MailboxSinstall.read

-

Chỉ đọc Cài đặt Outlook

Bạn có thể rút lại, cập nhật, tạo và xóa các danh mục Outlook. Các danh mục này có thể được sử dụng để phân loại tin nhắn, sự kiện và danh bạ.

account = Account(credentials)
account.authenticate(scopes=['basic', 'message_all'])
7

MailboxSinstall.read

account = Account(credentials)
account.authenticate(scopes=['basic', 'message_all'])
8

-

Chỉ đọc Cài đặt Outlook

MailboxSinstall.ReadWrite

account = Account(credentials)
account.authenticate(scopes=['basic', 'message_all'])
9

Cài đặt_all

Để đọc và viết cài đặt Outlook

Sử dụng