带有 Android 本地设备组的 Google Cloud Messaging (GCM) 提供 HTTP 错误代码 401 [英] Google Cloud Messaging (GCM) with local device groups on Android gives HTTP Error code 401

查看:23
本文介绍了带有 Android 本地设备组的 Google Cloud Messaging (GCM) 提供 HTTP 错误代码 401的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让 Google Cloud Messaging (GCM) 与本地设备组一起在 Android 上工作,如 https://developers.google.com/cloud-messaging/android/client-device-group.

I am trying to get Google Cloud Messaging (GCM) to work on Android with local device groups as described on https://developers.google.com/cloud-messaging/android/client-device-group .

我已在网络控制台中启用以下服务 ( https://console.developers.google.com ):

I have enabled the following services in the web console ( https://console.developers.google.com ):

  • Android 版 Google 云消息传递
  • Google 云发布/订阅
  • Google+ API

我还创建了以下凭据:

  • API 密钥(具有指定的包名称和 SHA-1 证书密钥)
  • OAuth 2.0 客户端 ID(Web 应用程序)

我的 Android 代码如下(错误处理等已被剥离):

My Android code looks as follows (error handling etc. has been stripped):

String account = ACCOUNT_NAME; // E.g. "johndoe@google.com"
String scope = "audience:server:client_id:" + WEB_APPLICATION_CLIENT_ID;
String token = GoogleAuthUtil.getToken(context, account, scope);

// Token is successfully found here :-)

URL url = new URL("https://android.googleapis.com/gcm/notification");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("project_id", NUMERICAL_PROJECT_ID);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.connect();

JSONObject requestBody = new JSONObject();
requestBody.put("operation", "add");
requestBody.put("notification_key_name", "foobar");
requestBody.put("registration_ids", 
  new JSONArray(Arrays.asList(new String[]{"42", "44"})));
requestBody.put("id_token", token);

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

// connection.getResponseCode() is now 401

提交给 https://android.googleapis.com/gcm/notification 的 JSON 看起来像这样:

The submitted JSON to https://android.googleapis.com/gcm/notification looks something like this:

{
  "operation": "add",
  "notification_key_name": "foobar",
  "registration_ids": ["42","44"],
  "id_token": "[veeeeeery long string]"
}

回复内容为:

<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>

我做错了什么?

推荐答案

发现了诀窍:您正在使用谷歌帐户获取 id_token,您需要使用电子邮件作为 notification_key_name.因此,如果您使用的是 foo@gmail.com,则需要将此地址用作 notification_key_name.

Found the trick: you are using a google account to take the id_token, you need to use EXACTLY the email as notification_key_name. So if you are using foo@gmail.com, you need to use this address as notification_key_name.

这篇关于带有 Android 本地设备组的 Google Cloud Messaging (GCM) 提供 HTTP 错误代码 401的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆