GCM未授权错误401

5

我无法从GCM获取响应,即使我提供了谷歌生成的正确的服务器API密钥。

问题:我想从我的Sails应用程序使用GCM进行推送通知。 我正在将下面的代码发布到“https://android.googleapis.com/gcm/send

代码:1

{
  "headers":{
    "Content-Type":"application/json",
    "Authorization":"key=xxxxxxxxxxxxxxxxxxxx"
  },
 "notification":{
   "title":"Hello Notify",
   "text":"Notification By Sails"
 },
 "registration_ids":["xxxxxxxxxxxx","xxxxxxxxxxxx","xxxxxxxxxxxx"]
}

代码:2

{
  "headers":{
    "Content-Type":"application/json",
    "Authorization":"key=xxxxxxxxxxxxxxxxxxxx"
  },
 "body":{
   "notification":{
      "title":"Hello Notify",
      "text":"Notification By Sails"
    }
  },
 "registration_ids":["xxxxxxxxxxxx","xxxxxxxxxxxx","xxxxxxxxxxxx"]
}

我得到的响应如下。
"statusCode": 401,
"body": "<HTML>\n
           <HEAD>\n<TITLE>Unauthorized</TITLE>\n</HEAD>\n
           <BODY BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\">\n
               <H1>Unauthorized</H1>\n
               <H2>Error 401</H2>\n
           </BODY>\n
         </HTML>\n",
2个回答

2

应该使用HTTP标头而不是载荷中的元素作为标题。


这里的示例应更符合您想要实现的内容:http://code.runnable.com/U4kVi8yKO58ptG0l/%5Bandroid-api%5D-gcm-send-using-node-js - Mickaël Rémond
@DebuggerCoder curl -X POST --header "Content-Type:application/json" --header "Authorization:key=AIzaXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" "https://android.googleapis.com/gcm/send" --data-ascii '{"notification":{"body":"yyy"},"to":"XXXXXXXXXX:YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY-ZZZZZZZZZZZZZZZZZZZZ"}' - TheWonderBird
1
@DebuggerCoder 同时,请使用“body”属性而不是“text”。 - TheWonderBird

0

请确保您已在Cloud Console中启用了正确的API。如果使用服务器API密钥发送,则需要启用“适用于Android的Google云消息传递”。

A screenshot from Cloud Console


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接