6

Google 웹 사이트의 통계 데이터를보고하기 위해 Google Analytics API v3 (닷넷 버전)을 사용하고 있습니다. 내 로컬 컴퓨터에서 제대로 실행되는 코드가 있지만 일부 방화벽 규칙으로 인해 프로덕션 서버에서 작동하지 않습니다. Google 시스템 관리자는 프록시를 사용해 보도록 제안합니다. 인터넷에서 Google 애널리틱스 API 서비스의 프록시를 설정하기위한 가이드 라인을 검색했지만 운이 없었습니다. 이와 관련하여 모든 포인터를 이해하십시오.프록시 서버를 통해 Google Analytics v3 API를 라우팅

편집 : 그래서 Web.config의에서 System.Net 구성을 사용하기로 결정

public DataTable GetSearchTrends() 
    { 
     string GoogleAnalyticsProfileId = AppConfigManager.GetGoogleAnalyticsProfileIdForInis(); 

     var service = new AnalyticsService(new BaseClientService.Initializer() 
     { 
      Authenticator = Authenticate() 

     }); 

      DataResource.GaResource.GetRequest request = service.Data.Ga.Get(
      GoogleAnalyticsProfileId, 
      string.Format("{0:yyyy-MM-dd}", StartDate), 
      string.Format("{0:yyyy-MM-dd}", EndDate), 
      GoogleAnalyticsSearchUniquesMetric 
      ); 

     request.Dimensions = GoogleAnalyticsSearchKeywordMetric; 
     request.Sort = string.Concat("-", GoogleAnalyticsSearchUniquesMetric); 
     request.MaxResults = NumberOfSearchTrendsToFetch; 

     GaData response = request.Fetch(); 

     return SearchTrendsHelper.ConvertToDataTable(
      response.Rows, 
      SearchTrendsKeywordsExcludeList, 
      NumberOfSearchTrendsToDisplay 
      ); 
    } 


    private IAuthenticator Authenticate() 
    { 
     string GoogleAnalyticsServiceScope = AnalyticsService.Scopes.AnalyticsReadonly.GetStringValue(); 
     string GoogleApiServiceAccountId = AppConfigManager.GetGoogleApiServiceAccountId(); 
     string GoogleApiServiceAccountKeyFile = AppConfigManager.GetGoogleApiServiceAccountKeyFile(); 
     string GoogleApiServiceAccountKeyPassword = AppConfigManager.GetGoogleApiServiceAccountKeyPassword(); 
     AuthorizationServerDescription desc = GoogleAuthenticationServer.Description; 

     X509Certificate2 key = new X509Certificate2(
      HttpContextFactory.Current.Server.MapPath(GoogleApiServiceAccountKeyFile), 
      GoogleApiServiceAccountKeyPassword, 
      X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet 
      ); 

     AssertionFlowClient client = new AssertionFlowClient(desc, key) { 
      ServiceAccountId = GoogleApiServiceAccountId, 
      Scope = GoogleAnalyticsServiceScope, 
     }; 

     OAuth2Authenticator<AssertionFlowClient> auth = new OAuth2Authenticator<AssertionFlowClient>(
      client, 
      AssertionFlowClient.GetState 
      ); 

     return auth; 
    } 
+0

코드를 업로드 할 수 있습니까? 어떻게 인증하고보고합니까? –

+0

@KamranShahid : 질문을 소스 코드 – itsbalur

답변

3

내가 포럼이나 인터넷에서 유용한 문서를 찾을 수 없습니다.

<system.net> 
    <defaultProxy useDefaultCredentials="true"> 
     <proxy proxyaddress="http://abc.com:3128" usesystemdefault="True" bypassonlocal="True"/> 
     <bypasslist> 
     <add address="http://xyz.com" /> 
     <add address="http://www.example.com" /> 
     </bypasslist> 
    </defaultProxy> 
    </system.net> 

우리가 프록시를 통과하지 않는 모든 요청은 <bypasslist>에 추가 할 수 있습니다. Google API 클래스 라이브러리가 변경 될 때마다 프록시를 설정하기 위해 코드를 다시 작성하지 않아도된다는 이점이 있습니다. :-)

+0

에 추가했습니다. 내가 추가 한 항목의 목록을 제공 할 수 있습니까? bypasslist