gpt4 book ai didi

c# - 如何通过 .Net 使用 Azure Databricks 进行身份验证?

转载 作者:行者123 更新时间:2023-12-03 04:17:37 24 4
gpt4 key购买 nike

我正在尝试弄清楚如何将 HTTP 请求发送到我的应用程序的 Azure Databricks。目前我陷入了身份验证;每个请求都会返回 401 Unauthorzied 错误。

我按照他们的指南创建了个人访问 token 并检索了其 key 。这是我的代码,它将发送创建集群的请求:

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 获取尸体

{
"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();

测试结果

enter image description here

更新:

添加Base64Encode函数代码。

private static string Base64Encode(string plainText)
{
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
return Convert.ToBase64String(plainTextBytes);
}

关于c# - 如何通过 .Net 使用 Azure Databricks 进行身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51775509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com