在C#.NET中从Google Analytics Reporting API v4获取报告 - GoogleWebAuthorizationBroker.AuthorizeAsync失败

时间:2018-01-02 20:46:59

标签: c# asp.net google-analytics google-analytics-api google-reporting-api

我正在尝试按照分析报告的文档进行操作,但我对Oauth2的文档(在C#中,但我无法开始工作)和分析报告API的文档感到非常困惑。非常稀疏(它有Java和Python中的示例,但不是C#)。我一直在阅读以下文档:

https://developers.google.com/analytics/devguides/reporting/core/v4/

https://developers.google.com/analytics/devguides/reporting/core/v4/

https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth

Analytics Reporting API V4 Client Library for .NET

我在Asa.NET MVC的Oauth2上整理了一些基于Stack Overflow答案和Google Developers documentation的代码。这是我的代码:

class GoogleAnalyticsUtil
{  
    public List<AnalyticsPage> GetReport(DateTime startDate)
    {
        List<AnalyticsPage> report = new List<AnalyticsPage>();
        try
        {
            var credentialTask = GetCredential();
            credentialTask.Wait();
            var credential = credentialTask.Result;
            using (var svc = new AnalyticsReportingService(
                new BaseClientService.Initializer
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "Google Analytics API Console"
                }))
            {
                var dateRange = new DateRange
                {
                    StartDate = startDate.ToString("yyyy-MM-dd"),
                    EndDate = DateTime.Now.ToString("yyyy-MM-dd")
                };
                var sessions = new Metric
                {
                    Expression = "ga:sessions",
                    Alias = "Sessions"
                };
                var date = new Dimension { Name = "ga:date" };

                var reportRequest = new ReportRequest
                {
                    DateRanges = new List<DateRange> { dateRange },
                    Dimensions = new List<Dimension> { date },
                    Metrics = new List<Metric> { sessions },
                    ViewId = "<<view id>>"
                };
                var getReportsRequest = new GetReportsRequest
                {
                    ReportRequests = new List<ReportRequest> { reportRequest }
                };
                var batchRequest = svc.Reports.BatchGet(getReportsRequest);
                var response = batchRequest.Execute();
                foreach (var x in response.Reports.First().Data.Rows)
                {
                    report.Add(new AnalyticsPage()
                    {
                        Path = x.Dimensions.First(),
                        Views = x.Metrics.First().Values
                    });
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        return report;
    }

    static async Task<UserCredential> GetCredential()
    {

        var clientSecretPath = HttpRuntime.AppDomainAppPath + "client_secret.json";
        var credPath = HttpRuntime.AppDomainAppPath + "credentials/GoogleAnalyticsApiConsole/";

        UserCredential credential;

        using (var stream = new FileStream(clientSecretPath,
                FileMode.Open, FileAccess.Read))
        {
            var secrets = GoogleClientSecrets.Load(stream).Secrets;            

            credential = await dsAuthorizationBroker.AuthorizeAsync(
                secrets,
                new[] {AnalyticsReportingService.Scope.Analytics},
                "<<username>>", 
                CancellationToken.None, 
                new FileDataStore(credPath, true));

            return credential;
        }
    }
}

class AnalyticsPage
{
    public string Path { get; set; }
    public IList<string> Views { get; set; }
    public string ViewString { get; set; }
}

public class dsAuthorizationBroker : GoogleWebAuthorizationBroker
{
    public static string RedirectUri = "https://<<my website>>/AuthCallback/IndexAsync";

    public new static async Task<UserCredential> AuthorizeAsync(
        ClientSecrets clientSecrets,
        IEnumerable<string> scopes,
        string user,
        CancellationToken taskCancellationToken,
        IDataStore dataStore = null)
    {
        var initializer = new GoogleAuthorizationCodeFlow.Initializer
        {
            ClientSecrets = clientSecrets,
        };
        return await AuthorizeAsyncCore(initializer, scopes, user,
            taskCancellationToken, dataStore).ConfigureAwait(false);
    }

    private static async Task<UserCredential> AuthorizeAsyncCore(
        GoogleAuthorizationCodeFlow.Initializer initializer,
        IEnumerable<string> scopes,
        string user,
        CancellationToken taskCancellationToken,
        IDataStore dataStore)
    {
        initializer.Scopes = scopes;
        initializer.DataStore = dataStore ?? new FileDataStore(Folder);
        var flow = new dsAuthorizationCodeFlow(initializer);
        return await new AuthorizationCodeInstalledApp(flow,
            new LocalServerCodeReceiver())
            .AuthorizeAsync(user, taskCancellationToken).ConfigureAwait(false);
    }
}


public class dsAuthorizationCodeFlow : GoogleAuthorizationCodeFlow
{
    public dsAuthorizationCodeFlow(Initializer initializer)
        : base(initializer) { }

    public override AuthorizationCodeRequestUrl
                    CreateAuthorizationCodeRequest(string redirectUri)
    {
        return base.CreateAuthorizationCodeRequest(dsAuthorizationBroker.RedirectUri);
    }
}
namespace MySite.Web.Controllers.WebAPI
{
    public class AuthCallbackController : Google.Apis.Auth.OAuth2.Mvc.Controllers.AuthCallbackController
    {
        protected override Google.Apis.Auth.OAuth2.Mvc.FlowMetadata FlowData
        {
            get { return new AppFlowMetadata(); }
        }

        [System.Web.Mvc.HttpGet]
        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 = "PMA"
                });

                // YOUR CODE SHOULD BE HERE..
                // SAMPLE CODE:
                //var list = await service.Files.List().ExecuteAsync();
            }

            Response.Redirect(result.RedirectUri);
        }
    }
}

GetCredential()中的授权失败,错误

`无法启动带有“https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&response_type=code&client_id=224691372209-3ljoils93ufa13mgk2ahilvniqa30f2p.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fmysite.com%2FAuthCallback%2FIndexAsync&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fanalytics”的浏览器进行授权。有关详细信息,请参阅内部异常。

当我尝试直接进入mysite.com/AuthCallback/IndexAsync时,我得到了404,所以也许这就是问题所在?但是我不确定控制器有什么问题,或者为什么导致授权失败。我正在使用Web应用程序Auth2.0客户端ID,但我也尝试使用类型'Other',它不会在参数中使用RedirectUrls,但这也不起作用。我可以在没有重定向的情况下进行授权吗?我需要credential.result授权才能继续使用我的其他代码,或者在C#中我是否应该从Google Analytics Reporting API获取数据呢?

0 个答案:

没有答案