gpt4 book ai didi

python - 从 GCE 实例使用时,Google App Engine Admin API 不工作?

转载 作者:行者123 更新时间:2023-11-28 18:36:03 27 4
gpt4 key购买 nike

我正在尝试使用 Google App Engine Admin API通过 Python 客户端库和纯 cURL 调用。

这在本地机器上运行良好,但在 GCE 机器上就不行了。

我正在尝试的 cURL 命令是这样的:

$ curl -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" https://appengine.googleapis.com/v1beta4/apps/myapp
{
"name": "apps/myapp",
"id": "myapp"
}

这是在我的本地机器上成功执行的结果,其中 TOKEN 是我个人帐户的 oauth2 舞蹈的结果。

GCE 机器上的相同命令:

$ curl -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" https://appengine.googleapis.com/v1beta4/apps/myapp
curl: (7) Failed to connect to appengine.googleapis.com port 443: Connection timed out

无论我使用的是我个人的基于 oauth2 的 token ,还是从运行中获得的基于服务帐户的 token ,都会出现此行为

curl http://metadata/computeMetadata/v1/instance/service-accounts/default/token -H "Metadata-Flavor: Google"

尝试使用 Python 客户端库时也是如此。

这是我在 Python 中所做的部分片段:

gae = discovery.build('appengine', 'v1beta4')
method = gae.apps().get(appsId='myapp')
method.execute(http=my_authorized_http_instance)

my_authorized_http_instance 是授权的 httplib2.Http() 实例,使用 https://www.googleapis.com/auth/cloud-platform范围。

在本地机器上,这工作正常,我收到描述 myapp 的响应。

在 GCE 实例上,最后一行需要很长时间才能执行,并最终引发此异常:

ResponseNotReady                          Traceback (most recent call last)
<ipython-input-9-b5373a41f867> in <module>()
----> 1 meth.execute(http=http)

/usr/local/lib/python2.7/dist-packages/oauth2client/util.pyc in positional_wrapper(*args, **kwargs)
135 else: # IGNORE
136 pass
--> 137 return wrapped(*args, **kwargs)
138 return positional_wrapper
139

/usr/local/lib/python2.7/dist-packages/googleapiclient/http.pyc in execute(self, http, num_retries)
720
721 resp, content = http.request(str(self.uri), method=str(self.method),
--> 722 body=self.body, headers=self.headers)
723 if resp.status < 500:
724 break

/usr/local/lib/python2.7/dist-packages/oauth2client/util.pyc in positional_wrapper(*args, **kwargs)
135 else: # IGNORE
136 pass
--> 137 return wrapped(*args, **kwargs)
138 return positional_wrapper
139

/usr/local/lib/python2.7/dist-packages/oauth2client/client.pyc in new_request(uri, method, body, headers, redirections, connection_type)
549
550 resp, content = request_orig(uri, method, body, clean_headers(headers),
--> 551 redirections, connection_type)
552
553 if resp.status in REFRESH_STATUS_CODES:

/usr/local/lib/python2.7/dist-packages/httplib2/__init__.pyc in request(self, uri, method, body, headers, redirections, connection_type)
1606 content = ""
1607 else:
-> 1608 (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
1609 except Exception, e:
1610 if self.force_exception_to_status_code:

/usr/local/lib/python2.7/dist-packages/httplib2/__init__.pyc in _request(self, conn, host, absolute_uri, request_uri, method, body, headers, redirections, cachekey)
1348 auth.request(method, request_uri, headers, body)
1349
-> 1350 (response, content) = self._conn_request(conn, request_uri, method, body, headers)
1351
1352 if auth:

/usr/local/lib/python2.7/dist-packages/httplib2/__init__.pyc in _conn_request(self, conn, request_uri, method, body, headers)
1304 continue
1305 try:
-> 1306 response = conn.getresponse()
1307 except httplib.BadStatusLine:
1308 # If we get a BadStatusLine on the first try then that means

/usr/lib/python2.7/httplib.pyc in getresponse(self, buffering)
1037 #
1038 if self.__state != _CS_REQ_SENT or self.__response:
-> 1039 raise ResponseNotReady()
1040
1041 args = (self.sock,)

ResponseNotReady:

是的,GCE 实例具有 cloud-platform 服务帐户:

$ curl -s http://metadata/computeMetadata/v1/instance/service-accounts/default/scopes -H "Metadata-Flavor: Google" | grep cloud-platform
https://www.googleapis.com/auth/cloud-platform

此外,我想弄清楚这里是否是服务帐户问题,所以我尝试在 GCE 实例上以自己的身份授权,而不是使用服务帐户。我遇到了同样的错误。

有什么想法吗?我做错了什么吗?

最佳答案

您遇到了我们正在修复的托管 VM 的内部错误。

目前,appengine.googleapis.com 映射到 VM 上的内部地址,该地址用于私有(private) VM 到 AppEngine 的流量,但不会将 HTTPS 请求转发到公共(public) API。我们正在转移这个内部 channel ,以免干扰公共(public) channel 。

与此同时,您可以将 appengine.googleapis.com 的手动条目添加到机器上的 /etc/hosts 文件,并重新映射端口 10001(这用于内部通信)指向内部传输地址,像这样:

echo '64.233.181.95 appengine.googleapis.com' >> /etc/hosts
sysctl net.ipv4.ip_forward=1
iptables -t nat -A POSTROUTING -j MASQUERADE
iptables -t nat -A PREROUTING -p tcp -d 64.18.0.0/20,64.233.160.0/19,66.102.0.0/20,66.249.80.0/20,72.14.192.0/18,74.125.0.0/16,108.177.8.0/21,173.194.0.0/16,207.126.144.0/20,209.85.128.0/17,216.58.192.0/19,216.239.32.0/19 --dport 10001 -j DNAT --to-destination 169.254.169.253:10001

(其中 IP 地址集源自 _spf.google.com 记录。)

这只会影响已启用托管 VM 的项目。我们希望很快(几周)准备好修复。

UPD:修复了上面命令中的 appengine.googleapis.com 域名

关于python - 从 GCE 实例使用时,Google App Engine Admin API 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32437261/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com