【问题标题】:GCM Client Based Device Group Management基于 GCM 客户端的设备组管理
【发布时间】:2015-12-16 03:49:14
【问题描述】:

我在将 GCM 注册 ID 添加到我的 Android 应用中的设备组客户端时遇到问题。我已按照https://developers.google.com/cloud-messaging/android/client-device-group 的所有说明进行操作,但不断收到 401 HTTP 响应。我找到了以下帖子,但没有人有答案...

我已成功从 GoogleSignInApi 和 Google 说明中提供的方法获取身份验证令牌,但都返回 401 响应。我已确保在我的 Google 开发者控制台中将客户端 ID 用于 Web 应用程序,但仍然没有运气。这是我的代码 sn-p...

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();

我认为这个 HTTP 发布请求并不重要,但我还确保正确的项目和客户端 ID (android) 存储在我的 google-services.json 中。有没有人成功管理设备组客户端?如果是这样,我的代码与您的代码有何不同?

【问题讨论】:

标签: android google-cloud-messaging


【解决方案1】:

我不确定是否可以在没有服务器 API 密钥的情况下执行此操作。 401 错误表明,如果该 URL 用于设备组设置,则应包含某种 HTTP 授权标头。

我最好的建议是使用客户端密钥库机制 (http://developer.android.com/training/articles/keystore.html) 很好地隐藏服务器 API 密钥。

有关如何使用 SERVER_API 密钥完成整个操作的详细信息,请参见此处:Google Cloud Messaging (GCM) with local device groups on Android gives HTTP Error code 401

希望这会有所帮助。 :-)

【讨论】:

  • 我什至在客户端尝试使用 api 密钥,但响应始终是来自服务器的 401。设备组的客户端似乎完全崩溃了。
猜你喜欢
  • 1970-01-01
  • 2014-11-13
  • 1970-01-01
  • 1970-01-01
  • 2020-07-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-30
  • 1970-01-01
相关资源
最近更新 更多