![](/img/trans.png)
[英]Ruby on Rails 4: How to authenticate to Azure AD from Rails Application?
[英]Getting 'Service_InternalServerError' when creating application through Graph API in Azure AD
我正在尝试以编程方式在 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>,
}
之后我通过波纹管请求获得身份验证令牌:
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请求形成'body'时要小心。 我在 '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',
}
这是有用的博客链接:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.