Set GOOGLE_APPLICATION_CREDENTIALS in Python project to use Google API
If you're working on a jupyter notebook and want to set GOOGLE_APPLICATION_CREDENTIALS environment variable in Python code :
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/path/to/file.json"
I know that this post was answered but the following is a cleaner way to specify the GOOGLE_APPLICATION_CREDENTIALS
variable.
client = language.LanguageServiceClient.from_service_account_json("/path/to/file.json")
There is 1 more simpler way of making it working by explicity mentioning Credentials and passing them to client as shown below.
import os
from google.oauth2 import service_account
credentials = service_account.Credentials.from_service_account_file("your-json-path-with-filename.json")
client = language.LanguageServiceClient(credentials=credentials)
In case you have the credentials in memory (environment variable for example), and you don't want to create a file especially for it:
from google.cloud import storage
from google.oauth2 import service_account
gcp_json_credentials_dict = json.loads(gcp_credentials_string)
credentials = service_account.Credentials.from_service_account_info(gcp_json_credentials_dict)
client = storage.Client(project=gcp_json_credentials_dict['project_id'], credentials=credentials)
Using python3.7 and google-cloud-storage==1.35.0