1

I am working on a project (ASP.NET MVC 5) where I am using Umbraco 7.4.3. I am trying to implement the google analytics api along with oauth2. I used the sample code available on the google documentation platform. After authorizing with my google account I get a correct refresh token. But the problem is this refresh token is returned in the URL and is not getting passed by my controller to my view which remains empty. I have a feeling that my controller does not wait to execute it's code after the user authorized his or her google account hence the controller is not bothered about the await operator.

Link to the sample code

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: I have tried this sample code out in a ASP.NET MVC 5 project without Umbraco installed which works perfectly.

Any one able to push me into the right direction?

svick
  • 236,525
  • 50
  • 385
  • 514
user2963570
  • 381
  • 6
  • 21
  • What is the URL to your controller action? When you put a breakpoint in the action, does it get hit? – elolos Apr 21 '16 at 13:57
  • /umbraco/surface/googleanalytics/add. When I put a breakpoint it does get hit but result is always null. – user2963570 Apr 21 '16 at 14:05
  • When debugging, does the cancellation token passed to the action have the value you expect? – elolos Apr 21 '16 at 14:08
  • Yes it is. I'm pretty sure the problem is dedicated to the await operator. Because there is nothing going wrong with the authentication. The problem is the code is not awaiting the respons of the user.. – user2963570 Apr 21 '16 at 14:57

1 Answers1

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.

user2963570
  • 381
  • 6
  • 21