[Google Cloud Platform] Use Google Cloud API using API Client Library

Introduction

Assumed environment

$ cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)
$ python -V
Python 2.7.5
$ pip -V
pip 7.1.0 from /usr/lib/python2.7/site-packages (python 2.7)

Preparation

$ sudo pip install --upgrade google-api-python-client

$ sudo curl https://sdk.cloud.google.com | bash
→ All questions in the middle can be left with Y or the default value.
$ exec -l $SHELL

Authentication

I will explain two methods.

1. How to use a service account

How to create an application account instead of your google account. Of course, it is a mechanism for the application that you (they) use, so be careful not to distribute it to people.

$ export GOOGLE_APPLICATION_CREDENTIALS='/xxx/xxx/Project name-xxx.json'

2. How to authenticate with gcloud command

How to authenticate with your google account and save your credentials on the host.

$ gcloud auth login
Go to the following link in your browser:

    https://accounts.google.com/o/oauth2/auth?redirect_uri=xxx...
→ Connect to the displayed URL with a web browser logged in with a google account that has access rights to your project.

Enter verification code:* Enter the authentication code displayed at the above URL
Saved Application Default Credentials.

Operation check

sample.py


 1  #!/usr/bin/python
 2
 3  from oauth2client.client import GoogleCredentials
 4  from googleapiclient.discovery import build
 5
 6  credentials = GoogleCredentials.get_application_default()
 7  
 8  compute = build('compute', 'v1', credentials=credentials)
 9  project = 'Project ID'
10  zone = 'Zone name'
11  
12  print compute.instances().list(project=project, zone=zone).execute()

Digression and supplement


Recommended Posts

[Google Cloud Platform] Use Google Cloud API using API Client Library
Let's publish the super resolution API using Google Cloud Platform
Speech transcription procedure using Google Cloud Speech API
Use Google Cloud Vision API from Python
I tried using the Google Cloud Vision API
How to use the Google Cloud Translation API
Read files on GCS using Cloud Storage Client Library
Speech transcription procedure using Python and Google Cloud Speech API
Use configparser when using API
Try to determine food photos using Google Cloud Vision API
Play with YouTube Data API v3 using Google API Python Client
Print PDF using Google Cloud Print. (GoogleAPI)
[Python3] Google translate google translate without using api
Use Google Analytics API from Python
Stream speech recognition using Google Cloud Speech gRPC API on python3 on Mac!
Google Cloud Speech API vs. Amazon Transcribe
Google Cloud Vision API sample for python
[google-oauth] [python] Google APIs Client Library for Python
Streaming speech recognition with Google Cloud Speech API
Implement OAuth without using client library (Java)
Try using Python with Google Cloud Functions
How to use GCP's Cloud Vision API
Creating Google Spreadsheet using Python / Google Data API
SELECT data using client library in BigQuery
How to use Service Account OAuth and API with Google API Client for python
Continue to challenge Cyma's challenges using the OCR service of Google Cloud Platform
I checked the library for using the Gracenote API
Procedure to use TeamGant's WEB API (using python)
Automatic voice transcription with Google Cloud Speech API
Until you can use the Google Speech API
Try running Distributed TensorFlow on Google Cloud Platform
Use JIRA API
[GCP] [Python] Deploy API serverless with Google Cloud Functions!
Upload JPG file using Google Drive API in Python
Monitor temperature using Raspberry Pi + Alibaba cloud IoT platform
From python to running instance on google cloud platform
Use of Google Cloud Storage (GCS) with "GAE / Py"
How to analyze with Google Colaboratory using Kaggle API
Using the National Diet Library Search API in Python
Speech file recognition by Google Speech API v2 using Python
Display the weather forecast on M5Stack + Google Cloud Platform
A story that contributes to new corona analysis using a free trial of Google Cloud Platform