如何在没有Application Default Credentials或Cloud SDK的情况下向Google Cloud API进行身份验证? [英] How to authenticate to Google Cloud API without Application Default Credentials or Cloud SDK?

查看:178
本文介绍了如何在没有Application Default Credentials或Cloud SDK的情况下向Google Cloud API进行身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从AWS Lambda函数访问Google Cloud API,但我不知道如何进行身份验证。 Google Cloud文档中的认证指南( https://cloud.google.com/docs/authentication// a>)希望我下载凭据JSON文件并使用应用程序默认凭证,但任何使用托管函数的人都知道,问题是您不需要管理服务器或运行时环境,因此Lambda不会让我能够在运行代码的环境中存储任意文件。

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.

我可以在本地使用Cloud SDK来获取访问令牌,但它会过期,我可以在我的代码中使用它作为一个永久的解决方案。

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.

是否有一种方法可以获取访问令牌,我可以在代码中无限期地使用它来调用Google Cloud API?有没有其他解决方案?

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?

推荐答案

我发现如何对凭证进行硬编码,而无需将其保存在JSON文件中。这是在这里的文档:

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

下面是一个调用Language API的示例。

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

这篇关于如何在没有Application Default Credentials或Cloud SDK的情况下向Google Cloud API进行身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆