5

में किसी सेवा खाते के साथ Google Analytics डेटा पुनर्प्राप्त करते समय अमान्य प्रतिक्रिया त्रुटि मैं Google सेवा खाते का उपयोग करके अपने Google Analytics डेटा तक पहुंचने के लिए एक ऑनलाइन एप्लिकेशन लिखने की कोशिश कर रहा हूं। यहाँ मेरी कोड है:सी # .NET

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace GA_server2server_POC.Models 
{ 
    using System.Security.Cryptography.X509Certificates; 
    using Google.Apis.Analytics.v3; 
    using Google.Apis.Analytics.v3.Data; 
    using Google.Apis.Authentication.OAuth2; 
    using Google.Apis.Authentication.OAuth2.DotNetOpenAuth; 
    using Google.Apis.Util; 
    using Google.Apis.Services; 
    using Google.Apis.Requests; 

    public class Oauth_With_API 
    { 
     public static void ApiTest() 
     { 
      log4net.Config.XmlConfigurator.Configure(); 
      const string ServiceAccountId = "xxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com"; 
      const string ServiceAccountUser = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxdeveloper.gserviceaccount.com"; 
      AssertionFlowClient client = new AssertionFlowClient(
       GoogleAuthenticationServer.Description, new X509Certificate2("C:\\Users\\rcarter\\Downloads\\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-privatekey.p12", "notasecret", X509KeyStorageFlags.Exportable)) 
      { 
       Scope = "https://www.googleapis.com/auth/analytics.readonly", 
       ServiceAccountId = ServiceAccountUser 
      }; 



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

      AnalyticsService service = new AnalyticsService(new BaseClientService.Initializer() 
      { 
       Authenticator = authenticator 
      }); 

      string profileId = "ga:xxxxxxxx"; 
      string startDate = "2013-07-01"; 
      string endDate = "2013-07-15"; 
      string metrics = "ga:visits"; 
      DataResource.GaResource.GetRequest request = service.Data.Ga.Get(profileId, startDate, endDate, metrics); 
      request.Dimensions = "ga:date"; 
      GaData data = request.Execute(); //error occurs here. After this, thread exits. 

      Console.WriteLine(data.TotalResults); 

     } 
    } 
} 

अब तक मेरी कोड निष्पादित करता है, लेकिन मैं निम्नलिखित उत्पादन प्राप्त करें:

WebDev.WebServer40.exe Information: 0 : DotNetOpenAuth, Version=4.0.0.11165, Culture=neutral, PublicKeyToken=2780ccd10d57b246 (official) 
WebDev.WebServer40.exe Information: 0 : Preparing to send AssertionFlowMessage (2.0) message. 
WebDev.WebServer40.exe Information: 0 : Sending AssertionFlowMessage request. 
WebDev.WebServer40.exe Information: 0 : HTTP POST https://accounts.google.com/o/oauth2/token 
WebDev.WebServer40.exe Information: 0 : The following required parameters were missing from the DotNetOpenAuth.OAuth2.Messages.AccessTokenFailedResponse message: {error, 
} 
WebDev.WebServer40.exe Information: 0 : Received UnauthorizedResponse response. 

इस के बाद, धागा बाहर निकलता है, और कार्यक्रम के डेटा के किसी भी मुद्रित करने के लिए मना कर दिया। यह समस्या request.Execute(); पर होती है। जो हिस्सा मैं विशेष रूप से भ्रमित करता हूं, वह यह है कि यदि मैं Console.WriteLine(data.TotalResults); पर ब्रेकपॉइंट डालता हूं, तो मैं स्थानीय वैरिएबल data में इच्छित डेटा देख सकता हूं। इसमें सबकुछ शामिल है जिसे मैं प्रिंट करना चाहता हूं, लेकिन मैं request.Execute(); के बाद कुछ भी करने से रोकने में त्रुटि के कारण की पहचान नहीं कर सकता। बहुत खोज के बाद, मुझे ऊपर सूचीबद्ध त्रुटि के बारे में बहुत कुछ नहीं मिला है।

मैं जिस कोड का उपयोग कर रहा हूं वह इस प्रश्न here पर दिए गए उत्तर पर आधारित है। Google Analytics लाइब्रेरीज़ में कुछ चीजें बदली हैं क्योंकि उस प्रश्न का उत्तर दिया गया था, लेकिन मेरा अधिकांश कोड समान है।

मैंने सभी खाता-विशिष्ट चरों की जांच और पुनः जांच की है। इसका परीक्षण करने के लिए, मैं इसे अपनी स्थानीय मशीन पर एएसपी.नेट एमवीसी 4 वेब ऐप के रूप में चला रहा हूं।

इस समस्या का निवारण कैसे करें इस पर कोई मदद या सलाह की सराहना की जाती है। कृपया मुझे बताएं कि क्या मैं अधिक जानकारी प्रदान कर सकता हूं जो मदद कर सकता है। पढ़ने के लिए धन्यवाद।

+0

निम्नलिखित यह काफी है easy.Problem कोई ज्यादा प्रलेखन होती है/नमूने वहां उपलब्ध हैं की कोशिश। मैंने कुछ जावा कोड को .net समकक्ष में परिवर्तित कर दिया है क्योंकि नमूना केवल जावा में था। मैं कुछ अन्य कोड साझा कर रहा हूं जो मैंने अपने अन्य सिस्टम पर स्थानीय रूप से उपलब्ध कराए हैं। अगर मुझे मदद मिलती है या नहीं तो मुझे बताएं। अन्यथा मैं आपको पूर्ण कामकाजी नमूना भेजूंगा धन्यवाद –

उत्तर

0

एक

using System.Security.Cryptography.X509Certificates; 
using DotNetOpenAuth.OAuth2; 
using Google.Apis.Analytics.v3; 
using Google.Apis.Analytics.v3.Data; 
using Google.Apis.Authentication.OAuth2; 
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth; 
using Google.Apis.Services; 


     private void TestMethod() 
     { 
      try 
      { 
       string scope_url = "https://www.googleapis.com/auth/analytics.readonly"; 

       //client_id: This is the "Email Address" one, not the "Client ID" one... oddly... 
       string client_id = "************-***********************@developer.gserviceaccount.com"; 

       //key_file: This is the physical path to the key file you downloaded when you created your Service Account 
       string key_file = @"***************************************-privatekey.p12"; 

       //key_pass: This is probably the password for all key files, but if you're given a different one, use that. 
       string key_pass = "notasecret"; 


       AuthorizationServerDescription desc = GoogleAuthenticationServer.Description; 

       //key: Load up and decrypt the key 
       X509Certificate2 key = new X509Certificate2(key_file, key_pass, X509KeyStorageFlags.Exportable); 

       //client: we're using the AssertionFlowClient, because we're logging in with our certificate 
       AssertionFlowClient client = new AssertionFlowClient(desc, key) { ServiceAccountId = client_id, Scope = scope_url }; 
       OAuth2Authenticator<AssertionFlowClient> auth = new OAuth2Authenticator<AssertionFlowClient>(client, AssertionFlowClient.GetState); 

       //gas: An instance of the AnalyticsService we can query 
       // AnalyticsService gas = null;// new AnalyticsService(auth); 

       var gas = new AnalyticsService(new BaseClientService.Initializer() 
       { 
        Authenticator = auth 
       }); 
       //r: Creating our query 
       DataResource.GaResource.GetRequest r = gas.Data.Ga.Get("ga:*******", "2012-09-26", "2012-10-10", "ga:visitors"); 

       //d: Execute and fetch the results of our query 
       GaData d = r.Fetch(); 
      } 
      catch (Exception ex) 
      { 

       throw; 
      } 
     } 
+0

याद रखें कि आपको अपने Google Analytics में क्लाइंट_आईडी ईमेल व्यवस्थापक व्यवस्थापक के रूप में पंजीकृत करना होगा लेखा –