简体   繁体   中英

Google Analytics .net api OAuth compile error

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM