基于GCM客户端的设备组管理 [英] GCM Client Based Device Group Management
问题描述
我遇到的问题是,在我的Android应用中将GCM注册ID添加到设备组客户端。我遵循了 https://developers.google的所有说明。 com / cloud-messaging / android / client-device-group ,但不断收到401 HTTP响应。我发现了以下帖子,但没有人有答案...
- 获取通知密钥错误401 gcm https://android.googleapis.com/gcm/googlenotification
- Google云消息传递,返回401未经授权
- Google云消息传递,401从客户端创建通知密钥时返回401未授权。 如何成功在客户端上生成通知密钥使用GCM?
我成功从GoogleSignInApi获取授权令牌,并使用Google说明中提供的方法,回401回应。我确保我在Google Developer Console中使用Web应用程序的客户端ID,但仍然没有运气。这是我的代码段...
网址url =新网址(https://android.googleapis.com/gcm / googlenotification);
HttpURLConnection con =(HttpURLConnection)url.openConnection();
con.setDoOutput(true);
// HTTP请求头
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();
//使用您从控制台获取的客户端ID来初始化范围。
final String scope =audience:server:client_id:
+MY_WEB_APP_CLIENT_ID;
String idToken =;
尝试{
idToken = GoogleAuthUtil.getToken(this,sharedPref.getString(googleEmail,),scope);
} catch(Exception e){
e.printStackTrace();
}
// HTTP请求
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();
我不认为这个HTTP post请求很重要,但我也确保了正确的项目和客户端ID(android)存储在我的google-services.json中。有没有人管理设备组客户端的任何成功?如果是这样,我的代码与您的代码有什么不同?
我不确定没有服务器就可以做到这一点API密钥。 401错误表示如果该URL用于设备组设置,应该包含某种HTTP授权标头。
我的最佳建议是将服务器API密钥妥善保存使用客户端密钥存储机制隐藏( http://developer.android.com/training /articles/keystore.html )。
有关使用SERVER_API键执行整个操作的详细信息,请参阅:具有本地设备组的Google Cloud Messaging(GCM) Android给HTTP错误代码401
希望这会有所帮助。 : - )
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...
- get notification key error 401 gcm https://android.googleapis.com/gcm/googlenotification
- Google Cloud Messaging, returning 401 Unauthorized
- Google Cloud Messaging, 401 Unauthorized is returned when creating notification key from client
- How to successfully "Generate a Notification Key on the Client" with GCM?
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?
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. :-)
这篇关于基于GCM客户端的设备组管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!