pythonandroidcsvgoogle-developers-console

How to programmatically download CSV files from Google Play Developer Console using Python?


I am trying to download CSV files from the Google Play Developer Console. Inside the Developer Console, under Download Reports > Statistics, I can download CSV files with statistics on downloads, installations, and uninstalls of my applications.

Using the URI of the bucket that stores the data in Cloud Storage which I obtain in the Developer Console, I open the following URL: https://console.cloud.google.com/storage/browser/pubsite_prod_7157330435810114607, where the files are displayed. From there, being logged in with the same Gmail account as my developer account in Developer Console, I am allowed to download the CSV files one by one.

However, I want to download them using a Python script to plot the data with matplotlib:

import webbrowser

def download_file():
    # Download URL for the CSV file
    download_url = "https://storage.googleapis.com/pubsite_prod_7157330435810114607/stats/installs/installs_com.geology_quiz_and_guide.mineralogy_202301_app_version.csv"

    # Opens the browser at Google's login URL
    webbrowser.open('https://accounts.google.com/')

    # Wait to allow time to log in manually
    input("Press Enter after you have logged into Google...")

    # Once logged in, attempt to download the file
    webbrowser.open(download_url)

    print("Downloading file...")

download_file()

The script does not allow me to download the file due to lack of permissions:

<Error>
    <Code>AccessDenied</Code>
    <Message>Access denied.</Message>
    <Details>Anonymous caller does not have storage.objects.get access to the Google Cloud Storage object. Permission 'storage.objects.get' denied on resource (or it may not exist).</Details>
</Error>

Why can't I download the files if I am logged in and it's the account associated with the bucket?

Is there another way to download the CSV files?


Solution

  • It would be better you download the file from your bucket using a service account and the cloud storage python client.

    First you generate a service account .json file which will contain your credentials so you can authenticate with the google cloud api.

    On your google cloud console, you can go to IAM and admin>Service accounts. You can create a new service account or use default, provided it has the permissions to access the storage bucket for the project which it normally has. Create a new key for the service account and choose the JSON type. Store the downloaded json file at a location convenient for you maybe next to your script.

    Also pip install the library google-cloud-storage and any dependencies.

    Here is a sample code you can use to download the files from your storage bucket.

    from google.cloud import storage
    import os
    
    # set environment variable referencing location of service account file
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "file.json" # actual path
    
    # function to download file from bucket
    def download_blob(bucket_name, source_blob_name, destination_file_name):
        storage_client = storage.Client()
        bucket = storage_client.bucket(bucket_name)
        blob = bucket.blob(source_blob_name)
        
        # download blob to local file
        blob.download_to_filename(destination_file_name)
    

    So if my bucket name is bucket1 and blob inside the bucket is bucket1data and i want to save to '\files\downloaded_blob`

    download_blob('bucket1', 'bucket1data', '\files\downloaded_blob')