Google Cloud Messaging(GCM)与Android上的本地设备组提供HTTP Error code 401 [英] Google Cloud Messaging (GCM) with local device groups on Android gives HTTP Error code 401
问题描述
我试图让Google云消息传递(GCM)在本地设备组的Android上工作,如 https://developers.google.com/cloud-messaging/android/client-device-group 。
我已在Web控制台中启用了以下服务( https://console.developers.google.com ):
- Google云消息传递for Android
- Google云端发布/订阅
- Google+ API
我还创建了以下凭据: ul> 我的Android代码如下所示(错误处理等已被剥离): 提交的JSON https://android.googleapis.com/gcm/notification 看起来像这样: 响应内容为: 我做错了什么? 找到诀窍:您正在使用Google帐户来使用id_token,您需要将电子邮件作为notification_key_name使用。因此,如果您使用的是foo@gmail.com,则需要将此地址用作notification_key_name。 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 . I have enabled the following services in the web console ( https://console.developers.google.com ): I have also created the following credentials: My Android code looks as follows (error handling etc. has been stripped): The submitted JSON to https://android.googleapis.com/gcm/notification looks something like this: The response contents is: What am I doing wrong? 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. 这篇关于Google Cloud Messaging(GCM)与Android上的本地设备组提供HTTP Error code 401的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
字符串帐户= ACCOUNT_NAME; //例如johndoe@google.com
String scope =audience:server:client_id:+ WEB_APPLICATION_CLIENT_ID;
String token = GoogleAuthUtil.getToken(context,account,scope);
//在此处成功找到代币:-)
网址=新网址(https://android.googleapis.com/gcm/notification);
HttpURLConnection连接=(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()现在是401
{
operation:add,
notification_key_name:foobar,
registration_ids:[42,44],
id_token:[veeeeeery long string]
}
< HTML>
< HEAD>
< TITLE>未经授权的< / title>
< / HEAD>
< BODY BGCOLOR =#FFFFFFTEXT =#000000>
< H1>未经授权< / H1>
< H2>错误401< / H2>
< / BODY>
< / HTML>
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
{
"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>