谷歌api时刻错误Google.GoogleApiException [英] google api moments error Google.GoogleApiException

查看:257
本文介绍了谷歌api时刻错误Google.GoogleApiException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google API 我尝试插入片刻,但出现错误: Google.GoogleApiException未处理 消息=发生错误,但无法反序列化错误响应 来源= Google.Apis ServiceName =任务

I am using Google API I try to insert moments but I get error: Google.GoogleApiException was unhandled 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

推荐答案

您需要做的是设置目标URL或在矩主体内设置其他元数据.如果同时设置两者,则会出现错误.以下代码应该可以工作:

What you need to do is either set the target URL or set the other metadata inside of the moment body. If you are setting both, you will get an error. The following code should work:

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库测试了上面的代码,无论哪种情况,它都可以工作.

I just tested the above code with the 1.4 library, this should work in either case.

您可能不是在创建Google+服务客户端,而是在创建Tasks服务客户端并尝试使用该客户端.以下样例是一个完整的示例,该示例构建了一个Google+服务并编写了一个时刻:

It's possible that you are not creating a Google+ service client but instead are just creating the Tasks service client and trying to use that. The following boilerplate is a full example that constructs a Google+ service and writes a moment:

            // 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();

这篇关于谷歌api时刻错误Google.GoogleApiException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆