Google c# Api, changing from v2.3 to v3

后端 未结 2 427
执念已碎
执念已碎 2021-01-21 05:54

I have the following code to query google analytics using the C# v2.3 api:

string username = \"SAMPLE@SAMPLE.COM\";
string pass = \"PASS\";
string gkey = \"?key=         


        
相关标签:
2条回答
  • 2021-01-21 06:36

    I was having the same problem. I'd installed the nuget packages, but get the same error "Could not load file or assembly 'Microsoft.Threading.Tasks.Extensions.Desktop, Version=1.0.16.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified." I tried removing and re-installing the nuget packages to no avail. So, I eventually just searched for the Microsoft.Threading.Tasks.Extensions.Desktop.dll file and it was found in the net40 folder. I copied it to my bin folder and then it worked.

    0 讨论(0)
  • 2021-01-21 06:52

    Accessing Google Analtyics with v3 client library is really nice tbh. The only thing i havent worked out is how to send it a refreshtoken i have stored in the database. Im stuck with the one it stores for me on the Pc.

    Request autentication: All the client scret stuff is stored in client_secret.json you can download the file from google apis consol. It will pop up a browser window if it doesnt have approval if it does it will just continue on.

    private void Form1_Load(object sender, EventArgs e)
    {
     UserCredential credential;
     using (var stream = new System.IO.FileStream("client_secret.json", System.IO.FileMode.Open, System.IO.FileAccess.Read))
     {
      credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
      GoogleClientSecrets.Load(stream).Secrets,
      new[] { AnalyticsService.Scope.AnalyticsReadonly },
      "user", CancellationToken.None, new FileDataStore("Analytics.Auth.Store")).Result;
      }
    

    }

    Now to access Google Analytics you need to make an analytics Service.

    AnalyticsService service = new AnalyticsService(new BaseClientService.Initializer() {
     HttpClientInitializer = credential,
     ApplicationName = "Analytics API sample",
     });
    

    All your calls will now be run against that.

    Yours should end up being something like:

    DataResource.GaResource.GetRequest request = service.Data.Ga.Get(ga:34197921, new DateTime(2012, 1, 2).ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd"), ""ga:visits,ga:visitors,ga:visitBounceRate,ga:goalStartsAll,ga:goalCompletionsAll,ga:goal1Starts,ga:goal1Completions,ga:goal2Starts,ga:goal2Completions,ga:goal3Starts,ga:goal3Completions,ga:goal4Starts,ga:goal4Completions,ga:adCost,ga:totalValue");
    request.Dimensions = "ga:medium,ga:campaign,ga:date,ga:isMobile,ga:isTablet,ga:searchKeyword,ga:hostname";
    

    I have a blog post that runs though most of the diffrent calls you can make. http://daimto.com/google-analytics-api-v3-with-c/

    0 讨论(0)
提交回复
热议问题