久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

微軟圖形身份驗證

Microsoft Graph Authentication(微軟圖形身份驗證)
本文介紹了微軟圖形身份驗證的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在用 Python 構建一個可以從 Azure AD 檢索數據的應用程序.此數據可能需要應用程序權限或委托權限.我成功檢索了只需要應用程序權限的數據.但是,為了檢索需要委托權限的數據,我正在嘗試使用 OAuth2.是否可以使用 OAuth2 通過 Microsoft Graph 進行身份驗證,但不讓用戶使用網頁登錄,而是通過 Python 腳本本身提供用戶憑據?

I’m building an application in Python which can retrieve data from Azure AD. This data can require either Application permissions or Delegated permissions. I had a success retrieving data which needs only Application permissions. However, in order to retrieve data which needs delegated permission, I am trying to use OAuth2. Is it possible to get authenticated with Microsoft Graph using OAuth2 but not having the user sign in using the web page, but instead supplying the user credentials through the Python script itself?

注意:我想使用 Microsoft Graph API(v1.0 和 beta)而不是 Azure AD Graph API.

Note: I want to use Microsoft Graph API (v1.0 and beta) and not Azure AD Graph API.

推薦答案

假設您已注冊并配置(api 權限)您的 azure 應用程序,并且您已復制應用程序client id"和client secret",您可以定義一個類舉行你的會議.以下代碼適用于我的應用:

Assuming you have registered and configured (api permissions) your azure app and you have copied the apps "client id" and "client secret" you can define a class that holds your session. The following code works for my app:

import json
import requests
from requests_oauthlib import OAuth2Session
from oauthlib.oauth2 import BackendApplicationClient


class SharepointSession(object):
    """ Base Class without credentials, use real credentials in derived Classes
    or instances
    """
    api_uri = "https://graph.microsoft.com"
    api_version = "v1.0"
    scope = ["https://graph.microsoft.com/.default"]
    directory_id = ""  # - tenant id
    token_url = "https://login.microsoftonline.com/{}/oauth2/v2.0/token"
    sites_url = "{}/{}/sites".format(api_uri, api_version)
    site = document_name = app_name = client_id = client_secret = ""
    site_id = None
    doc_id = None

    def __init__(self):
        """  """

    def getTokenizedSession(self):
        """
        OAuth2 to get access token
        First set up a backend client, mind to set grant_type
        build a OAuth2 Session with the client
        get access token

        Mind: python 3.x oauthlib requires scope params on more calls than py 2.x
        """
        client = BackendApplicationClient(
            client_id=self.client_id, scope=self.scope, grant_type="client_credentials")

        session = OAuth2Session(client=client, scope=self.scope)
        # fill access token
        token = session.fetch_token(token_url=self.token_url.format(self.directory_id),
                                    client_id=self.client_id,
                                    scope=self.scope,
                                    client_secret=self.client_secret)
        self.session = session
        self.token = token
        return session, token

    def getSiteId(self):
        # get the site id
        ae = "{}/myonline.sharepoint.com:/sites/{}:".format(
            self.sites_url, self.site)
        rt = self.session.get(ae)
        response = json.loads(rt.text)
        self.site_id = response.get("id")
        return self.site_id

    def someOtherMethod(self):
        """         ...             """

現在您可以使用從您的 azure 應用注冊復制的憑據實例化會話類,即目錄 ID"(與租戶 ID 相同)、客戶端 ID"和客戶端密碼"像這樣:

Now you can instantiate the session class with the credentials copied from your azure app registration i.e. "directory id" (same as tenant id), "client id" and "client secret" like this:

mysp_session = SharepointSession()
mysp_session.directory_id = "XXXXXXXX-XXXX-YYYY-ZZZZ-XXXXXXXXX"
mysp_session.site = "MySitename"
mysp_session.document_name = "Testlist"
mysp_session.client_id = r"xxxxxxxxxxxxxxxxxxxxxxx"
mysp_session.client_secret = r"xxxxxxxxxxxxxxxxxxxxxxx"

# connect 
session, token = mysp_session.getTokenizedSession()

# do your business logic
mysp_session.getSiteId()
....
mysp_session.someOtherMethod()

希望對你有幫助

這篇關于微軟圖形身份驗證的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How should I verify a log message when testing Python code under nose?(在鼻子下測試 Python 代碼時,我應該如何驗證日志消息?)
Patch __call__ of a function(修補函數的 __call__)
How to call self in a mock method of an object in Python?(如何在 Python 中對象的模擬方法中調用 self?)
Mocking only a single method on an object(僅模擬對象上的單個方法)
Mocking a subprocess call in Python(在 Python 中模擬子進程調用)
Checking call order across multiple mocks(檢查多個模擬的調用順序)
主站蜘蛛池模板: 亚洲综合视频 | 国产一区二区三区在线 | 成人亚洲精品 | 亚洲最大av网站 | 999在线精品 | 国产午夜精品视频 | 亚洲免费三区 | 精品国产视频 | 国产精品久久久久久中文字 | 日韩精品999 | 亚洲视频三区 | 一区二区在线不卡 | 精品久久香蕉国产线看观看亚洲 | 亚洲成人中文字幕 | 中文字幕亚洲欧美 | 中文字幕亚洲一区 | 一级免费看 | 免费看国产一级特黄aaaa大片 | 一级一片在线观看 | 中文字幕三区 | 久久伊 | h视频免费在线观看 | 国产精品资源在线 | 免费看国产a | www.成人.com | 欧美视频精品 | 久久www免费视频 | 国产ts人妖一区二区三区 | 国产一区 在线视频 | 成年人免费看 | 精品国产乱码一区二区三 | 日本国产精品视频 | 国产精品一区二区三区四区 | 亚洲毛片在线观看 | 久久最新网址 | 亚洲一区二区三区在线播放 | 日本字幕在线观看 | 日日夜精品视频 | 色888www视频在线观看 | 国产 日韩 欧美 在线 | 欧美视频在线看 |