如何更新到Thingspeak 401授权错误

时间:2017-03-10 13:27:26

标签: java json

我试图将JSON数据更新到Thingspeak频道,但我收到401错误授权错误。已发送" writekey"作为参数。错误是

  

{"状态":" 401""错误" {" ERROR_CODE":" error_auth_required&#34 ;, "消息":"需要授权","详细信息":"请提供正确的身份验证详细信息。"}}

`    try {
            List<NameValuePair> nvPairList = new ArrayList<NameValuePair>(); 
            NameValuePair nv5 = new BasicNameValuePair("writeApi_Key",writeApi_Key);
            nvPairList.add(nv5);
        HttpClient client = HttpClientBuilder.create().build();
        HttpPut put= new HttpPut(urlname);
        URI uri = null;
        try {
            uri = new URIBuilder(put.getURI()).addParameters(nvPairList).build();
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        put.setURI(uri);
        put.setHeader("writeApi_Key", writeApi_Key);
        put.setHeader(HTTP.CONTENT_TYPE, "application/json");
        put.setHeader("charset", "utf-8");
        put.setHeader("Connnection", "keep-alive");
        put.setHeader("Cache-Control", "no-cache");
        System.out.println("Url header of post:::"+put.toString());
        StringEntity entity = new StringEntity(entryobj.toString());
        put.setEntity(entity);
        System.out.println("Url header of post:::"+put.toString());

        HttpResponse response = client.execute(put);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
          String line = "";
          while ((line = rd.readLine()) != null) {
        System.out.println(line);
          }

            int statusCode = response.getStatusLine().getStatusCode();

            if (statusCode != 200) {
                System.out.println("connection refused");
            } else if (response.getStatusLine().equals("0")) {

                System.out.println("Update Failed");
            }
            HttpEntity responseentity = response.getEntity();
            String responseString = EntityUtils.toString(responseentity, "UTF-8");
            System.out.println(responseString);
        } catch (ClientProtocolException cpe) {

            cpe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }    `

输出url是当writeApi_Key和api_key分别检查输出低于

  
      
  1. 帖子的网址标题::: PUT https://api.thingspeak.com/channels/230391.json?writeApi_Key=UEDXXXXXXXXXXXXX HTTP / 1.1
  2.   
  3. 帖子的网址标题::: PUT https://api.thingspeak.com/channels/230391.json?api_key+=VG2XXXXXXXXXXXXX HTTP / 1.1
  4.   

请找一个可以抛光的人..非常感谢你......

1 个答案:

答案 0 :(得分:0)

检查以确保您的API密钥正确无误。许多有此问题的人使用零('0')而不是字母'O',反之亦然。 '1'和'l'也是一个问题。