我正在使用Google API 我尝试插入片刻,但我收到错误: Google.GoogleApiException未得到处理 Message =发生错误,但无法反序列化错误响应 来源= Google.Apis 的ServiceName =任务
我的代码:
//创建服务。
var service = new TasksService(new BaseClientService.Initializer()
{
Authenticator = auth
});
TaskLists results = service.Tasklists.List().Execute();
//it's work fine
Moment body = new Moment();
ItemScope target = new ItemScope();
target.Id = "replacewithuniqueforaddtarget";
target.Image = "http://www.google.com/s2/static/images/GoogleyEyes.png";
target.Type = "http://schemas.google.com/AddActivity";
target.Description = "The description for the activity";
target.Name = "An example of add activity";
body.Target = target;
body.Target.Url = "https://developers.google.com/+/web/snippet/examples/widget";
body.Type = "http://schemas.google.com/AddActivity";
MomentsResource.InsertRequest insert = new MomentsResource.InsertRequest(service, body, "me", MomentsResource.Collection.Vault);
Moment wrote = insert.Execute(); //error here
答案 0 :(得分:1)
您需要做的是设置目标URL或在当前体内设置其他元数据。如果您同时设置两者,则会出错。以下代码应该有效:
ItemScope target = new ItemScope();
// target.Id = "replacewithuniqueforaddtarget"; // This is optional.
target.Url = "https://developers.google.com/+/web/snippet/examples/widget";
Moment body = new Moment();
body.Target = target;
body.Type = "http://schemas.google.com/AddActivity";
MomentsResource.InsertRequest insert =
new MomentsResource.InsertRequest(service, body, "me",
MomentsResource.Collection.Vault);
Moment wrote = insert.Execute();
或以下代码:
ItemScope target = new ItemScope();
target.Id = "replacewithuniqueforaddtarget"; // Optional
target.Image = "http://www.google.com/s2/static/images/GoogleyEyes.png";
target.Type = "http://schemas.google.com/AddActivity";
target.Description = "The description for the activity";
target.Name = "An example of add activity";
Moment body = new Moment();
body.Target = target;
body.Type = "http://schemas.google.com/AddActivity";
MomentsResource.InsertRequest insert =
new MomentsResource.InsertRequest(service, body, "me",
MomentsResource.Collection.Vault);
Moment wrote = insert.Execute();
我刚用1.4库测试了上面的代码,无论哪种情况都适用。
您可能没有创建Google+服务客户端,而只是创建任务服务客户端并尝试使用它。以下样板文件是构建Google+服务并写下片刻的完整示例:
// Register the authenticator and construct the Plus service
// for performing API calls on behalf of the user.
_authState = YOUR_AUTHSTATE_OBJECT;
AuthorizationServerDescription description =
GoogleAuthenticationServer.Description;
var provider = new WebServerClient(description);
provider.ClientIdentifier = CLIENT_ID;
provider.ClientSecret = CLIENT_SECRET;
var authenticator =
new OAuth2Authenticator<WebServerClient>(
provider,
GetAuthorization)
{
NoCaching = true
};
ps = new PlusService(new BaseClientService.Initializer()
{
Authenticator = authenticator
});
Moment body = new Moment();
body.Target = target;
body.Type = "http://schemas.google.com/AddActivity";
MomentsResource.InsertRequest insert =
new MomentsResource.InsertRequest(ps, body, "me",
MomentsResource.Collection.Vault);
Moment wrote = insert.Execute();