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

时光怂恿深爱的人放手 提交于 2019-12-09 14:51:42

问题


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?


回答1:


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];
});


来源:https://stackoverflow.com/questions/41567548/how-to-authenticate-to-google-cloud-api-without-application-default-credentials

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!