0

我正在使用 Google API 我尝试插入时刻,但出现错误:Google.GoogleApiException 未处理 Message=An Error occurred, but the error response could not be deserialized Source=Google.Apis ServiceName=tasks

我的代码:

// 创建服务。

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
4

1 回答 1

1

您需要做的是设置目标 URL 或在 moment 正文中设置其他元数据。如果你同时设置,你会得到一个错误。以下代码应该可以工作:

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+ 服务客户端,而只是创建了 Tasks 服务客户端并尝试使用它。以下样板是一个完整的示例,它构建了一个 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();
于 2013-07-09T16:53:59.363 回答