Databricks Jobs REST API 调用不适用于 Powershell

Databricks Jobs REST API call does not work with Powershell

所以,这是在 Databricks here

中对 Jobs API 的 link 调用

使用请求在 Python 中一切正常。例如,创造就业机会和工作清单都有效

import requests
proxy= "http://127.0.0.1:8888"
access_token="tokenabc"

proxies = {
"https": proxy,
}


header_read = {
'Authorization': "Bearer " + access_token,
'Accept': "application/scim+json"
}

#list jobs
init_get=requests.get('https://databricksworkspaceid.azuredatabricks.net/api/2.0/jobs/list', headers=header_read, proxies=proxies)

#create job
init_post=requests.post('https://databricksworkspaceid.azuredatabricks.net/api/2.0/jobs/create', headers=header_read, proxies=proxies,verify=False,json=job_payload)

然而,奇怪的是,在 Powershell 中,只有工作创建有效,工作列表失败。

#this works
Invoke-RestMethod -Uri 'https://databricksworkspaceid.azuredatabricks.net/api/2.0/jobs/create' -Headers @{ 'Authorization' = "Bearer $bearertoken" } -Method Post -Body $content -ContentType  'application/json'

#this does not work
Invoke-WebRequest -Uri 'https://databricksworkspaceid.azuredatabricks.net/api/2.0/jobs/list' -Headers @{ 'Authorization' = "Bearer $bearertoken" } -Method Get

#RestMethod also does not work
Invoke-RestMethod -Uri 'https://databricksworkspaceid.azuredatabricks.net/api/2.0/jobs/list' -Headers @{ 'Authorization' = "Bearer $bearertoken" } -Method Get

我也试过在这些上设置内容类型,但没有任何帮助。 此外,显式设置代理 (fiddler) 也无济于事。

-proxy "http://127.0.0.1:8888"

但不应该也是代理,因为 post 方法有效。

我一直收到类似

的错误
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At line:22 char:5
+     Invoke-WebRequest -Uri 'https://databricksworkspaceid.azuredatabricks.net/ap ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

或者在 RestMethod 的情况下

Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.
At line:22 char:5
+     Invoke-RestMethod -Uri 'https://databricksworkspaceid.azuredatabricks.net/ap ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

如果在 Powershell 中一切都失败了,我本可以理解,但是 post 方法(创建工作)有效,所以不确定为什么连接会在 Get 请求而不是 [= 的情况下终止43=].

通过一些论坛posts,我也尝试了以下但无济于事-

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

有谁知道我在做什么wrong/missing?莫名其妙地看到在 python 中工作,但只是 Powershell 中的一部分。

所以,我终于找到了答案。基本上有两件事:

  1. 必须在到达列表端点之前执行此操作(奇怪的是,正如我所说 - 创建端点)有效

基本上允许 TLS、TLS1.1 和 TLS1.2

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12
  1. 只做第 1 点是行不通的。我还必须使用“提升的”powershell 会话。基本上 运行 包含步骤 1 的脚本,使用“运行 作为管理员”。