1

我正在开发一个使用 Umbraco 7.4.3 的项目(ASP.NET MVC 5)。我正在尝试与 oauth2 一起实现谷歌分析 api。我使用了 google 文档平台上提供的示例代码。使用我的谷歌帐户授权后,我得到了正确的刷新令牌。但问题是这个刷新令牌是在 URL 中返回的,并且没有被我的控制器传递给我的视图,该视图仍然是空的。我有一种感觉,在用户授权他或她的谷歌帐户后,我的控制器不会等待执行它的代码,因此控制器不会为 await 运算符而烦恼。

链接到示例代码

public class GoogleAnalyticsController : SurfaceController
{
    public async Task<ActionResult> Add(CancellationToken cancellationToken) 
    {
        var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata()).AuthorizeAsync(cancellationToken);

        if (result.Credential != null)
        {
            var service = new AnalyticsService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = result.Credential,
                    ApplicationName = "Analytics Dashboard"
                });

            // YOUR CODE SHOULD BE HERE..
            ViewBag.AccessToken = result.Credential.Token.AccessToken;
            ViewBag.RefreshToken = result.Credential.Token.RefreshToken;

            var list = await service.Management.AccountSummaries.List().ExecuteAsync(cancellationToken);
            ViewBag.Username = list.Username;

            for (int i = 0; i < list.TotalResults; i++)
            {
                ViewBag.WebsiteNames += list.Items[i].Name + "(" + list.Items[i].WebProperties[0].WebsiteUrl + ")";
            }

            return View("~/Views/Configboard.cshtml");
        }
        else
        {
            return new RedirectResult(result.RedirectUri);
        }
    }

PS:我已经在没有安装 Umbraco 的 ASP.NET MVC 5 项目中尝试了这个示例代码,它运行良好。

有谁能把我推向正确的方向?

4

1 回答 1

2

For anyone else getting this problem, the solution was actually pretty simple:

I made a custom route for the AuthCallbackController (/authcallback/indexasync) and it all worked. Because Umbraco takes over the default routing this URL was not reachable hence the action of the authcallbackcontroller was not executed.

于 2016-04-26T09:39:33.617 回答