ruby - 在 Azure AD 中通过 Graph API 创建应用程序时获取 'Service_InternalServerError'

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

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

首先我获取授权码,示例网址格式如下:

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>,
  }

之后我通过以下请求获取身份验证 token :

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."}}}

如有任何建议,我们将不胜感激。

最佳答案

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

body = {
  "odata.type" => "Microsoft.DirectoryServices.Application",
  "identifierUris" => ["https://mynamehere.com/#{someidentifier}"],
  "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ee7eaebe0fae7e8e7ebfcdbfce7fdcee1eaeffaefa0faf7feeb" rel="noreferrer noopener nofollow">[email protected]</a>" => "Collection(Edm.String)",
  "availableToOtherTenants" => true,
  "homepage" => 'https://myhomepage.com',
  "replyURLs" => ["http://localhost:3000/someUrl"],
  "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6f1d0a1f03163a3d231c2f000b0e1b0e411b161f0a" rel="noreferrer noopener nofollow">[email protected]</a>" => "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/

关于ruby - 在 Azure AD 中通过 Graph API 创建应用程序时获取 'Service_InternalServerError',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41577568/

相关文章:

ruby - 我可以通过 GH API 获取两次提交之间一个文件的差异数据吗?

azure - azure web 应用程序上的用户权限问题(应用程序池 - 身份)

android - 在 android 中使用桌面 View 启动 chrome 浏览器应用程序

c++ - 从 native 应用程序访问 chrome 中的 DOM

java - 瓦提更换

mysql - gem mysql2 gem 未加载

Ruby:将两个相似的方法合并为一个?

ruby - 为什么 Ruby 同时具有 && 和 'and' 运算符?

python - 如何使用Python代码在Azure上创建虚拟机?

azure - 将 TFS 迁移到新服务器