在您的 Transcrypt 代码挂起的 HTML 页面中加载 JavaScript 库。然后您应该能够在您的 Transcrypt 模块中访问 Google 的 JavaScript 客户端库的顶级对象。
我不熟悉 Google 的 JavaScript 客户端库,但看了一眼 doco,我猜这是client.js暴露gapi
的主要对象。
从原理上讲,您可以执行以下操作:首先是 HTML 文件index.html:
<html>
<head>
<meta charset="utf-8">
... other google scripts you might need ...
<script src="https://apis.google.com/js/client.js"></script>
</head>
<body>
... here your page elements...
<script type="module">import * as main from './__target__/your_google_stuff.js';</script>
</body>
</html>
然后在同一目录中有 Transcrypt 模块your_google_stuff.py:
def do_your_stuff():
def auth_callback(res):
if res and res.error is None:
# Authenticated!
...do your stuff after authentication here
# here do the authentication, etc
gapi.client.setApiKey(YOUR_API_KEY) # Note that gapi should be globally available here!
gapi.auth.authorize({
'client_id': YOUR_ID,
... more atributes here...
}, auth_callback)
gapi.load(..., do_yor_stuff)
使用 Transcrypt编译your_google_stuff.py并通过提供index.html查看结果。