AuthorizationCodeCredential 類別

藉由兌換先前從 Azure Active Directory 取得的授權碼來驗證。

如需驗證流程的詳細資訊,請參閱 Azure Active Directory 檔

繼承
azure.identity._internal.get_token_mixin.GetTokenMixin
AuthorizationCodeCredential

建構函式

AuthorizationCodeCredential(tenant_id: str, client_id: str, authorization_code: str, redirect_uri: str, **kwargs: Any)

參數

tenant_id
str
必要

應用程式的 Azure Active Directory 租使用者識別碼。 也稱為其 「目錄」識別碼。

client_id
str
必要

應用程式的用戶端識別碼

authorization_code
str
必要

來自使用者登入的授權碼

redirect_uri
str
必要

應用程式的重新導向 URI。 必須符合用來要求授權碼的 URI。

authority
str

Azure Active Directory 端點的授權單位,例如「login.microsoftonline.com」,Azure 公用雲端的授權單位 (,這是預設) 。 AzureAuthorityHosts 定義其他雲端的授權單位。

client_secret
str

其中一個應用程式的用戶端密碼。 僅適用于 Web 應用程式和 Web API。

additionally_allowed_tenants
List[str]

除了認證可能取得權杖的指定「tenant_id」之外,指定租使用者。 新增萬用字元值 「*」,以允許認證取得應用程式可存取之任何租使用者的權杖。

範例

建立 AuthorizationCodeCredential。


   from azure.identity import AuthorizationCodeCredential

   credential = AuthorizationCodeCredential(
       tenant_id="<tenant_id>",
       client_id="<client_id>",
       authorization_code="<auth_code>",
       redirect_uri="<redirect_uri>",
   )

方法

close

關閉認證的傳輸會話。

get_token

要求 範圍的存取權杖。

Azure SDK 用戶端會自動呼叫此方法。

第一次呼叫此方法時,認證會兌換其授權碼。 在後續呼叫時,如果認證在兌換授權碼時取得重新整理權杖,則會傳回快取的存取權杖或兌換重新整理權杖。

close

關閉認證的傳輸會話。

close() -> None

get_token

要求 範圍的存取權杖。

Azure SDK 用戶端會自動呼叫此方法。

第一次呼叫此方法時,認證會兌換其授權碼。 在後續呼叫時,如果認證在兌換授權碼時取得重新整理權杖,則會傳回快取的存取權杖或兌換重新整理權杖。

get_token(*scopes: str, claims: str | None = None, tenant_id: str | None = None, **kwargs: Any) -> AccessToken

參數

scopes
str
必要

存取權杖所需的範圍。 這個方法至少需要一個範圍。 如需範圍的詳細資訊,請參閱 https://learn.microsoft.com/azure/active-directory/develop/scopes-oidc

claims
str

權杖中所需的其他宣告,例如在授權失敗之後,在資源提供者的宣告挑戰中傳回的其他宣告。

tenant_id
str

要包含在權杖要求中的選擇性租使用者。

傳回

具有所需範圍的存取權杖。

傳回類型

例外狀況

驗證失敗。 錯誤的 message 屬性會提供原因。 來自 Azure Active Directory 的任何錯誤回應都可以當做錯誤的 response 屬性使用。