【问题标题】:ADLS Gen2 REST API giving error with status code 400ADLS Gen2 REST API 给出错误,状态码为 400
【发布时间】:2020-11-18 18:00:41
【问题描述】:

我正在浏览 Azure Data Lake Storage Gen2 的 REST API 列表,当我从 CLI/Postman 调用 api 时遇到问题。

我刚从简单的获取请求 path list api 开始。

但响应如下

{"error":{"code":"InvalidHeaderValue","message":"The value for one of the HTTP headers is not in the correct format.\nRequestId:ecb96bb0-501f-0030-2578-65cf12000000\nTime:2020-07-29T07:20:35.6240747Z"}}

搜索后找到create directory/file的示例代码并尝试创建目录但得到类似的响应

提出请求

curl -i -X PUT -H "x-ms-version: 2020-07-29" -H "content-length: 0" -H "Authorization: Bearer $ACCESS_TOKEN" "https://$AccountName.dfs.core.windows.net/mydata?resource=filesystem"

对上述请求的回应

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                  Dload  Upload   Total   Spent    Left  Speed
100   202  100   202    0     0    115      0  0:00:01  0:00:01 --:--:--   115HTTP/1.1 400 The value         
for one of the HTTP headers is not in the correct format.
Content-Length: 202
Content-Type: application/json;charset=utf-8
Server: Windows-Azure-HDFS/1.0 Microsoft-HTTPAPI/2.0
x-ms-request-id: 8d615c77-001f-0070-2b79-65c82a000000
Date: Wed, 29 Jul 2020 07:25:54 GMT

{"error":{"code":"InvalidHeaderValue","message":"The value for one of the HTTP headers is not in the correct format.\nRequestId:8d615c77-001f-0070-2b79-65c82a000000\nTime:2020-07-29T07:25:55.9048230Z"}}

对于身份验证,我使用https://login.microsoftonline.com/{{tenantID}}/oauth2/token?,然后使用下面的代码在测试部分捕获不记名令牌。

pm.test(pm.info.requestName, () => {
    pm.response.to.not.be.error;
    pm.response.to.not.have.jsonBody('error');
});
pm.globals.set("bearerToken", pm.response.json().access_token);

pm.test(pm.info.requestname, ()=>{
    pm.response.to.not.be.error;
    pm.response.to.not.have.jsonBody('error');
})
pm.globals.set('BearerToken',("Bearer ").concat(pm.response.json().access_token));

谁能告诉我哪里出了问题?



根据屏幕截图的要求更新问题。

在做了这么多试验后发现filesystem-list 我得到了响应,但是path-list 得到错误说AuthorizationPermissionMismatch403 状态码。

文件系统列表:

路径列表:

失败的标题

【问题讨论】:

  • 您的令牌的资源是什么?应该是https://storage.azure.com
  • IN 资源使用https://management.azure.com/ 并尝试了您的建议,但响应相同。
  • 您的资源应该是https://storage.azure.com。我之前说过这个吗?为什么是的,是的,我做到了
  • @HongOoi 尝试使用https://storage.azure.com 仍然是相同的响应。 {"error":{"code":"AuthorizationPermissionMismatch","message":"This request is not authorized to perform this operation using this permission.\nRequestId:8cb9734d-f01f-0090-19b8-6c4bb3000000\nTime:2020-08-07T12:44:57.8346082Z"}}
  • @HongOoi 告诉我它是如何为file system - list API 工作的?

标签: azure rest postman azure-storage azure-data-lake


【解决方案1】:

更新 0807:

假设您已经创建了一个Service principal。然后按照以下步骤操作:

第 1 步:在 azure 门户中,您的 ADLS Gen2 -> 单击“访问控制”-> 单击“添加”-> 单击“添加角色分配”-> 在“添加角色分配”弹出窗口中,选择“贡献者” " 代表角色;然后选择您的服务主体名称;最后,点击保存按钮。截图如下(注意,可能需要几分钟才能生效):

然后添加“Storage Blob Data Reader”角色:

第 2 步:在 postman 中,输入此 url 获取请求:https://login.microsoftonline.com/<tenant_id>/oauth2/token

然后在Body -> 中选择“form-data”,输入以下键和值:

grant_type:  client_credentials
client_id :  xxxxxxx
client_secret: xxxxxx
resource: https://storage.azure.com/

截图如下:

最后,您可以调用 ADLS Gen2 rest api(注意:x-ms-date 应设置为今天)。截图如下:


更新 0730:如何使用 SAS 令牌进行身份验证。

1.Nav to azure portal -> 你的“data Lake storage gen2 account” -> 然后在左侧窗格中,点击“Shared access signature” -> 然后选择适当的值 -> 最后,点击“Generate SAS”和连接字符串”按钮。请参考下面的截图:

2.打开邮递员,有一点需要注意:

1.对于SAS令牌,请替换第一个?和 &。然后将 SAS 令牌放在 url 的末尾。 2.如果你使用的是SAS token,那么你不需要在请求头中使用Authorization,请去掉它。

截图如下:

如果您还有其他问题,请告诉我。


原答案:

您的请求中有几个错误。

Path - List为例,x-ms-version应该是2018-11-09;您应该在请求标头中添加x-ms-date;请删除请求标头中的Content-Length

我在我身边测试它,它工作正常。测试结果如下:

如果您还有其他问题,请告诉我。

【讨论】:

  • 按照建议进行了更改,但仍然出现错误 { "error": { "code": "InvalidAuthenticationInfo", "message": "Server failed to authenticate the request. Please refer to the information in the www-authenticate header.\nRequestId:2ca97766-701f-0027-438d-656619000000\nTime:2020-07-29T09:49:04.0210844Z" } }
  • @BenBean,您能告诉我您是如何生成身份验证的吗?这是身份验证问题。
  • 如果您看到邮递员截图,在第一个文件中,我正在验证不记名令牌并将其保存在变量中。并在下一个请求(第二个文件)中传递变量。如果我将鼠标悬停在变量 {{bearerToken}} 上,它会显示值。
  • @BenBean,我的意思是你如何获得原始不记名令牌?
  • 如果邮递员做错了什么,那么为什么我在这里也没有得到任何回复https://docs.microsoft.com/en-us/rest/api/storageservices/datalakestoragegen2/path/list#code-try-0
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 2017-03-24
  • 2022-08-06
  • 1970-01-01
  • 2020-09-15
  • 2019-12-27
相关资源
最近更新 更多