问题描述
我正在尝试对我在App Engine中运行的应用进行身份验证以调用Cloud Run服务。为此,我通过Google Auth库(getIdTokenClient方法)请求了OAuth 2令牌,在这里https://github.com/googleapis/google-auth-library-nodejs#working-with-id-tokens似乎是推荐的方法。
当OAuth 2尝试访问Google元数据时,我的应用出现了以下错误:
gaxios.ts:91混合内容:“ https://myapp-dev.nw.r.appspot.com/”上的页面已通过HTTPS加载,但请求了不安全的资源“ http://169.254.169.254” / computeMetadata / v1 / instance'。该请求已被阻止;内容必须通过HTTPS提供。
下面是我的代码:
const {GoogleAuth} = require('google-auth-library');
const url = 'https://myapp-dev-fvnpywgyfa-nw.a.run.app';
const auth = new GoogleAuth();
const serviceRequestOptions = {
method: 'GET',headers: {
'Content-Type': 'text/plain',},timeout: 3000,};
try {
// Create a Google Auth client with the Renderer service url as the target audience.
if (!client) client = await auth.getIdTokenClient(url);
// Fetch the client request headers and add them to the service request headers.
// The client request headers include an ID token that authenticates the request.
const clientHeaders = await client.getRequestHeaders();
serviceRequestOptions.headers['Authorization'] =
clientHeaders['Authorization'];
} catch (err) {
throw Error('Could not create an identity token: ',err);
}
解决方法
gaxios.ts:91混合内容:
https://myapp-dev.nw.r.appspot.com/
上的页面已通过HTTPS加载,但请求了不安全的资源http://169.254.169.254/computeMetadata/v1/instance
。该请求已被阻止;内容必须通过HTTPS投放
-
子网169.254.0.0/16是用于“本地链接”块(rfc3330)的IANA专用网络(rfc3927)。该子网未路由到公共Internet,因此只能在本地网段中访问。
-
URL
http://169.254.169.254/computeMetadata/v1/instance
在诸如AWS,Yandex Cloud,Google Cloud Platform之类的Cloud Services中用作内部链接本地地址 (GCP还使用http://metadata.google.internal/computeMetadata/v1/instance
URL)获取有关VM实例的信息。
IP地址169.254.169.254仅可通过http:访问,因为它在专用内部网络中工作,在该内部网络中无法验证SSL证书(请参阅第1节-到公共Inet的路由)。
因此,如果您的应用尝试访问http://169.254.169.254/computeMetadata/v1/instance
-您肯定做错了。无法访问该地址by easy way
可能是Using OAuth 2.0 to Access Google APIs链接可以为您提供帮助。