How to generate OAuth 2.0 token using Service Account

3,967 views
Skip to first unread message

ganesh ransarje

unread,
Aug 2, 2018, 8:14:42 AM8/2/18
to gce-discussion
Can anyone know how to generate OAuth token 2.0 using Service Account? Actually, I want to create VM instance using rest API and need to provide generated token to the same API.

Yui (Google Cloud Support)

unread,
Aug 2, 2018, 6:11:55 PM8/2/18
to gce-discussion

2. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the JSON file that contains your service account key:

export GOOGLE_APPLICATION_CREDENTIALS=<PATH_of_KEY>

3. Obtain an authorization token:

gcloud auth application-default print-access-token

I hope this helps.

ganesh ransarje

unread,
Aug 3, 2018, 4:36:27 AM8/3/18
to gce-discussion
Hey, thank you so much. It's working fine. but I want to generate the token[using OAuth 2.0+Service Account] using rest api only. Could you help me with this? My approach is as below:-
1. Generate OAuth 2.0 token using Service Account through REST API only
2. Request Create VM REST API along with the generated token 

Yui (Google Cloud Support)

unread,
Aug 6, 2018, 12:50:14 PM8/6/18
to gce-discussion

A good start on Oauth 2.0 for generation of token can be found here. Also, please review this article as well as it explains on how to create a JWT in order to call Google APIs using service accounts, e.g. to create a GCE VM using REST API.



Edward Oak

unread,
May 28, 2019, 5:24:17 PM5/28/19
to gce-discussion
Sorry to gravedig.


2. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the JSON file that contains your service account key:


export GOOGLE_APPLICATION_CREDENTIALS=<PATH_of_KEY>

Can you elaborate on HOW to set the variable to an absolute path that is dependent on current logged-in user?
Also
Where do I put this code?

I need the code required to add inside my login.php wordpress+woo page.

I want to have X amount of .json keys inside a folder. When user_1 login through login.php, I need the code that will fetch the user's json key and auth against google with it.

Thanks

Milad (Google Cloud Platform)

unread,
Jun 27, 2019, 6:09:12 PM6/27/19
to gce-discussion
Hello Edward,

If you want to use a service account you need to set an environment variable as explains here. But I believe you need to use 'User accounts' to authenticate to a GCP API concerning your comment. You can authenticate users directly to your application, when the application needs to access resources on behalf of an end user. Generally, you can authenticate to a Google Cloud Platform API using service accounts or user accounts.

Also, you can create a storage bucket to save your data or .jason keys over there and upload your objects and then use it whenever you need to use.
Reply all
Reply to author
Forward
0 new messages