简体   繁体   English

使用Google Apps脚本将HTTPPost发送到GCM时出现错误=缺少注册

[英]Getting Error=Missing Registration while sending HTTPPost to GCM Using Google Apps Script

I am trying to send http post to GCM server using google apps script. 我正在尝试使用Google Apps脚本将http发布发送到GCM服务器。 I have the device ID and the proper credentials. 我有设备ID和适当的凭据。 Here is the code I am using: 这是我正在使用的代码:

function doThat(){
  var url = "https://android.googleapis.com/gcm/send";
  var options = {
    "method": "post",
    "headers": {
      "Authorization": "key=myAPIKEY",
      "Content-Type":"application/json"
    },
    "payload": {
      "data": {
        "score": "5x1",
        "time": "15:10"
      },
      "registration_ids": ["regid1"]
    }
  };
  var response = UrlFetchApp.fetch(url, options);
  Logger.log("Response: "+response.toString());
}

Here, myAPIKEY and regid1 are place holders. 在这里,myAPIKEY和regid1是占位符。 An equivalent java code works well for this. 等效的Java代码对此效果很好。 Hence there seems no problem with the credentials. 因此,凭证似乎没有问题。 I went through this link of android documentation to find the format of json to be sent to GCM server as(Those who edit this code below note that the api keys and others are directly taken from that link on android GCM site. ITS NOT MINE.SO PLS DO NOT CHANGE IT): 我浏览了android文档的链接以找到要发送到GCM服务器的json格式为(以下编辑此代码的人请注意,api键和其他密钥直接从android GCM网站上的该链接获取。它不是我的。因此请不要更改它):

    Content-Type:application/json
Authorization:key=AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA

{
  "registration_ids" : ["APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx..."],
  "data" : {
    ...
  },
}

Note: If I always get "Error=Missing Registration" at Logger.log() statement in execution logs. 注意:如果我总是在执行日志的Logger.log()语句中收到“错误=缺少注册”。 The java equivalent code works perfect. 等效的Java代码效果很好。 But google script gives this error.So what is wrong here? 但是谷歌脚本给出了这个错误。那么这里出什么问题了?

I think you need to set the contentType advanced argument and stringify your payload. 我认为您需要设置contentType高级参数并字符串化有效负载。 See this sample for pointers: 请参阅此示例以获取指针:

https://github.com/entaq/GoogleAppsScript/blob/master/Android/SharedDb.gs https://github.com/entaq/GoogleAppsScript/blob/master/Android/SharedDb.gs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM