如何通过 .Net 对 Azure Databricks 进行身份验证?
How does one authenticate with Azure Databricks through .Net?
我想知道如何为我的应用程序向 Azure Databricks 发送 HTTP 请求。目前我陷入身份验证;每个请求 returns 一个 401 Unauthorzied 错误。
我按照他们的指南创建了一个个人访问令牌并检索了它的密钥。这是我的代码,它将发送创建集群的请求:
var values = new Dictionary<string, string>
{
{ "cluster_name", "hello" },
{ "spark_version", "4.0.x-scala2.11" },
{"machine","eastus2" },//,
{"login","token" },
{"password", "dapi75dcd6e05815ae7bc52cc873b1b0f55c" }
};
var content = new FormUrlEncodedContent(values);
Debug.WriteLine("started");
var response = client.PostAsync("https://eastus2.azuredatabricks.net/api/2.0/clusters/get", content).Result;
var responseString = response.Content.ReadAsStringAsync().Result;
Debug.WriteLine(responseString);
但这总是失败,我怀疑用户名和密码的键名是错误的,但我不知道如何找出正确的标签。
感谢您的帮助!
编辑:这是返回的内容
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
<title>Error 401 </title>
</head>
<body>
<h2>HTTP ERROR: 401</h2>
<p>Problem accessing /api/2.0/clusters/get. Reason:
<pre> Unauthorized</pre></p>
<hr />
</body>
</html>
根据Azure Databricks Rest API sample,我们可以知道我们需要请求授权header Authorization: Basic base64codestring
。
The token you got from above needs to be turned into a Base64 encoded string. The important thing to note is that you must ALSO include a prefix of "token:". So the full string to encode is something like "token:dapi56b...........d5"
我们可以从 create clusters API
中得到 body
{
"cluster_name": "my-cluster",
"spark_version": "4.0.x-scala2.11",
"node_type_id": "Standard_D3_v2",
"spark_conf": {
"spark.speculation": true
},
"num_workers": 25
}
以下是我的测试演示代码:
var url = "https://eastasia.xxxxxxx.net/api/2.0/clusters/create";
HttpClient client = new HttpClient();
var token = "token:dapi.......";
var body ="{\"cluster_name\": \"my-cluster\",\"spark_version\": \"4.0.x-scala2.11\",\"node_type_id\": \"Standard_D3_v2\",\"spark_conf\": { \"spark.speculation\": true},\"num_workers\": 25}"
client.DefaultRequestHeaders.Add("Authorization", "Basic "+ Base64Encode(token));
HttpContent content = new StringContent(body);
var response = client.PostAsync(url, content).Result;
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
Console.ReadKey();
测试结果
更新:
添加Base64Encode函数代码。
private static string Base64Encode(string plainText)
{
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
return Convert.ToBase64String(plainTextBytes);
}
我想知道如何为我的应用程序向 Azure Databricks 发送 HTTP 请求。目前我陷入身份验证;每个请求 returns 一个 401 Unauthorzied 错误。
我按照他们的指南创建了一个个人访问令牌并检索了它的密钥。这是我的代码,它将发送创建集群的请求:
var values = new Dictionary<string, string>
{
{ "cluster_name", "hello" },
{ "spark_version", "4.0.x-scala2.11" },
{"machine","eastus2" },//,
{"login","token" },
{"password", "dapi75dcd6e05815ae7bc52cc873b1b0f55c" }
};
var content = new FormUrlEncodedContent(values);
Debug.WriteLine("started");
var response = client.PostAsync("https://eastus2.azuredatabricks.net/api/2.0/clusters/get", content).Result;
var responseString = response.Content.ReadAsStringAsync().Result;
Debug.WriteLine(responseString);
但这总是失败,我怀疑用户名和密码的键名是错误的,但我不知道如何找出正确的标签。
感谢您的帮助!
编辑:这是返回的内容
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
<title>Error 401 </title>
</head>
<body>
<h2>HTTP ERROR: 401</h2>
<p>Problem accessing /api/2.0/clusters/get. Reason:
<pre> Unauthorized</pre></p>
<hr />
</body>
</html>
根据Azure Databricks Rest API sample,我们可以知道我们需要请求授权header Authorization: Basic base64codestring
。
The token you got from above needs to be turned into a Base64 encoded string. The important thing to note is that you must ALSO include a prefix of "token:". So the full string to encode is something like "token:dapi56b...........d5"
我们可以从 create clusters API
中得到 body{
"cluster_name": "my-cluster",
"spark_version": "4.0.x-scala2.11",
"node_type_id": "Standard_D3_v2",
"spark_conf": {
"spark.speculation": true
},
"num_workers": 25
}
以下是我的测试演示代码:
var url = "https://eastasia.xxxxxxx.net/api/2.0/clusters/create";
HttpClient client = new HttpClient();
var token = "token:dapi.......";
var body ="{\"cluster_name\": \"my-cluster\",\"spark_version\": \"4.0.x-scala2.11\",\"node_type_id\": \"Standard_D3_v2\",\"spark_conf\": { \"spark.speculation\": true},\"num_workers\": 25}"
client.DefaultRequestHeaders.Add("Authorization", "Basic "+ Base64Encode(token));
HttpContent content = new StringContent(body);
var response = client.PostAsync(url, content).Result;
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
Console.ReadKey();
测试结果
更新:
添加Base64Encode函数代码。
private static string Base64Encode(string plainText)
{
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
return Convert.ToBase64String(plainTextBytes);
}