I am trying to get the attachments from the beacons registered in my Google developer console project with API_KEY. As described in Proximity Beacon API, the request can be made by just using the API Key and sending the body in POST request. I have done all that shown below. 1) The method to make http request:
public String getAttachments(String beaconname, String account,String data) {
//beacon name = beacons/3!....
//acountname = emailused;
//data = json body
String token = null;
String SCOPE = "oauth2:https://www.googleapis.com/auth/userlocation.beacon.registry";
String result = null;
try {
token = GoogleAuthUtil.getToken(getActivity(), account, SCOPE);
Log.w("Token", token);
} catch (IOException e) {
e.printStackTrace();
} catch (GoogleAuthException e) {
e.printStackTrace();
}
BufferedReader bufferedReader = null;
HttpURLConnection httpURLConnection = null;
try {
URL url = new URL("https://proximitybeacon.googleapis.com/v1beta1/beaconinfo:getforobserved?key="+API_KEY);
Log.w("URL", url.toString());
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
httpURLConnection.setRequestProperty("Accept","application/json");
httpURLConnection.setRequestProperty("Authorization", "Bearer " + token);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(httpURLConnection.getOutputStream(),"UTF-8"));
writer.write(data);
writer.flush();
writer.close();
Log.w("data sent",data);
int resposnecode = httpURLConnection.getResponseCode();
String responsemessage = httpURLConnection.getResponseMessage();
Log.e("Response code", resposnecode + "");
Log.e("Response message",responsemessage);
bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(),"UTF-8"));
String line = null;
StringBuilder builder = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
builder.append(line);
}
result = builder.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpURLConnection.disconnect();
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
The body(data parameter used in above method) that I send with this post request is as :
{ "observations":[
{
"advertisedId":{
"type":"EDDYSTONE",
"id":"vv8QICkg/0QBAwASb9z/Ug=="
},
"timestampMs":"2018-11-20T10:31:02.972000000Z"
}],"namespacedTypes":[
"true-bla-020202/Bye"]}
I am getting Bad request with code 400 as response. The beacon is having two attachments in beacon dashboard with namespace mentioned in the POST body. I am trying this from days without any success.