1

I'm having issues adding GCM registration ID's to a device group client side in my Android app. I've followed all the instructions from https://developers.google.com/cloud-messaging/android/client-device-group but keep getting a 401 HTTP response. I've found the following posts but no one has an answer...

I'm successfully getting an auth token from GoogleSignInApi and the method provided in Google's instructions but both give back 401 responses. I've ensured that I'm using the client ID for a Web Application in my Google Developer Console and still no luck. Here is my code snippet...

URL url = new URL("https://android.googleapis.com/gcm/googlenotification");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);


// HTTP request header
con.setRequestProperty("project_id", getString(R.string.gcm_defaultSenderId));
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
con.setRequestMethod("POST");
 con.connect();

String accountName = getAccount();


//Initialize the scope using the client ID you got from the Console.
final String scope = "audience:server:client_id:"
                    + "MY_WEB_APP_CLIENT_ID";
String idToken = "";
try {
    idToken = GoogleAuthUtil.getToken(this, sharedPref.getString("googleEmail", ""), scope);
} catch (Exception e) {
    e.printStackTrace();
}

// HTTP request
JSONObject data = new JSONObject();
data.put("operation", "add");
data.put("notification_key_name", "my_group_name");
data.put("registration_ids", new JSONArray(Arrays.asList(registrationId)));
data.put("id_token", idToken);

OutputStream os = con.getOutputStream();
os.write(data.toString().getBytes("UTF-8"));
os.close();

int responseCode = con.getResponseCode();

I don't think it matters for this HTTP post request but I've also ensured the right project and client ID (android) are stored in my google-services.json. Has anyone had any success managing device groups client side? If so what's different in my code from yours?

Community
  • 1
  • 1
Leonardo Casale
  • 472
  • 5
  • 13
  • Try this official repo on the device group tap here: https://github.com/google/gcm/tree/master/samples/android/gcm-demo and grab some ideas – bjiang Nov 30 '15 at 23:30
  • Unfortunately the demo uses the server side approach where the API key is used. I want to avoid using that key on the client for security reasons. – Leonardo Casale Dec 02 '15 at 02:52
  • I've got the same problem, have you found a solution? – greywolf82 Dec 12 '15 at 10:42
  • Looks like notification_key_name needs to be the email of the user that gets your id_token, see [this answer](http://stackoverflow.com/a/34298106/3983628). – Andy Dec 16 '15 at 16:33
  • Possible duplicate of [Google Cloud Messaging (GCM) with local device groups on Android gives HTTP Error code 401](http://stackoverflow.com/questions/34098773/google-cloud-messaging-gcm-with-local-device-groups-on-android-gives-http-erro) – Andy Dec 16 '15 at 16:36

1 Answers1

0

I'm not sure that it is possible to do this without the server API key. The 401 error indicates that some sort of HTTP Authorization header should be included if that URL is used for device group setup.

My best suggestion is to keep the server API key well hidden using a client-side keystore mechanism (http://developer.android.com/training/articles/keystore.html).

For details on how do do the whole thing using the SERVER_API key, please see here: Google Cloud Messaging (GCM) with local device groups on Android gives HTTP Error code 401

Hope this helps. :-)

Community
  • 1
  • 1
Morten
  • 684
  • 1
  • 5
  • 15
  • I tried to use the api key even on the client but the response is always 401 from server. It seems the client side of device group is completely broken. – greywolf82 Dec 12 '15 at 10:44