Upload financial reports from Google Cloud Storage using the .NET API

Hello, Habr!
Not so long ago at work, I was faced with the task of automating the receipt of financial reports from our Google account. In this publication, I would like to tell you how to do this using the example of the .NET API (C #) and to warn you against errors that I encountered myself.

So let's get started.

To get started, let's collect some primary information:
  • 1. Find out your BucketID.
    To do this, go to play.google.com/apps/publish , click on the "Financial Reports" in the left panel. At the bottom will be the desired BucketID . Please note that we will use BucketID without the gs: // characters below.

  • 2. Getting credential
    We go to console.developers.google.com . Create a new project (Create Project button).

    Enter your project name.

    After creating the project, click on the API & auth in the left pane , then Credential :

    Click the Create Client ID button and select Service account :

    After creating the account, generate and download the P12 key file.

  • 3. Issuing access to read fin. reports
    Ignorance of this feature took me> 50% of the total time spent on application development.

    So, to make everything work without further problems, you need to give the right to read financial statements for the account generated in Section 2 (field EMAIL ADDRESS ).

    To do this, go to the Google Play Publish Page and invite for our EMAIL ADDRESS with the right to read fin. reports. To do this, you need admin rights in your Google account.

To summarize: we found the BucketID , downloaded a .p12 file , received an Email Address and sent an invite with the ability to read fin. reports.

Move on. Create a new console application project in Visual Studio. It is advisable to select the version of the .NET Framework no lower than 4.5 (otherwise there will be problems with spaces in the file names).

After creating the project, install the nuget package with the API:

Install-Package Google.Apis.Storage.v1beta2 

The version of the API may change over time, so stay tuned.

And here is the whole code (assembled in pieces from different places). Replace Bucket, email and the path to the .p12 file with yours.

using System;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Storage.v1beta2;
namespace GoogleCloudStorageAPIClient
    class Program
        static void Main(string[] args)
            const string bucket = "pubsite_prod_rev_XXXXXXXXXXXXXXXXXXXXX";  // без gs://
            const string email = "YYYYYYYYYYYYYYYYYYYYYY@developer.gserviceaccount.com";
            var certificate = new X509Certificate2(
                    @"<указываем путь к .p12 файлу>",
            var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(email)
                Scopes = new[] { StorageService.Scope.DevstorageReadOnly }
            var service = new StorageService(new BaseClientService.Initializer
                HttpClientInitializer = credential,
                ApplicationName = "GoogleReportDownloader"
            var listRequest = service.Objects.List(bucket);
            var list = listRequest.Execute();
            if (list != null)
                Console.WriteLine("File count = {0}\r\n", list.Items.Count);
                foreach (var item in list.Items)
                    // скачать файл
                    var getRequest = service.Objects.Get(bucket, item.Name);
                    var objectName = string.Format("{0}\\{1}", @"C:\!temp\", item.Name);
                    using (var fileStream = new FileStream(objectName, FileMode.Create, FileAccess.Write))

I hope this publication saves someone a lot of time and nerves.

Also popular now: