pythonpowershellgoogle-cloud-platformgoogle-cloud-vision

How do I define my Google Cloud Platform service account's key in a python program?


I'm trying to use Google Cloud Vision's OCR as a substitute for Pytesseract. Part of this is defining the account key. The way google suggests doing this is by setting the GOOGLE_APPLICATION_CREDENTIALS environmental variable to the path with the key. I use VSCode, so running $env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\User_1\key.json command in powershell works just fine. However, the program I am writing will be used on my boss's computer, and I can't enter the powershell command every time. The example code Google gives to see if this worked is:

def implicit():
    from google.cloud import storage

    # If you don't specify credentials when constructing the client, the
    # client library will look for credentials in the environment.
    storage_client = storage.Client()

    # Make an authenticated API request
    buckets = list(storage_client.list_buckets())
    print(buckets)

implicit()

Is there another way to pass the key to GCP without doing this? I know with the google sheets API, I use:

def auth():
            creds = None
            token_file = 'token.json'
            SCOPES = ['https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/spreadsheets']
            if os.path.exists(token_file):
                creds = Credentials.from_authorized_user_file(token_file, SCOPES)
            if not creds or not creds.valid:
                if creds and creds.expired and creds.refresh_token:
                    creds.refresh(Request())
                else:
                    flow = InstalledAppFlow.from_client_secrets_file(
                        client_secrets_file='credentials.json', scopes=SCOPES)
                    creds = flow.run_local_server(port=0)
                with open(token_file, 'w') as token:
                    token.write(creds.to_json())
            return creds

However, I am under the impression that this is not applicable here. Or, if it is, I'm not sure how to apply it correctly. Are there any alternatives to running the powershell command every time?

The only alternative I've been able to think of is to find a way to run the powershell command through python, but the program will end up being a standalone .exe on my boss's computer (using pyinstaller), so I do not believe this will work.


Solution

  • I think you have missed the documentation of passing key explicitly. From the doc :

    from google.cloud import storage
    
    # Explicitly use service account credentials by specifying the private key file.
    storage_client = storage.Client.from_service_account_json('path_to_json_file')
    
    # Make an authenticated API request
    buckets = list(storage_client.list_buckets())
    print(buckets)
    

    Another simple way of doing this is setting the GOOGLE_APPLICATION_CREDENTIALS in environment via python itself.

    import os
    from google.cloud import storage
    
    os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'path_to_json_file'
    storage_client = storage.Client()