Authentication for Google Cloud APIs on Python can be a bit challenging and can discourage those who're keen to use their amazing APIs such as the Natural Language Processing APIs.
Recently I had some issues too and found a simple way to do it. Plus, I think Google Cloud has updated to prevent authentication via Google Cloud SDK. An error such as this will appear:
UserWarning: Your application has authenticated using end user credentials from Google Cloud SDK. We recommend that most server applications use service accounts instead. If your application continues to use end user credentials from Cloud SDK, you might receive a "quota exceeded" or "API not enabled" error. For more information about service accounts, see https://cloud.google.com/docs/authentication/.
I wouldn't say Google Cloud's documentation is the best, so I got lost for a while.
Fortunately, there's a very simple way to do it, simpler than using the SDK.
As suggested by the error message, we'll create a service account.
Go over to Google Cloud Console and create a project. If it's your first time there, it might look different compared to the snapshot below, but just look for the "New Project" button.
You can name it anything you want, but I believe the ID needs to be unique (to the whole world).
Once that is done, click on the "Credentials" section then click on the "Create Credentials" blue button. Select "Service Account Key".
On the next page, select the project that you created from the previous step and leave the option selected as "JSON".
Clicking "Create" will then start a download in your browser for the JSON file. Place it somewhere safe in your local drive or in your working folder.
Make sure that you've installed the Google Cloud Python packages.
pip install google-cloud
On your chosen Python editor, authenticate yourself via the following script (for Natural Language API):
from google.cloud import language path = '/Users/Yourname/YourProjectName-123456.json' #replace this with the FULL path to the JSON file downloaded earlier client = language.LanguageServiceClient.from_service_account_json(path) #YOU'RE AUTHENTICATED!
I'm sure the ".from_service_account_json" is available for the other modules under google.cloud package too, so feel free to explore.
You may continue your journey by testing out some sentiment analysis. Have fun!