GCM未授权错误401

时间:2015-07-24 05:15:51

标签: google-cloud-messaging sails.js

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

问题:我想在我的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 个答案:

答案 0 :(得分:2)

标头应该是H​​TTP标头,而不是有效负载中的元素。

答案 1 :(得分:0)

确保您已在Cloud Console中启用了正确的API。如果使用服务器API密钥发送,则需要启用“Google Cloud Messaging for Android”。

A screenshot from Cloud Console