【问题标题】:Getting 'Service_InternalServerError' when creating application through Graph API in Azure AD在 Azure AD 中通过 Graph API 创建应用程序时出现“Service_InternalServerError”
【发布时间】:2017-01-12 03:54:14
【问题描述】:

我正在尝试以编程方式在 Azure AD 中创建应用程序。 我在管理门户中添加了初始应用程序并授予了 Graph Api 和 Active Directory 的权限(目录读/写两者)。

首先我获取授权码,示例 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

作为回应,我收到以下错误,描述性不强,不确定出了什么问题:

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

欢迎提出任何建议。

【问题讨论】:

    标签: ruby azure automation azure-active-directory azure-ad-graph-api


    【解决方案1】:

    我解决了我的问题,在尝试创建 App 时出现内部错误。 为 http 请求形成“正文”时要小心。我在“replyUrls”属性的 url 末尾添加了额外的“/”。为每个属性添加数据类型也很重要。这是对我有用的示例请求:

    body = {
      "odata.type" => "Microsoft.DirectoryServices.Application",
      "identifierUris" => ["https://mynamehere.com/#{someidentifier}"],
      "identifierUris@odata.type" => "Collection(Edm.String)",
      "availableToOtherTenants" => true,
      "homepage" => 'https://myhomepage.com',
      "replyURLs" => ["http://localhost:3000/someUrl"],
      "replyURLs@odata.type" => "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/

    【讨论】:

    • 很高兴您能够解决此问题。我过去发现避免此类问题的一种非常简单的方法是首先对类似的对象执行“GET”,然后修改该对象以包含您想要的新属性,然后将其用于您的 POST。例如,我将使用 Azure 门户创建一个应用程序,然后使用该应用程序的框架在我的 POST 请求中创建新应用程序。另一个技巧可能是使用 Fiddler 来捕获 PowerShell 的 HTTP 流量,或者其他可以使用图创建应用程序的服务,然后使用它来帮助创建你的 body。
    • 感谢您的提示,以后会记住的!
    猜你喜欢
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    • 2019-07-15
    • 1970-01-01
    相关资源
    最近更新 更多