Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to authenticate to Google Cloud API without Application Default Credentials or Cloud SDK?

I'm trying to access the Google Cloud API from an AWS Lambda function but I don't know how to authenticate. The auth guide in the Google Cloud documentation (https://cloud.google.com/docs/authentication) wants me to download a credentials JSON file and use Application Default Credentials, but as anyone who has used hosted functions already knows, the point is that you don't need to manage a server or runtime environment, so Lambda doesn't give me the ability to store arbitrary files in the environment of the running code.

I can use the Cloud SDK locally to get an access token but it expires so I can't use it in my function as a permanent solution.

Is there not a way I can get an access token that I can use indefinitely in my code to call the Google Cloud API? Is there any other solution?

like image 939
Mohamed Fakhreddine Avatar asked Jan 10 '17 11:01

Mohamed Fakhreddine


People also ask

How do I authenticate my Google API?

User accounts With a user account, you can authenticate to Google APIs and services in the following ways: Use the gcloud CLI to set up Application Default Credentials (ADC). Use the gcloud CLI to generate access tokens. Use your user credentials to impersonate a service account.

What mechanism should you use to authenticate your application when invoking Google APIs?

Google ID token authentication Authentication with a Google ID token allows users to authenticate by signing in with a Google account. Once authenticated, the user has access to all Google services. You can use Google ID tokens to make calls to Google APIs and to APIs managed by Endpoints.

What are the credentials options are available to access APIs in Google Cloud Platform?

There are three types of credential types available: API key – Use this credential to access publicly-available data anonymously in your app. OAuth client ID – Use this credential to authenticate as an end user and access their data. Requires your app to request and receive consent from the user.


1 Answers

I found how to hard-code the credentials without the need to save them in a JSON file. It was in this documentation here:

https://googlecloudplatform.github.io/google-cloud-node/#/docs/language/0.7.0/guides/authentication

Below is an example that calls the Language API.

var language = require('@google-cloud/language')({
  projectId: '',
  credentials: {
      client_email: '',
      private_key: '',
  }
});

language.detectEntities('Axel Foley is from Detroit').then(function(data) {
  var entities = data[0];
  var apiResponse = data[1];
});
like image 125
Mohamed Fakhreddine Avatar answered Oct 07 '22 00:10

Mohamed Fakhreddine