Google Analytics .net api OAuth编译错误

时间:2022-11-16 15:15:20

I am trying to write some code that gets the Google Analytics data via the .net/c# api from Google,

我正在尝试编写一些代码,通过来自Google的.net / c#api获取Google Analytics数据,

I use the following topic to get started: stack overflow thread

我使用以下主题开始:堆栈溢出线程

and wrote the following code

并编写了以下代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlTypes;
using System.Security.Cryptography.X509Certificates;
using Google.Apis.Authentication.OAuth2;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Analytics.v3;

namespace ManagementAPI.Models
{
public class Value
{
    public Guid SiteID { get; set; }
    public Guid WidgetID { get; set; }
    public string NewValue { get; set; }
    public DateTime updateTime { get; set; }
    public string GaRefreshToken { get; set; }
    public string GaAccesToken { get; set; }
    public string GaAccountName { get; set; }

    public void getGaValue()
    {
        var client = new WebServerClient(GoogleAuthenticationServer.Description, "319907436177.apps.googleusercontent.com", "rIir_V4IWcckC0QoDX3gZLYd");
        var auth = new OAuth2Authenticator<WebServerClient>(client, Authenticate);
        var asv = new AnalyticsService(auth);
        var request = asv.Data.Ga.Get("ga:" + GaAccountName, "2012-01-01", "2012-02-20", NewValue);
        request.Dimensions = "ga:pagePath";
        request.Sort = "-ga:visitors";
        request.MaxResults = 5;
        var report = request.Fetch();
        Console.ReadLine();
        NewValue = "TEST";
    }

    private static IAuthorizationState Authenticate(WebServerClient client)
    {
        IAuthorizationState state = new AuthorizationState(new string[] { }) { RefreshToken = "REFRESH_TOKEN" };

        client.RefreshToken(state);
        return state;
    }

}

but when i try to compile this i get the following error:

但是当我尝试编译这个时,我得到以下错误:

Error   1   The best overloaded method match for 'Google.Apis.Analytics.v3.AnalyticsService.AnalyticsService(Google.Apis.Services.BaseClientService.Initializer)' has some invalid arguments    H:\vs12\ManagementAPI\ManagementAPI\Models\Value.cs 27  23  ManagementAPI

Error   2   Argument 1: cannot convert from 'Google.Apis.Authentication.OAuth2.OAuth2Authenticator<DotNetOpenAuth.OAuth2.WebServerClient>' to 'Google.Apis.Services.BaseClientService.Initializer'  H:\vs12\ManagementAPI\ManagementAPI\Models\Value.cs 27  44  ManagementAPI

I also tried to "fix" the api dll as described in the other thread, but this would not compile.

我也尝试按照其他线程中的描述“修复”api dll,但这不会编译。

I would like to post this as a comment on the other answer but since I can't, I try to do this with a new question.

我想发布这个作为对另一个答案的评论,但由于我不能,我尝试用一​​个新问题来做这个。

EDIT: Used the wrong version, this will still not compile though.

编辑:使用了错误的版本,但仍然无法编译。

1 个解决方案

#1


1  

I solved the compile problem by making the following change:

我通过进行以下更改解决了编译问题:

 var auth = new OAuth2Authenticator<WebServerClient>(client, Authenticate);
        var asv = new AnalyticsService(new BaseClientService.Initializer()
        {
            Authenticator = auth
        });

This way the Service has a OAuth2 login

这样,服务就有OAuth2登录

#1


1  

I solved the compile problem by making the following change:

我通过进行以下更改解决了编译问题:

 var auth = new OAuth2Authenticator<WebServerClient>(client, Authenticate);
        var asv = new AnalyticsService(new BaseClientService.Initializer()
        {
            Authenticator = auth
        });

This way the Service has a OAuth2 login

这样,服务就有OAuth2登录