1

我試圖以編程方式在Azure AD中創建應用程序。 我在管理門戶中添加了初始應用程序,並授予了圖形API和活動目錄(兩個目錄都是讀/寫)的權限。通過Azure AD中的圖形API創建應用程序時,出現'Service_InternalServerError'

首先,我獲得授權碼,樣本URL形成如下:

uri = Addressable::URI.parse('https://login.microsoftonline.com/common/oauth2/authorize') 
    uri.query_values = { 
    response_type: 'code', 
    response_mode: 'form_post', 
    client_id: <Application ID>, 
    redirect_uri: <Redirect URI>, 
    resource: 'https://graph.windows.net/', 
    state: <UUID>, 
    } 

後來我打通波紋管的請求的身份驗證令牌:

client = OAuth2::Client.new(client_id, 
          client_secret, 
          :site => 'https://login.microsoftonline.com', 
          :authorize_url => '/common/oauth2/authorize', 
          :token_url => '/common/oauth2/token') 

auth_token = client.auth_code.get_token(auth_code, 
           :redirect_uri => redirect_uri, 
           :scope => 'openid' 

最後,我可以讓圖形API調用應用程序端點添加一個應用程序:

graph_url = 'https://graph.windows.net/<TENANT ID>/applications?api-version=1.6' 
body = { 
    'identifierUris' => ['<URI>'], 
    'availableToOtherTenants' => true, 
    'homepage' => <Home PAGE>, 
    'replyURLs' => <SOME REPLY URL>, 
    'displayName' => <APP DISPLAY NAME>, 
} 
headers = {'Content-Type' => 'application/json','Authorization' => "Bearer #{auth_token.token}"} 
conn = Faraday.new(graph_url, {headers: headers}) 
res = conn.post graph_url, body.to_json 

作爲迴應我得到波紋管錯誤whic h是不是很具描述性,不知道有什麼問題:

{"odata.error"=>{"code"=>"Service_InternalServerError", "message"=>{"lang"=>"en", "value"=>"Encountered an internal server error."}}} 

任何建議表示讚賞。

回答

1

我解決了我的問題,嘗試創建應用時出現內部錯誤。 爲http請求形成'body'時要小心。我在'replyUrls'屬性的url末尾添加了額外的'/'。同時爲每個屬性添加數據類型也很重要。這裏是爲我工作的樣品要求:

body = { 
    "odata.type" => "Microsoft.DirectoryServices.Application", 
    "identifierUris" => ["https://mynamehere.com/#{someidentifier}"], 
    "[email protected]" => "Collection(Edm.String)", 
    "availableToOtherTenants" => true, 
    "homepage" => 'https://myhomepage.com', 
    "replyURLs" => ["http://localhost:3000/someUrl"], 
    "[email protected]" => "Collection(Edm.String)", 
    "displayName" => 'This is my app', 
} 

這裏是一個博客,是有幫助的鏈接:

http://blog.mszcool.com/index.php/2016/06/a-deep-dive-into-azure-ad-multi-tenant-apps-oauthopenidconnect-flows-admin-consent-and-azure-ad-graph-api/

+0

很高興你能解決這個問題。過去我發現,避免類似問題的一種非常簡單的方法是首先對類似對象執行「GET」,然後修改該對象以包含所需的新屬性,然後將其用於POST。例如,我將使用Azure Portal創建應用程序,然後使用該應用程序的框架在我的POST請求中創建新的應用程序。另一個技巧可能是使用Fiddler捕獲PowerShell的HTTP流量,或另一種可以使用圖形創建應用程序的服務,然後使用它來幫助創建您的身體。 –

+0

感謝您的提示,今後會牢記它! – natia

相關問題