【问题标题】:google api moments error Google.GoogleApiExceptiongoogle api 时刻错误 Google.GoogleApiException
【发布时间】:2013-07-16 02:11:48
【问题描述】:

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

我的代码:

// 创建服务。

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

【问题讨论】:

标签: google-plus


【解决方案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();

【讨论】:

  • 同样的错误。当我尝试下一个代码时,我也会收到此错误: MomentsResource.ListRequest listRequest = new MomentsResource.ListRequest(service, "me", MomentsResource.CollectionEnum.Vault); MomentsFeed 时刻列表 = listRequest.Execute();
  • 你能告诉我更多关于你的开发环境的信息吗?您提到了 .NET,您使用的是哪种库版本和编程语言?
  • c# 表单应用程序,框架 4.0,google.APIs 1.4.0.28227,google.apis.Authentication.OAuth2 1.4.0.28223,Google.apis.plus.v1 1.4.0.28200,google.apis.tasks。 v1 1.4.0.28200,dotNetOpenAuth 4.0.0.11165,Newtonsoft.Json 4.5.0.0
  • 也许您使用的是 Tasks 服务客户端而不是 Plus 服务客户端?
  • 我的代码:私有 IAuthorizationState GetAuthorization(NativeApplicationClient 客户端) { String[] reqAuthScopes = new[] { "googleapis.com/auth/plus.me" }; if (_authstate == null) { _authstate = new AuthorizationState(reqAuthScopes); Uri requestURL = client.RequestUserAuthorization(reqAuthScopes); uri url = client.RequestUserAuthorization(reqAuthScopes);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-05
  • 2016-08-02
  • 2013-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多