scrape-zyte-login

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese
API keys must never appear in the transcript. Do not echo, read, or write key values directly.
SHUB_APIKEY
and
ZYTE_API_KEY
are persisted in the project's
.env
file. Keep
.env
out of version control.
API密钥绝对不能出现在对话记录中。请勿直接回显、读取或写入密钥值。
SHUB_APIKEY
ZYTE_API_KEY
会持久化存储在项目的
.env
文件中。请将
.env
排除在版本控制之外。

1. Check credentials

1. 检查凭证

Report whether each key is set in the environment or stored in
.env
(do not export anything):
bash
{ [ -n "$ZYTE_API_KEY" ] || grep -q '^ZYTE_API_KEY=' .env 2>/dev/null; } && echo "ZYTE_API_KEY: present" || echo "ZYTE_API_KEY: missing"
{ [ -n "$SHUB_APIKEY" ] || grep -q '^SHUB_APIKEY=' .env 2>/dev/null; } && echo "SHUB_APIKEY: present" || echo "SHUB_APIKEY: missing"
If both are present, proceed to step 3.
报告每个密钥是否已在环境中设置或存储在
.env
文件中(请勿导出任何内容):
bash
{ [ -n "$ZYTE_API_KEY" ] || grep -q '^ZYTE_API_KEY=' .env 2>/dev/null; } && echo "ZYTE_API_KEY: present" || echo "ZYTE_API_KEY: missing"
{ [ -n "$SHUB_APIKEY" ] || grep -q '^SHUB_APIKEY=' .env 2>/dev/null; } && echo "SHUB_APIKEY: present" || echo "SHUB_APIKEY: missing"
如果两个密钥都已存在,直接进入步骤3。

2. Run OAuth setup flow

2. 运行OAuth设置流程

Tell the user:
I'm starting the Zyte OAuth setup flow in your browser. Please complete login/consent there.
Run the OAuth flow script:
bash
uv run ${CLAUDE_SKILL_DIR}/scripts/oauth.py --production
The script stores
SHUB_APIKEY
directly in
.env
(the value is never printed) and saves a projects-and-keys file. Its output includes lines like
Saved data to /path/to/file
and
Saved SHUB_APIKEY to .env
. Scan the output and identify the path for the projects-and-keys file.
Make sure
.env
stays out of version control:
bash
grep -qxF '.env' .gitignore 2>/dev/null || echo '.env' >> .gitignore
告知用户:
我将在你的浏览器中启动Zyte OAuth设置流程,请在那里完成登录/授权操作。
运行OAuth流程脚本:
bash
uv run ${CLAUDE_SKILL_DIR}/scripts/oauth.py --production
该脚本会将
SHUB_APIKEY
直接存储在
.env
文件中(密钥值绝不会被打印),并保存一个项目与密钥文件。其输出包含类似
Saved data to /path/to/file
Saved SHUB_APIKEY to .env
的行。扫描输出内容,找到项目与密钥文件的路径。
确保
.env
文件不纳入版本控制:
bash
grep -qxF '.env' .gitignore 2>/dev/null || echo '.env' >> .gitignore

3. Let user pick project and Zyte API key

3. 让用户选择项目和Zyte API密钥

From the projects-and-keys file found in step 2, list to the user:
  • All available projects:
    project id
    and
    project name
  • All available Zyte API keys:
    apikey name
    (never show key values)
Ask the user to choose exactly one project and one API key by name.
After the user picks an API key, resolve its download URL from the same file, download it, and store it in
.env
under
ZYTE_API_KEY
, replacing any existing entry. The value is never printed to the terminal and is not exported to the shell (
SHUB_APIKEY
is read from
.env
).
The wrapper script prints the HTTP status code on its first line and the response body on the rest. The command below guards against writing a failed response (e.g. an auth error body) into
.env
as if it were a key: it saves the key only when the status is
200
, and otherwise prints the server response so you can report the failure to the user (the key value is never printed):
bash
OUT="$(uv run ${CLAUDE_SKILL_DIR}/../scrape-scrapy-cloud/scripts/scrapy_cloud_api.py GET $URL)"
if [ "$(printf '%s\n' "$OUT" | head -n1)" = "200" ]; then
  { grep -v '^ZYTE_API_KEY=' .env 2>/dev/null; printf 'ZYTE_API_KEY=%s\n' "$(printf '%s\n' "$OUT" | tail -n +2)"; } > .env.tmp && mv .env.tmp .env
  echo "ZYTE_API_KEY stored in .env"
else
  echo "Failed to download Zyte API key; .env left unchanged. Server response:"
  printf '%s\n' "$OUT"
fi
If it reports a failure, do not proceed; the most common cause is a missing or unresolved
SHUB_APIKEY
(needs
shub>=2.18.1
, which reads it from
.env
).
Cleanup the projects-and-keys file after reading:
bash
rm /path/to/projects-and-keys.json
从步骤2中找到的项目与密钥文件里,向用户列出:
  • 所有可用项目:
    project id
    project name
  • 所有可用Zyte API密钥:
    apikey name
    (绝不能显示密钥值)
请用户通过名称选择恰好一个项目和一个API密钥。
用户选择API密钥后,从同一文件中解析其下载URL,下载密钥并将其存储在
.env
文件的
ZYTE_API_KEY
字段下,替换任何现有条目。密钥值绝不会打印到终端,也不会导出到Shell环境(
SHUB_APIKEY
会从
.env
文件读取)。
包装脚本会在第一行打印HTTP状态码,其余行打印响应体。下面的命令可防止将失败响应(如认证错误内容)当作密钥写入
.env
文件:仅当状态码为
200
时才保存密钥,否则打印服务器响应以便你向用户报告失败(密钥值绝不会被打印):
bash
OUT="$(uv run ${CLAUDE_SKILL_DIR}/../scrape-scrapy-cloud/scripts/scrapy_cloud_api.py GET $URL)"
if [ "$(printf '%s\n' "$OUT" | head -n1)" = "200" ]; then
  { grep -v '^ZYTE_API_KEY=' .env 2>/dev/null; printf 'ZYTE_API_KEY=%s\n' "$(printf '%s\n' "$OUT" | tail -n +2)"; } > .env.tmp && mv .env.tmp .env
  echo "ZYTE_API_KEY stored in .env"
else
  echo "Failed to download Zyte API key; .env left unchanged. Server response:"
  printf '%s\n' "$OUT"
fi
如果报告失败,请不要继续操作;最常见的原因是
SHUB_APIKEY
缺失或未解析(需要
shub>=2.18.1
,该版本会从
.env
文件读取密钥)。
读取完成后清理项目与密钥文件:
bash
rm /path/to/projects-and-keys.json

4. Save selected project ID

4. 保存选中的项目ID

If the user selected a project ID in step 3, save it:
bash
mkdir -p .scrape/.zyte
echo "PROJECT_ID" > .scrape/.zyte/project-id
If no project was selected, skip. Tell the user: "Zyte credentials are active."
Return to the caller:
Zyte setup complete:
  ZYTE_API_KEY: present
  SHUB_APIKEY: present
  Project ID: 12345 (or not saved)
如果用户在步骤3中选择了项目ID,保存该ID:
bash
mkdir -p .scrape/.zyte
echo "PROJECT_ID" > .scrape/.zyte/project-id
如果未选择项目,请跳过此步骤。告知用户:"Zyte凭证已激活。"
向调用方返回:
Zyte setup complete:
  ZYTE_API_KEY: present
  SHUB_APIKEY: present
  Project ID: 12345 (or not saved)