2013-03-26 48 views
1

我尝试写一些代码,通过从谷歌的.NET/C#API获取谷歌Analytics的数据,谷歌分析.NET API的OAuth编译错误

我用下面的话题开始:stack overflow thread

,并写了下面的代码

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,但这不会编译。

我想发布这个作为对其他答案的评论,但因为我不能,我尝试用一​​个新的问题做到这一点。

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

回答

1

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

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

这样的服务有OAuth2用户登录

+0

你可以给我一些参考,其中该认证进度被指定为每最近的图书馆。很难找到用于此的代码示例。特别是WebServerClient类在哪里? – 2013-05-26 11:35:23