2016-02-22 91 views
0

我正在嘗試調用Azure ARM Rest API來創建資源組。我傳遞tenant_id,client_id和client_secret以獲取將在以後用作授權標頭的訪問令牌。我的代碼如下所示。應用程序ID是應用程序外的客戶端ID,應用程序密鑰是選擇持續時間後生成的密鑰。Azure Active Directory錯誤。訪問令牌來自錯誤的發佈者

import adal 
import requests 
token_response = adal.acquire_token_with_client_credentials(
    'https://login.microsoftonline.com/' + '<tenantId>', 
    '<ApplicationId>', 
    '<Application Secret>' 
) 
access_token = token_response.get('accessToken') 

endpoint = 'https://management.azure.com/subscriptions/xxxx/resourcegroups/resourcename?api-version=2015-01-01' 

headers = {"Authorization": 'Bearer ' + access_token} 
json_output = requests.put(endpoint,headers=headers).json() 
print json_output 

但這扔我一個錯誤如下

{u'error': {u'message': u"The access token is from the wrong issuer 'https://sts 
.windows.net/xxx/'. It must match the tenant 'h 
ttps://sts.windows.net/xxx/' associated with th 
is subscription. Please use the authority (URL) 'https://login.windows.net/xxx' to get the token. Note, if the subscription is 
transferred to another tenant there is no impact to the services, but informatio 
n about new tenant could take time to propagate (up to an hour). If you just tra 
nsferred your subscription and see this error message, please try back later.", 
u'code': u'InvalidAuthenticationTokenTenant'}} 

這個錯誤是什麼意思和我在傳遞正確的憑證。如果我使用錯誤中提到的證書,我會得到另一個錯誤,它說沒有找到提及的client_id的應用程序。

+0

什麼是你傳遞的 「TenantId」 的價值? –

+0

我傳遞這是OAuth 2.0用戶授權端點,這是後弦https://login.windows.net – shwetha

+0

租戶ID要麼是一個GUID或類似的東西'somevalue.onmicrosoft.com'(基本上是你的天青AD名稱)。你在提供嗎? –

回答

0

由於有消息稱,你需要去對抗login.windows.net,而不是login.microsoftonline.com

token_response = adal.acquire_token_with_client_credentials(
'https://login.windows.net/' + '<tenantId>', 
'<ApplicationId>', 
'<Application Secret>' 
+2

我認爲這不重要,因爲'login.windows.net'會重定向到'login.microsoftonline.com'。請參閱此帖:https://blogs.technet.microsoft.com/ad/2015/03/06/simplifying-our-azure-ad-authentication-flows/。 –

+0

@BenV,你我仍然有同樣的錯誤。 – shwetha

0

似乎有一些問題,你的AD應用程序。要驗證Azure ARM,您需要一個帶有服務主體的AD。 您可以參考Create Active Directory application and service principal using portalAuthenticating a service principal with Azure Resource Manager來創建一個新的AD應用程序。在你的代碼中使用這些信息,然後重試。

+0

您的回答並未解釋某些應用程序(如Azure PowerShell和Visual Studio Cloud Explorer)是否能夠在我的目錄中沒有Service Principal的情況下訪問Azure資源。 – Dai

0

在客戶端憑證在代碼中使用

"https://management.core.windows.net/" 

,而不是https://login.microsoftonline.com/

token_response = adal.acquire_token_with_client_credentials( 'https://management.core.windows.net/' + '<tenantId>', '<ApplicationId>', '<Application Secret>'

我固定與此相同的問題。

感謝, 普山

相關問題