【问题标题】:Authentication Details Not Provided REST API POST Request未提供身份验证详细信息 REST API POST 请求
【发布时间】:2019-01-31 14:10:30
【问题描述】:

我正在向 API 服务器发帖,我收到以下回复:

Server Response: {"detail":"Authentication credentials were not provided."}

我正在用 Java 编写请求,但我不确定为什么它说没有提供凭据。

这是我的代码的 sn-p:

 hconn.setRequestProperty("Content-Type", "application/json");
 hconn.setRequestProperty( "Accept", "application/json" );
 if( urlconn instanceof HttpsURLConnection )
 {
    String encoded = new String(Base64.getEncoder().encode( ( username + ":" + password).getBytes()));
    String auth = "Basic " + encoded;
    urlconn.setRequestProperty("Encoded Authorization", auth );

 }

其中hconn 的类型为HttpURLConnection

这是你们需要的唯一相关的sn-p。有没有我想在这里设置的属性?

我知道服务器响应来自 Django 框架,但文档并不清楚发现需要什么来防止这种情况发生。

任何帮助表示赞赏。

【问题讨论】:

    标签: java django rest api


    【解决方案1】:

    Authorization 是正确的请求标头名称,而不是“编码授权”。所以,你应该设置

    urlConn.setRequestProperty("Authorization", auth);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-05
      • 2018-08-25
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      • 2020-12-21
      • 2016-12-07
      • 2014-10-07
      相关资源
      最近更新 更多