【发布时间】:2013-07-21 21:44:06
【问题描述】:
我正在观看云端硬盘资源。设置手表(使用带有 node.js 和 drive.file.watch 的 googleapis 0.2.13-alpha 客户端):
exports.subscribeDriveCallbacksCmd = function( user, fileId ){
var userId = user.id;
var baseUrl = exports.BASE_URL;
var subscribeUrl = baseUrl+"/incoming/file";
var watchId = 'id-'+fileId+'-'+(new Date()).getTime();
var subscription = {
id: watchId,
token: userId+':'+fileId,
type: 'web_hook',
address: subscribeUrl,
params:{
ttl: 600
}
};
var params = {
fileId: fileId
};
//var cmd = client.drive.files.watch(参数,订阅);
// FIXME - 破解 RPC 实现中的错误 var hack = {频道:订阅}; for(参数中的var键){ hack[key] = 参数[key]; } var cmd = client.drive.files.watch(hack);
返回命令; };
var cmd = exports.subscribeDriveCallbacksCmd(user, '0ZZuoVaqdWGhpUk9PZZ' ); var batch = client.newBatchRequest(); 批处理.add(cmd); batch.withAuthClient(user.auth).execute(cb);
在此之后,我收到了以下回复
以及带有以下标头的同步回调
{ kind: 'api#channel',
id: 'id-0ZZuoVaqdWGhpUk9PZZ-1374536746592',
resourceId: 'WT6g4bx-4or2kPWsL53z7YxZZZZ',
resourceUri: '@987654321@',
token: '101852559274654726533:0ZZuoVaqdWGhpUk9PZZ',
expiration: '1374537347934' }
'x-goog-channel-id': 'id-0ZZuoVaqdWGhpUk9PZZ-1374536746592',
'x-goog-channel-expiration': 'Mon, 22 Jul 2013 23:55:47 GMT',
'x-goog-resource-state': 'sync',
'x-goog-message-number': '1',
'x-goog-resource-id': 'WT6g4bx-4or2kPWsL53z7YxZZZZ',
'x-goog-resource-uri': '@987654322@',
'x-goog-channel-token': '101852559274654726533:0ZZuoVaqdWGhpUk9PZZ',
'user-agent': 'APIs-Google; (+@987654323@)
然而,这有几个问题:
- 这两个返回的resource-id与我订阅手表时传递的fileId不匹配。它确实与 resource-uri 中给出的 ID 匹配
- 尝试使用此处返回的 resourceID 或订阅时传递的 fileId,在尝试停止频道时会返回错误。
为 drive.channel.stop 给出的错误取决于我如何调用。如果我使用 Channel: Stop 页面底部的 API Explorer,为 resourceId 参数提供 resourceId 或 fileId,我会得到
404 Not Found
{
"error": {
"errors": [
{
"domain": "global",
"reason": "notFound",
"message": "Channel not found"
}
],
"code": 404,
"message": "Channel not found"
}
}
如果我在这段代码中使用 node.js 库:
exports.cancelDriveCallbacksCmd = function( watchId, fileId, resourceId ){
var body = {
id: watchId,
resourceId: resourceId
};
var cmd = client.drive.channels.stop( body );
return cmd;
};
var cmd = exports.cancelDriveCallbacksCmd( 'id-0ZZuoVaqdWGhpUk9PZZ-1374536746592', '0ZZuoVaqdWGhpUk9PZZ', 'WT6g4bx-4or2kPWsL53z7YxZZZZ' );
var batch = client.newBatchRequest();
batch.add(cmd);
batch.withAuthClient(user.auth).execute(cb);
我得到了错误
我怀疑这与 Bug 59 有关,它有一个解决方法(这是我在上面使用的 hack 代码)但应该在本周的某个时候修复,我理解。
{ code: 500,
message: 'Internal Error',
data:
[ { domain: 'global',
reason: 'internalError',
message: 'Internal Error' } ] }
所以我把它改成了这段代码,它解决了 files.watch 的错误:
exports.cancelDriveCallbacksCmd = function( watchId, fileId, resourceId ){
var params = {};
var body = {
id: watchId,
resourceId: resourceId,
fileId: fileId
};
//var cmd = client.drive.channels.stop( params, body );
// FIXME - hack around bug in RPC implementation
var hack = {channel:body};
for( var key in params ){
hack[key] = params[key];
}
var cmd = client.drive.channels.stop( hack );
console.log( 'cancelDriveCallbacksCmd', hack );
return cmd;
};
但我得到同样的 500 错误。
关于我可能做错了什么或者如何去调试我可能出错的地方有什么想法吗?
【问题讨论】:
-
第一个问题的答案:resourceId与fileId无关,仅使用1.从Google返回的resourceId和2.您提供的频道ID退订。
-
仅供参考,频道资源已绑定到用户。如果你创建了关于用户 A 资源的频道,你应该取消订阅用户 A 的凭据,否则你会得到 404。
-
更新了附加代码。如果resourceId与fileId无关,为什么resourceUrl包含fileId作为它的一部分?为什么确认返回时没有说明他们正在观看什么文件?但是我传递了通道 ID(我分配的)和资源 ID(它返回的)并且得到了有问题的错误。这两个操作都是由同一个用户完成的,尽管是通过两个不同的应用程序生成的两个不同的 access_tokens。
标签: google-drive-api google-api-nodejs-client