1

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

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

并编写了以下代码

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;
    }

}

但是当我尝试编译它时,我收到以下错误:

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

我还尝试按照另一个线程中的描述“修复”api dll,但这不会编译。

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

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

4

1 回答 1

1

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

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

这样服务就有了 OAuth2 登录

于 2013-03-28T22:01:29.900 回答