【问题标题】:Google API & OAuth2谷歌 API 和 OAuth2
【发布时间】:2014-08-08 09:59:57
【问题描述】:

遵循here 概述的 Google 代码示例。

我已经在我自己的测试项目中复制了他们的代码示例(在 Visual Studio Express 2013 中使用 MVC 4 模板),我无法运行该项目,因为我遇到了这些错误:

错误 1 ​​由于 'Google.Apis.Sample.MVC4.Controllers.HomeController.IndexAsync(System.Threading.CancellationToken)' 是一个返回 'Task' 的异步方法,因此 return 关键字后面不能跟对象表达式。您是否打算返回“任务”? f:\users\sausages\documents\visual studio 2013\Projects\GooglePOC\GooglePOC\Controllers\googleAnalytics.cs 33 17 GooglePOC

错误 2 由于 'Google.Apis.Sample.MVC4.Controllers.HomeController.IndexAsync(System.Threading.CancellationToken)' 是返回 'Task' 的异步方法,因此 return 关键字后面不能跟对象表达式。您是否打算返回“任务”? f:\users\sausages\documents\visual studio 2013\Projects\GooglePOC\GooglePOC\Controllers\googleAnalytics.cs 37 17 GooglePOC

这是它抱怨的代码块:

using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Mvc;

using Google.Apis.Auth.OAuth2.Mvc;
using Google.Apis.Drive.v2;
using Google.Apis.Services;

using Google.Apis.Sample.MVC4;

namespace Google.Apis.Sample.MVC4.Controllers
{
    public class HomeController : Controller
    {
        public async Task IndexAsync(CancellationToken cancellationToken)
        {
            var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata()).
                AuthorizeAsync(cancellationToken);

            if (result.Credential != null)
            {
                var service = new DriveService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = result.Credential,
                    ApplicationName = "ASP.NET MVC Sample"
                });

                // YOUR CODE SHOULD BE HERE..
                // SAMPLE CODE:
                var list = await service.Files.List().ExecuteAsync();
                ViewBag.Message = "FILE COUNT IS: " + list.Items.Count();
                return View();
            }
            else
            {
                return new RedirectResult(result.RedirectUri);
            }
        }
    }
}

【问题讨论】:

  • Taskvoid 几乎相同,但在异步上下文中,因此它可能会抱怨这一点。如果您改为返回Task<ViewResult>,它会改变吗?

标签: c# .net google-api


【解决方案1】:
 public async Task<ActionResult> IndexAsync(CancellationToken cancellationToken)

为我工作

【讨论】:

  • 这没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方发表评论 - 您可以随时评论自己的帖子,一旦您有足够的reputation,您就可以comment on any post
  • 对我有用的问题的答案是在方法定义中提供类型 Task (替换 Task),正如我在代码行中说明的那样。认为回答了这个问题(我认为这是“我怎样才能让我的代码工作?)。
猜你喜欢
  • 2012-06-17
  • 1970-01-01
  • 2021-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-13
  • 1970-01-01
相关资源
最近更新 更多