0
我試圖更新JSON數據到Thingspeak頻道,但我得到401錯誤授權錯誤。已發送「writekey」作爲參數。錯誤是如何更新爲Thingspeak 401授權錯誤
{「status」:「401」,「error」:{「error_code」:「error_auth_required」,「message」:「需要授權」,「details」:「請提供適當的驗證細節。 「}}
` 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();
} `
輸出網址爲當writeApi_Key和API_KEY分別檢查輸出是下面的交
- 地址報頭::: PUT https://api.thingspeak.com/channels/230391.json?writeApi_Key=UEDXXXXXXXXXXXXX HTTP/1。崗位1
- URL標頭::: PUT https://api.thingspeak.com/channels/230391.json?api_key+=VG2XXXXXXXXXXXXX HTTP/1.1
請找一些一誰可以擺脫拋光..謝謝你這麼多..
是的你的權利選擇不同場景下的「api_key」和「writeapi_key」。選擇是問題,現在它正常工作,通過在nvValue對中插入單個字段進行驗證。但是當試圖以Json Object格式發送數據時是一個問題。 – ZeroOrigin