0

我正在使用 Google 的官方 js 客户端来访问 Google API,在本例中是 Google Glass Mirror API。

我需要一些有关如何使用update甚至更好的patch方法的指导(请参阅https://developers.google.com/glass/v1/reference/timeline)来更新我之前插入到时间线中的时间线卡。

插入和列出相当容易并且可以成功运行。我已经使用本教程开始:https ://github.com/emil10001/glass-mirror-nodejs-auth-demo/blob/master/app.js

我试过以下:

card = {
        "kind": "mirror#timelineItem",
        "id": "74b88eb3-a6d7-4c13-8b0e-bfdecf71c913",
        "created": "2014-05-22T20:26:56.253Z",
        "updated": "2014-05-22T20:27:18.961Z",
        "etag": "1400790438961",
        "text": "This item3 auto-resizes according to the text length",
        "notification": {
            "level": "DEFAULT"
        }
    }

client
    .mirror.timeline.update({"id": card.id, resource: card})
    .withAuthClient(oauth2Client)
    .execute(function (err, data) {
        if (!!err)
            errorCallback(err);
        else
            successCallback(data);
    });

我得到以下有效负载的成功响应:

{ 
  kind: 'mirror#timelineItem',
  id: '74b88eb3-a6d7-4c13-8b0e-bfdecf71c913',
  selfLink: 'https://www.googleapis.com/mirror/v1/timeline/74b88eb3-a6d7-4c13-8b0e-bfdecf71c913',
  created: '2014-05-22T20:26:56.253Z',
  updated: '2014-05-22T20:32:11.862Z',
  etag: '1400790731862' 
}

我最终得到的是内容为空的卡片。我怀疑我没有正确使用更新方法并且第二个参数resource没有正确命名?

4

1 回答 1

0

Google API 通常使用两种不同的方式来指定事物的值:

  • 参数。这些通常是短值,用于控制您正在使用的资源或该资源如何返回给您。(如果您正在查看 API 文档,这些通常是 URL 的一部分。)
  • 资源。这些就是您关心的价值观本身。(如果您正在查看 API 文档,这将作为消息的正文发送。)

对于timeline.insert,您只是在处理一个资源,因为您只是在添加它。使用timeline.patch 和timeline.update,您可以同时处理一个参数(您正在编辑的资源的ID)和对资源的更改。所以你需要为函数调用指定两个参数。

使用上面的示例,调用可能如下所示:

client
.mirror.timeline.update(card.id, card)
.withAuthClient(oauth2Client)
.execute(function (err, data) {
    if (!!err)
        errorCallback(err);
    else
        successCallback(data);
});

您还询问了timeline.patchtimeline.update之间的区别。

更新用于将时间线项目替换为您在卡片中提供的所有内容(可写)。因此,使用上面的示例,文本通知字段可能会从以前的内容更新。其他字段将被忽略 - 它们不可写,Glass 将根据需要设置/更改它们。

正如您推测的那样,timeline.update 在您有卡片时最有用(因为您在发送给您时存储了它,或者因为您从timeline.list 或其他东西中获取了它)并且您需要更改其中的一些字段.

当您可能没有整张卡片,但您可能仍想更新其中的某些字段时,使用补丁。在这种情况下,资源仅指定您要更改的字段 - 其他字段不受影响。

所以下面两个是等价的:

var update = {
  text: "new text",
  notification: {
    level: "DEFAULT"
  }
};
client.mirror.timeline.update( card.id, update )
.withAuthClient(oauth2Client)
.execute(function(err,data){
  console.log(err,data);
});

var patch = {
  text: "new text",
};
client.mirror.timeline.patch( card.id, patch )
.withAuthClient(oauth2Client)
.execute(function(err,data){
  console.log(err,data);
});

请注意,如果您想删除带有补丁的字段(例如上面的通知),则需要将该字段显式设置为 null。

于 2014-05-23T11:54:12.960 回答