我正在努力让Google Cloud Messaging(GCM)在Android上与本地设备组一起工作,如https://developers.google.com/cloud-messaging/android/client-device-group所述.
我在Web控制台(https://console.developers.google.com)中启用了以下服务:
>适用于AndroID的Google Cloud Messaging
> Google Cloud Pub / Sub
> Google Api
我还创建了以下凭据:
> API密钥(具有指定的包名称和SHA-1证书keyprint)
> OAuth 2.0客户端ID(Web应用程序)
我的AndroID代码如下所示(错误处理等已被删除):
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
提交的JsON到https://android.googleapis.com/gcm/notification看起来像这样:
{ "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.
总结以上是内存溢出为你收集整理的在Android上使用本地设备组的Google Cloud Messaging(GCM)提供HTTP错误代码401全部内容,希望文章能够帮你解决在Android上使用本地设备组的Google Cloud Messaging(GCM)提供HTTP错误代码401所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)