Làm cách nào để gửi email từ office 365 bằng python?

Trong bài viết Tải dữ liệu danh sách SharePoint của Microsoft 365 bằng Python, tôi đã cung cấp các bước chi tiết để tải dữ liệu Danh sách SharePoint thông qua gói msal. Để biết chi tiết về cách cài đặt các gói Python cần thiết cũng như cách thiết lập , hãy tham khảo bài viết về SharePoint.  

quyền thiết lập

Chúng tôi cần thêm quyền để gửi email. Thư. Gửi. Quyền này cho phép bạn gửi email với tư cách là bất kỳ người dùng nào

Làm cách nào để gửi email từ office 365 bằng python?

Sau khi thêm quyền, hãy đảm bảo bạn cũng cung cấp sự đồng ý cho ứng dụng

Tạo kịch bản để gửi email

Với mã thông báo người dùng và người dùng đã sẵn sàng, giờ đây chúng tôi có thể sử dụng chúng để gửi email trong ứng dụng khách Python

Thêm tập lệnh python với nội dung sau

import json
import msal

import requests

client_id = '***'
client_secret = '***'
tenant_id = '***'
authority = f"https://login.microsoftonline.com/{tenant_id}"

app = msal.ConfidentialClientApplication(
    client_id=client_id,
    client_credential=client_secret,
    authority=authority)

scopes = ["https://graph.microsoft.com/.default"]

result = None
result = app.acquire_token_silent(scopes, account=None)

if not result:
    print(
        "No suitable token exists in cache. Let's get a new one from Azure Active Directory.")
    result = app.acquire_token_for_client(scopes=scopes)

# if "access_token" in result:
#     print("Access token is " + result["access_token"])


if "access_token" in result:
    userId = "***"
    endpoint = f'https://graph.microsoft.com/v1.0/users/{userId}/sendMail'
    toUserEmail = "***"
    email_msg = {'Message': {'Subject': "Test Sending Email from Python",
                             'Body': {'ContentType': 'Text', 'Content': "This is a test email."},
                             'ToRecipients': [{'EmailAddress': {'Address': toUserEmail}}]
                             },
                 'SaveToSentItems': 'true'}
    r = requests.post(endpoint,
                      headers={'Authorization': 'Bearer ' + result['access_token']}, json=email_msg)
    if r.ok:
        print('Sent email successfully')
    else:
        print(r.json())
else:
    print(result.get("error"))
    print(result.get("error_description"))
    print(result.get("correlation_id"))

Hãy nhớ thay thế các biến được đánh dấu cho phù hợp. Đối với ID người dùng của tài khoản email, bạn có thể tìm thấy nó trong Azure Active Directory hoặc trung tâm quản trị Microsoft 365

Ngoài Microsoft Excel và PowerPoint, một công cụ văn phòng được sử dụng phổ biến khác là Outlook. Tôi nghĩ những người có kinh nghiệm làm việc sẽ quen thuộc với phần mềm Outlook. Nó được sử dụng để nhận và gửi email để liên lạc

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 trở nên dễ dàng theo cách Pythonic. Truy cập vào Email, Lịch, Danh bạ, OneDrive, v.v. Dễ thực hiện theo cách dễ dàng và dễ hiểu đối với người mới bắt đầu và cảm thấy phù hợp với lập trình viên python dày dạn kinh nghiệm

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

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

  • Janscas
  • cung thủ toben
  • Geethanadh

Chúng tôi luôn mở 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 shell script

    @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 copy lệnh từ file khi dùng trên windows

Ví dụ nhanh về gửi tin nhắn

from O365 import Account

credentials = ('client_id', 'client_secret')

account = Account(credentials)
m = account.new_message()
m.to.add('[email protected]')
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?

  • Hỗ trợ gần như đầy đủ cho MsGraph và Office 365 Rest Api
  • Lớp trừu tượng tốt giữa mỗi Api. Thay đổi api (Graph so với Office365) và đừng lo lắng về việc triển khai nội bộ api
  • Hỗ trợ oauth đầy đủ với việc tự động xử lý mã thông báo làm mới
  • Tự động xử lý giữa datetimes địa phương và datetimes máy chủ. Làm việc với thời gian địa phương của bạn và để thư viện này làm 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ư dùng chung, 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 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 vật phẩm 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 (lọc, sắp xếp, chọn và tìm kiếm)
  • ApiComponents mô-đun có thể được tạo và xây dựng để đạt được nhiều chức năng hơn

Dự án này cũng là một 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
  • liệt kê.
    @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 trọn gói
  • Chuyển đổi múi giờ và thời gian nhận biết múi giờ
  • Vân vân. (xem mã. )

Những gì sau đây 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
  • Loại 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 Outlook
  • công dụng

Cài đặt

O365 có sẵn trên pypi. tổ chức. Đơn giản 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. >= Trăn 3. 4

Phụ thuộc dự án được cài đặt bởi pip

  • yêu cầu
  • yêu cầu-oauthlib
  • soup4
  • chuỗi ký tự
  • python-dateutil
  • tzlocal
  • pytz

Cách sử dụng

Bước đầu tiên để có thể làm việc với thư viện này là đăng ký ứng dụng và truy xuất mã thông báo xác thực. Xem Xác thực

Bạn nên 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ụ

Mẫu phổ biến để kiểm tra xác thực và sử dụng thư viện là mẫu này

________số 8

xác thực

Bạn chỉ có thể xác thực bằng cách sử dụng oauth athentication vì Microsoft đã ngừng sử dụng basic auth vào ngày 1 tháng 11 năm 2018

Hiện tại có ba phương pháp xác thực

  • Xác thực thay mặt người dùng. Bất kỳ người dùng nào cũng sẽ đồng ý cho ứng dụng truy cập tài nguyên của nó. Luồng oauth này được gọi là luồng cấp mã ủy quyền. Đây là phương thức xác thực mặc định được sử dụng bởi thư viện này

  • Xác thực thay mặt cho người dùng (công khai). Tương tự như trước đây nhưng dành cho các ứng dụng công khai không thể bảo mật bí mật ứng dụng khách. Bí mật khách hàng là không cần thiết

  • Xác thực với danh tính của riêng bạn. Điều này sẽ sử dụng danh tính của riêng bạn (danh tính ứng dụng). Luồng oauth này được gọi là luồng cấp thông tin xác thực ứng dụng khách

    'Xác thực bằng danh tính của chính bạn' không phải là phương pháp được phép cho tài khoản Microsoft Personal

Khi nào nên sử dụng cái này hay cái kia và các yêu cầu

Chủ đềThay mặt người dùng (auth_flow_type=='ủy quyền')Thay mặt người dùng (công khai) (auth_flow_type=='công khai')Với danh tính của riêng bạn (auth_flow_type=='credentials')Đăng ký Ứng dụngBắt buộcYêu cầuYêu cầuChỉ cần có sự đồng ý của quản trị viên trên một số quyền nâng caoChỉ

*O365 sẽ tự động làm mới mã thông báo cho bạn trên một trong hai 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, miễn là 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 đồ thị của Microsoft, gần như tương tự áp dụng cho API REST của Office 365

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 tại Azure App Registrations

    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 nào và tài khoản Microsoft cá nhân (e. g. Skype, Xbox, Triển vọng. 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 được chèn vào hộp văn bản "URI chuyển hướng" vì chỉ cần chọn hộp kiểm bên cạnh liên kết này dường như là khô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ỳ uri nào khác nếu muốn

    5. Ghi lại ID ứng dụng (máy khách). 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 thời hạn tốt nhất là không bao giờ. Ghi lại giá trị của bí mật khách hàng được tạo ngay bây giờ. Nó sẽ được ẩn sau này

    7. Theo Quyền Api

      • Khi xác thực "thay mặt người dùng"
        1. thêm các quyền được ủy quyền cho Microsoft Graph mà bạn muốn (xem phạm vi)
        2. Bạn nên thêm quyền "offline_access". Nếu không phải là 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 Graph mà bạn muốn
        2. Nhấp vào nút Đồng ý của quản trị viên (nếu bạn có quyền của 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, hãy sử dụng

      1. Thư. Đọc viết
      2. Thư. Gửi
      3. Người sử dụng. Đọc
  2. Sau đó, bạn cần đăng nhập lần đầu tiên để nhận 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. Trong 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 ứng dụng khách, bạn có thể sử dụng loại luồng xác thực 'công khai' chỉ yêu cầu id ứng dụng khách

      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 ứng dụng khách và bí mật ứng dụng 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 phạm vi bạn đã thêm trước đó trên cổng đăng ký ứng dụng)

        Ghi chú. khi sử dụng xác thực "thay mặt người dùng", bạn có thể chuyển phạm vi cho phương thức 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 phương thức xác thực. Cách nào cũng đúng

        Bạn có thể vượt qua "phạm vi giao thức" (như. "https. // đồ thị. Microsoft. com/Lịch. ReadWrite") vào phương thức hoặc sử dụng "trình trợ giúp phạm vi" như ("message_all"). Nếu bạn vượt qua các phạm vi giao thức, thì 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 ....
        41 phải được khởi tạo với cùng một giao thức được sử dụng bởi các phạm vi. Bằng cách sử dụng trình trợ giúp phạm vi, bạn có thể trừu tượng hóa giao thức khỏi 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ình trợ giúp phạm vi". Chuyển đến phần giao thức để biết thêm về chúng

        Ví dụ (theo các quyền trước đó được thêm và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 ....
        5

        Khi sử dụng phương thức xác thực "thay mặt người dùng", lệnh gọi phương thức này sẽ in một url mà người dùng phải truy cập để đồng ý với ứng dụng về các quyền được yêu cầu

        Sau đó, người dùng phải truy cập url này và đồng ý với ứng dụng. Khi có sự đồng ý, trang sẽ chuyển hướng đến. "https. //đăng nhập. microsoftonline. com/common/oauth2/nativeclient" theo mặc định (bạn có thể thay đổi điều này) với 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 vào bảng điều khiển. Phương thức sau đó sẽ trả về True nếu lần đăng nhập thành công

    • Khi xác thực bằng danh tính của chính 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 ứng dụng khách và bí mật ứng dụng khách), chỉ định tham số
        @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 ....
        43 thành "thông tin xác thực". Bạn cũng cần cung cấp '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ã thông báo cho bạn và lưu trữ mã đó trong chương trình phụ trợ. Không cần tương tác 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ề True nếu xác thực thành công

        Ví dụ

        @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

  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ỉ tồn tại trong 60 phút, nhưng lần thử ứng dụng sẽ tự động yêu cầu mã thông báo truy cập mới

    Khi sử dụng phương thức xác thực "thay mặt người dùng", điều này được thực hiện thông qua 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ã thông báo làm mới chỉ tồn tạ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 lại mã thông báo truy cập mới (người dùng không cần sự đồng ý mới, chỉ cần đăng nhập). Nếu ứng dụng của bạn cần hoạt động trong hơn 90 ngày mà không có tương tác của người dùng và không tương tác với API, thì bạn phải thực hiện lệnh gọi định kỳ tớ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 ....
    45 trước khi 90 ngày trôi qua

    Bảo trọng. 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 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 ý 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. Nội dung sau áp dụng cho phương thức xác thực "thay mặt người dùng" vì đây là quy trình 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ần 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 bảng điều khiển. Cách tốt nhất để đạt được điều này là sử dụng 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 ....
    47 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

    @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

    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 ....
    47 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 thức sẽ trả về ________ 260 và in thông báo nếu thành công

    Mẹo. Khi sử dụng MacO, bảng điều khiển bị 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

    @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 ....
    61 ở đầu tập lệnh của bạn

  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 thực hiện theo các bước này

    1. Trước tiên, hãy đảm bảo rằng bạn đang sử dụng Phần cuối mã thông báo thích hợp để lưu trữ mã thông báo xác thực (Xem Lưu trữ mã thông báo bên dưới)
    2. Từ trình xử lý chuyển hướng người dùng đến url đăng nhập của 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 tất xác thực với trạng thái và dữ liệu khác

    Ví dụ sau được thực hiện bằng Flask

    @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

    @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 ....
    62 và
    @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 ....
    63 nếu 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 người dùng truy cập và sử dụng một số tài nguyên. 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 người dùng) hoặc quyền ứng dụng. Cái trước được sử dụng khi phương thức xác thực là "thay mặt 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 người dùng"

Ghi chú. 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ần phụ trợ mã thông báo

Sau đó, người dùng của thư viện này 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

Ghi chú. Nếu sau này bạn thay đổi phạm vi được yêu cầu, mã thông báo hiện tại sẽ bị mất giá trị 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. Đọc thư. Đọc Viết và Thư. Gửi quyền, nhưng ứng dụng chỉ có thể yêu cầu quyền truy cập vào Thư. Đọc Viết và Thư. gửi giấy phép. Điều này được thực hiện bằng cách cung cấp phạm vi cho đố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 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

@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

Phạm vi triển khai 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ỏ giúp nhóm chức năng phạm vi và trừu tượng hóa giao thức được sử dụng

Phạm vi HelperBao gồm phạm vi cơ bản'offline_access' và 'Người dùng. Đọc'hộp thư'Mail. Đọc'mailbox_shared'Mail. Đọc. Shared'message_send'Mail. Send'message_send_shared'Mail. Gửi. Shared'message_all'Mail. ReadWrite' và 'Thư. Gửi'message_all_shared'Mail. Đọc viết. Được chia sẻ' và 'Thư. Gửi. Shared'address_book'Danh bạ. Read'address_book_shared'Danh bạ. Đọc. Đã chia sẻ'address_book_all'Danh bạ. ReadWrite'address_book_all_shared'Danh bạ. Đọc viết. Shared'calendar'Calendars. Read'calendar_shared'Calendars. Đọc. Shared'calendar_all'Calendars. ReadWrite'calendar_shared_all'Calendars. Đọc viết. Nhiệm vụ được chia sẻ. Read'tasks_all'Tasks. ReadWrite'users'User. Đọc cơ bản. All'onedrive'Files. Đọc. All'onedrive_all'Files. Đọc viết. Tất cả các trang web'sharepoint'. Đọc. Tất cả'sharepoint_dl'Sites. Đọc viết. Tất cả các'

Bạn có thể nhận được các phạm vi giống như trước khi sử dụng các giao thức và trình trợ giúp phạm vi như thế nà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 ....
6

@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

Ghi chú. Khi vượt qua các phạm vi khi 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 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, trình trợ giúp phạm vi sẽ tự động được chuyển đổi thành hương vị giao thức. Đó là những nơi duy nhất bạn có thể sử dụng trình trợ giúp phạm vi. Bất kỳ đối tượng nào khác sử dụng phạm vi (chẳng hạn 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 vị trí và cách lưu trữ mã thông báo bằng cách sử dụng Phần cuối mã thông báo thích hợp

Bảo trọng. 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

Các phương thức tải mã thông báo

  • Thuộc tí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 ....
    69 sẽ cố tải mã thông báo nếu chưa được tả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 ....
    50. phương thức này được gọi khi không có phiên yêu cầu được đặt. Theo mặc định, nó sẽ không cố tải mã thông báo. Đặt
    @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 ....
    51 để tải nó

Phương pháp lưu trữ mã thông bá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 ....
    63. theo mặc định sẽ lưu trữ mã thông báo, nhưng bạn có thể đặt
    @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 ....
    53 để trá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 ....
    54. theo mặc định sẽ lưu trữ mã thông báo. Để tránh điều đó, hãy thay đổ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 ....
    55 thành Sai. Tuy nhiên, đây là cài đặt chung (chỉ ảnh hưởng đến 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 ....
    56). Nếu bạn chỉ muốn thao tác làm mới tiếp theo không lưu trữ mã thông báo, bạn sẽ phải đặt lại thành True sau đó

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

Trên thực tế, chỉ có hai cách được triển khai (nhưng bạn có thể dễ dàng triển khai nhiều hơn như Cookie Backend, Redis Backend, v.v. )

  • @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 ....
    57 (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
  • @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 ....
    58. Lưu trữ và truy xuất mã thông báo từ Kho dữ liệu Google Firestore. Mã thông báo được lưu trữ dưới dạng tài liệu trong bộ sưu tập

Ví dụ: sử dụng phụ trợ mã thông báo FileSystem

from O365 import Account

credentials = ('client_id', 'client_secret')

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

Và bây giờ sử dụng cùng một ví dụ 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

Để triển khai TokenBackend mới

  1. Phâ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 ....
    59

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

    • from O365 import Account
      
      credentials = ('client_id', 'client_secret')
      
      account = Account(credentials)
      m = account.new_message()
      m.to.add('[email protected]')
      m.subject = 'Testing!'
      m.body = "George Best quote: I've stopped drinking, but only while I'm asleep."
      m.send()
      90 (đừng quên gọi cho
      from O365 import Account
      
      credentials = ('client_id', 'client_secret')
      
      account = Account(credentials)
      m = account.new_message()
      m.to.add('[email protected]')
      m.subject = 'Testing!'
      m.body = "George Best quote: I've stopped drinking, but only while I'm asleep."
      m.send()
      91)
    • from O365 import Account
      
      credentials = ('client_id', 'client_secret')
      
      account = Account(credentials)
      m = account.new_message()
      m.to.add('[email protected]')
      m.subject = 'Testing!'
      m.body = "George Best quote: I've stopped drinking, but only while I'm asleep."
      m.send()
      92. điều này sẽ tải mã thông báo từ chương trình phụ trợ mong muốn và trả về phiên bản
      from O365 import Account
      
      credentials = ('client_id', 'client_secret')
      
      account = Account(credentials)
      m = account.new_message()
      m.to.add('[email protected]')
      m.subject = 'Testing!'
      m.body = "George Best quote: I've stopped drinking, but only while I'm asleep."
      m.send()
      93 hoặc Không
    • from O365 import Account
      
      credentials = ('client_id', 'client_secret')
      
      account = Account(credentials)
      m = account.new_message()
      m.to.add('[email protected]')
      m.subject = 'Testing!'
      m.body = "George Best quote: I've stopped drinking, but only while I'm asleep."
      m.send()
      94. cái này sẽ lưu trữ
      from O365 import Account
      
      credentials = ('client_id', 'client_secret')
      
      account = Account(credentials)
      m = account.new_message()
      m.to.add('[email protected]')
      m.subject = 'Testing!'
      m.body = "George Best quote: I've stopped drinking, but only while I'm asleep."
      m.send()
      95 trong phần phụ trợ mong muốn
    • Tùy chọn bạn có thể thực hiện.
      from O365 import Account
      
      credentials = ('client_id', 'client_secret')
      
      account = Account(credentials)
      m = account.new_message()
      m.to.add('[email protected]')
      m.subject = 'Testing!'
      m.body = "George Best quote: I've stopped drinking, but only while I'm asleep."
      m.send()
      96,
      from O365 import Account
      
      credentials = ('client_id', 'client_secret')
      
      account = Account(credentials)
      m = account.new_message()
      m.to.add('[email protected]')
      m.subject = 'Testing!'
      m.body = "George Best quote: I've stopped drinking, but only while I'm asleep."
      m.send()
      97 và
      from O365 import Account
      
      credentials = ('client_id', 'client_secret')
      
      account = Account(credentials)
      m = account.new_message()
      m.to.add('[email protected]')
      m.subject = 'Testing!'
      m.body = "George Best quote: I've stopped drinking, but only while I'm asleep."
      m.send()
      98

Phương pháp

from O365 import Account

credentials = ('client_id', 'client_secret')

account = Account(credentials)
m = account.new_message()
m.to.add('[email protected]')
m.subject = 'Testing!'
m.body = "George Best quote: I've stopped drinking, but only while I'm asleep."
m.send()
98 dự định sẽ được triển khai cho các môi trường có nhiều phiên bản Kết nối đang chạy song song. Phương pháp này sẽ kiểm tra xem đã đến lúc làm mới mã thông báo hay chưa. Chương trình phụ trợ đã chọn có thể lưu trữ cờ ở đâu đó để trả lời câu hỏi này. Điều này có thể tránh tình trạng chạy đua giữa các trường hợp khác nhau đang cố gắng làm mới mã thông báo cùng một lúc, khi chỉ một trường hợp thực hiện việc làm mới. Phương thức sẽ trả về ba giá trị có thể

  • ĐÚNG VẬY. sau đó Kết nối sẽ làm mới mã thông báo
  • Sai. sau đó Kết nối sẽ KHÔNG làm mới mã thông báo
  • Không có. thì phương thức này đã thực hiện làm mới và do đó Kết nối không phải

Theo mặc định, điều này luôn trả về True vì giả sử 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 liên lạc 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. Tuy nhiên, bạn có thể sử dụng nhiều API khác của Microsoft miễn là bạn triển khai giao thức cần thiết

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

  • 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 Microsoft Graph 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
    
    # ...
    01 để sử dụng API Office 365

Cả hai giao thức đều giống nhau nhưng hãy xem xét những điều 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 trê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 tệp đính kèm lên tới 150 MB. MSGraph chỉ cho phép 4 MB trên mỗi yêu cầu (CẬP NHẬT. Bắt đầu từ ngày 22 tháng 10 năm 2019, bạn có thể tải lên các tệp có dung lượng tối đa 150 MB với phiên bản MSGraphProtocol beta)

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ể triển khai 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 để giao tiếp với các API khác của Microsoft

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

Tài nguyên

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 đều mặc định là tài nguyên 'ME'. '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 trình tạo giao thức

Ghi chú. 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' được ghi đè thành trống vì phương thức xác thực đã cho biết 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ư dùng chung

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 tài nguyê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 các tài nguyên sau

  • 'tôi'. người dùng đã đồng ý. mặc định cho mọi giao thức. Bị ghi đè khi sử dụng phương thức xác thực "với danh tính của chính bạn" (Chỉ khả dụng trên auth_flow_type ủy quyền)
  • 'người sử dụng. người dùng @ tên miền. com'. hộp thư dùng chung 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ẽ được suy ra
  • 'Địa điểm. sharepoint-site-id'. id trang web sharepoint
  • 'tập đoàn. nhóm-site-id'. id nhóm office365

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ể chuyển nó như 'users/example@exampl. com'. Áp dụng tương tự cho các tiền tố tài nguyên khác

Loại 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 có thể chỉ làm việc với những phần 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

Cũng dễ dàng triển khai 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 điểm cuối và sử dụng kết nối để thực hiện 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 giao tiế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ả thư và thư mục email

Đây là các 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ôĐược bao gồm trong Trình trợ giúp phạm viMô tảMail. ReadmailboxĐể chỉ đọc hộp thư của tôiMail. Đọc. Sharedmailbox_sharedĐể chỉ đọc người dùng khác / hộp thư dùng chungMail. Sendmessage_send, message_allĐể chỉ gửi messageMail. Gửi. Sharedmessage_send_shared, message_all_sharedĐể chỉ gửi tin nhắn với tư cách người dùng khác / hộp thư chungMail. ReadWritemessage_allĐể đọc và lưu thư trong hộp thư của tôiMail. Đọc viết. Sharedmessage_all_sharedĐể đọc và lưu thư trong hộp thư chung/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 thư điện tử

Đạ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ể lấy 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 thức của nó

Tạo một tin nhắn nháp dễ dàng như thế nà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 ....
50

Làm việc với các email đã lưu cũng dễ 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 ....
51

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

@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 ....
52

Truy xuất tiêu đề thư

Bạn có thể truy xuất tiêu đề thư bằng cách thực hiện việc nà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 ....
53

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

Lưu dưới dạng 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"

    @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 ....
    54

  • Lưu tin nhắn đính kèm dưới dạng "eml"

    Chăm sóc đầy đủ. không có cách nào để xác định rằng tệp đính kèm thực tế là một tin nhắn. 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ì đó có thể là một tin nhắn (hoặc một sự kiện, v.v. ). Bạn sẽ phải tự xác định điều nà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 ....
    55

Sổ địa chỉ

Sổ Địa chỉ nhóm chức năng của cả Thư mục Liên hệ và Danh bạ. Nhóm phân phối Outlook không được hỗ trợ (Bởi API của Microsoft)

Đây là các 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 Trình trợ giúp phạm vi Mô tả Liên hệ. Đọc sổ địa chỉ Để chỉ đọc danh bạ cá nhân của tôi Danh bạ. Đọc. Sharedaddress_book_sharedĐể chỉ đọc danh bạ của người dùng khác / hộp thư chung Liên hệ. ReadWrite address book_allĐể đọc và lưu danh bạ cá nhân Danh bạ. Đọc viết. Sharedaddress_book_all_shared Để đọc và lưu danh bạ từ người dùng khác / hộp thư chung Người dùng. Đọc cơ bản. AllusersĐể chỉ đọc các thuộc tính cơ bản từ người dùng trong tổ chức của tôi (Người dùng. Đọc. Tất cả yêu cầu sự đồng ý của quản trị viên)

Thư mục liên hệ

Đại diện cho một Thư mục trong Phần Danh bạ của bạn trong Office 365. Lớp Sổ địa chỉ đại diện cho thư mục mẹ (chính nó là một thư mục)

Bạn có thể lấy 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

@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 ....
56

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

Office 365 API (Hoặc MS Graph API) không có khái niệm như Danh sách địa chỉ toàn cầu của 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 mình

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

  • Quyền cơ bản cần thiết là Người dùng. Đọc cơ bản. Tất cả (giới hạn thông tin)
  • Toàn quyền là Người dùng. Đọc. 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)

@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 ....
57

Để truy xuất một liên hệ qua email của họ

@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 ....
58

Liên lạc

Mọi thứ được trả về từ 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

# ...
12 là 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

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

Tạo một liên hệ từ 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

# ...
12

@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 ....
59

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

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

Theo mặc định, một phiên bản Người dùng chứa 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

@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 ....
90

Lịch

Chức năng lịch và sự kiện đượ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

# ...
17

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

# ...
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 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

# ...
19

Đây là các 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 Trình trợ giúp phạm vi Mô tả Lịch. ReadcalendarĐể chỉ đọc lịch cá nhân của tôi. Đọc. Sharedcalendar_sharedĐể chỉ đọc lịch của người dùng khác / hộp thư dùng chung Lịch. ReadWritecalendar_allĐể đọc và lưu lịch cá nhân Lịch. Đọc viết. Sharedcalendar_shared_allĐể đọc và lưu lịch từ người dùng khác/hộp thư chung

Làm việc với 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

# ...
17

@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 ....
91

Làm việc với các 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

# ...
19

@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 ....
92

Ghi chú về Lịch và Sự kiện

  1. Bao gồm_recurring=True

    Điều quan trọng cần biết là khi truy vấn các sự kiện bằ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
    
    # ...
    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 đó sẽ KHÔNG lọc dữ liệu dựa trên các thao tác bạn đặt trên truy vấn (lớn hơn_bằng, ít hơn, v.v. ) nhưng chỉ lọc ngày giờ bắt đầu sự kiện giữa ngày giờ bắt đầu và ngày kết thúc được cung cấp

  2. Lịch được chia sẻ

    Có một số sự cố đã biết khi làm việc với lịch dùng chung trong Microsoft Graph

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

    Vì một số lý do không xác định, microsoft không cho phép tải lên 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 thư). Xem cái này. Vì vậy, để tải tệp đính kèm lên Sự kiện, trước tiên hãy lưu sự kiện, sau đó đính kèm thư và lưu lại

nhiệm vụ

Chức năng nhiệm 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

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

# ...
27 có thể liệt kê và tạo các thư mục tác 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, hãy sử dụng 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

# ...
11

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

Phạm vi thô Bao gồm trong Trình trợ giúp phạm vi Mô tảTác vụ. Readt taskĐể chỉ đọc các nhiệm vụ cá nhân của tôiTasks. ReadWritetasks_allĐể đọc và lưu lịch cá nhân

Làm việc với 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

# ...
27

@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 ....
93

Làm việc với các 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

# ...
11

@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 ....
94

Một ổ đĩa

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

# ...
35 xử lý tất cả các chức năng xung quanh Bộ lưu trữ Thư viện Tài liệu và Ổ đĩa trong SharePoint

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

# ...
35 cho phép truy xuất 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

# ...
37 xử lý tất cả Tệp và Thư mục từ bên trong
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

# ...
35 đã chọn. Thông thường bạn sẽ chỉ cần làm việc với ổ đĩa mặc định. Nhưng các 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

# ...
35 có thể xử lý nhiều ổ đĩa

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

# ...
37 sẽ cho phép bạn làm việc với Thư mục và Tệp

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

Phạm vi thôĐược bao gồm trong Trình trợ giúp phạm viTệp mô tả. Đọc Để chỉ đọc tệp của tôi Tệp. Đọc. AllonedriveĐể chỉ đọc tất cả các tệp mà người dùng có quyền truy cậpFiles. Đọc Viết Để đọc và lưu tệp của tôi Tệp. Đọc viết. Allonedrive_allĐể đọc và lưu tất cả các tệp mà người dùng có quyền truy cậ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 ....
95

Cả Tệp và Thư mục đều là DriveItems. Cả Hình ảnh và Ảnh đều là Tệp, nhưng Ảnh cũng là Hình ảnh. Tất cả đều có một số phương thức và thuộc tính khác nhau. Hãy cẩn thận khi sử dụng 'is_xxxx'

Khi sao chép một DriveItem, api có thể trả về một bản sao trực tiếp của mục đó hoặc một con trỏ tới tài nguyên sẽ thông báo về tiến trình của thao tác sao ché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 ....
96

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

@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 ....
97

Bạn cũng có thể

@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 ....
98

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, trang tính, bảng và thậm chí cả dữ liệu ô. Bạn cũng có thể ghi vào 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 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

# ...
44 bằ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

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

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

@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 ....
99

Phiên sổ làm việc

Khi tương tác với excel, bạn có thể sử dụng phiên sổ 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 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 phiên liên tục, phiên mới sẽ tự động được tạo khi 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

# ...
46

@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 ....
40

đố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

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

  • Bảng công việc
  • Phạm vi và NamedRange
  • Bảng, TableColumn và TableRow
  • RangeFormat (để định dạng phạm vi)
  • Biểu đồ (hiện chưa có)

Vài ví dụ

Đặt định dạng cho một phạm vi nhất đị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 ....
41

Cột tự động điều chỉ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 ....
42

Nhận các giá trị từ Bả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 ....
43

Điểm chia sẻ

SharePoint api đã xong nhưng chưa có tài liệu. Nhìn vào điểm chia sẻ. py để có thông tin chi tiết

Đây là các 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

# ...
48 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

# ...
49

Phạm vi thô Bao gồm trong Trình trợ giúp phạm vi Mô tả Trang web. Đọc. AllsharepointĐể chỉ đọc các trang web, danh sách và mụcTrang web. Đọc viết. Allsharepoint_dlĐể đọc và lưu các trang web, danh sách và mục

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

Api planner đã hoàn thành nhưng chưa có tài liệu nào. Nhìn vào kế hoạch. py để có thông tin chi tiết

Chức năng lập kế hoạch yêu cầu Quyền của quản trị viên

Danh mục Outlook

Bạn có thể truy xuất, cập nhật, tạo và xóa các danh mục triển vọng. 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à các 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

# ...
48 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

# ...
49

Phạm vi thôĐược bao gồm trong Trình trợ giúp phạm viMô tảMailboxSettings. Đọc-Để chỉ đọc cài đặt triển vọng MailboxSettings. ReadWritesettings_allĐể đọc và ghi cài đặt triển vọng

Thí dụ

@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 ....
44

công dụng

phân trang

Khi sử dụng một số phương thức nhất định, có thể bạn yêu cầu nhiều mục hơn số lượng mà api có thể trả về trong một lệnh gọi api. Trong trường hợp này, Api, trả về url "liên kết tiếp theo" nơi bạn có thể lấy thêm dữ liệu

Khi trường hợp này xảy ra, các phương thức trong thư viện này sẽ trả về 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

# ...
52 trừu tượng hóa tất cả những điều này thành một trình vòng lặp duy nhất. Đối tượng phân trang sẽ yêu cầu "liên kết tiếp theo" ngay khi cần

Ví dụ

@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 ....
45

Khi sử dụng một số phương pháp nhất định, bạn sẽ có tùy chọn chỉ định không chỉ tùy chọn giới hạn (số lượng mục được trả lại) mà còn tùy chọn hàng loạt. Tùy chọn này sẽ chỉ ra phương thức yêu cầu dữ liệu tới api theo lô cho đến khi đạt đến giới hạn hoặc dữ liệu được tiêu thụ. Điều này hữu ích khi bạn muốn tối ưu hóa bộ nhớ hoặc độ trễ của mạng

Ví dụ

@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 ....
46

Trình trợ giúp truy vấn

Khi sử dụng API Office 365, bạn có thể lọc, sắp xếp, chọn, mở rộng hoặc tìm kiếm trên một số trường. Quá trình lọc này tẻ nhạt như đang sử dụng Giao thức dữ liệu mở (OData)

Mỗ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

# ...
08 (chẳng hạn như
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) triển khai một phương thức new_query sẽ trả về một 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

# ...
55. 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

# ...
55 này có thể xử lý việc lọc, sắp xếp, chọn, mở rộng và tìm kiếm rất dễ dàng

Ví dụ

@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 ....
47

Bạn cũng có thể chỉ định dữ liệu cụ thể sẽ được truy xuất bằng "chọ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 ....
48

Bạn cũng có thể tìm kiếm nội dung. Như đã nói trong tài liệu đồ thị

Hiện tại, bạn không thể chỉ tìm kiếm bộ sưu tập người và tin nhắn. Yêu cầu $search trả về tối đa 250 kết quả. Bạn không thể sử dụng $filter hoặc $orderby trong yêu cầu tìm kiếm

Nếu bạn thực hiện tìm kiếm trên thư và chỉ chỉ định một giá trị mà không có thuộc tính thư cụ thể, thì quá trình tìm kiếm sẽ được thực hiện trên các thuộc tính tìm kiếm mặc định của từ, chủ đề và nội dung

@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 ....
49

Yêu cầu xử lý lỗi

Bất cứ khi nào xảy ra lỗi Yêu cầu, đối tượng kết nối sẽ đưa ra một ngoại lệ. Sau đó, ngoại lệ sẽ được ghi lại và ghi vào thiết bị xuất chuẩn với thông báo của nó, trả về Sai (Không, Sai, [], v.v. )

HttpErrors 4xx (Yêu cầu không hợp lệ) và 5xx (Lỗi máy chủ nội bộ) được coi là ngoại lệ và cũng do kết nối gây ra. Bạn có thể yêu cầu

@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 không tăng lỗi http bằng cách chuyể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

# ...
58 (mặc định là True)

Tôi có thể gửi email từ 365 không?

Chọn Trang chủ > Email mới. Thêm người nhận, chủ đề và thư trong nội dung email. Chọn Gửi .

Bạn có thể gửi email bằng Python không?

Python cung cấp ` thư viện để gửi email- “SMTP lib” . “smtplib” tạo đối tượng phiên máy khách Giao thức chuyển thư đơn giản được sử dụng để gửi email đến bất kỳ id email hợp lệ nào trên internet.