跳到主要内容

Zotero 文献库

收藏0

用 pyzotero 程序化管理 Zotero 文献库:检索条目、新建与更新条目、整理分类与标签、导出引用格式、上传 PDF 附件,支持 Web API 与本地 API。

让 AI 安装

星光尘缘LV2最新版本 0.1.0更新于 2026-11-24下载 0 次

references/authentication.md

5.1 KB查看排版效果
1# Authentication & Setup
2
3> **Security:** Never hardcode API keys in source code or commit them to version control. Use environment variables or a `.env` file scoped to `ZOTERO_*` keys only. Placeholder values like `ABC1234XYZ` below are illustrative — substitute your real credentials from env vars.
4
5## Credentials
6
7Obtain from https://www.zotero.org/settings/keys (or create a key at https://www.zotero.org/settings/keys/new):
8
9| Credential | Where to Find |
10|-----------|---------------|
11| **User ID** | "Your userID for use in API calls" section |
12| **API Key** | Create new key at /settings/keys/new, or via Settings → Security → Applications → "Create new key" at https://www.zotero.org/settings/security |
13| **Group Library ID** | Integer after `/groups/` in group URL (e.g. `https://www.zotero.org/groups/169947`) |
14
15## Environment Variables (recommended)
16
17Store in `.env` or export in shell:
18
19```
20ZOTERO_LIBRARY_ID=436
21ZOTERO_API_KEY=your_api_key_here
22ZOTERO_LIBRARY_TYPE=user
23```
24
25For `.env` loading, install `python-dotenv` (`uv add python-dotenv`). It does not come with Pyzotero. Load in Python:
26
27```python
28import os
29from dotenv import load_dotenv
30from pyzotero import Zotero
31
32load_dotenv()
33
34zot = Zotero(
35 library_id=os.environ['ZOTERO_LIBRARY_ID'],
36 library_type=os.environ.get('ZOTERO_LIBRARY_TYPE', 'user'),
37 api_key=os.environ['ZOTERO_API_KEY'],
38)
39```
40
41## Library Types
42
43```python
44import os
45
46# Personal library
47zot = Zotero(
48 os.environ['ZOTERO_LIBRARY_ID'],
49 'user',
50 os.environ['ZOTERO_API_KEY'],
51)
52
53# Group library — use the group ID as library_id
54zot = Zotero('169947', 'group', os.environ['ZOTERO_API_KEY'])
55```
56
57**Important**: A `Zotero` instance is bound to a single library. To access multiple libraries, create multiple instances.
58
59## Local Mode
60
61Zotero 7+ supports unauthenticated local reads at `http://127.0.0.1:23119/api`. User ID `0` selects the local personal library, including an unsynced library. Group libraries still use their numeric group ID.
62
63```python
64zot = Zotero(library_id='0', library_type='user', local=True)
65items = zot.items(limit=10) # reads from local Zotero
66```
67
68Enable local API access: Settings → Advanced → "Allow other applications on this computer to communicate with Zotero". See [cli.md](cli.md) and [mcp.md](mcp.md) for richer local access.
69
70## Optional Parameters
71
72```python
73zot = Zotero(
74 library_id=os.environ['ZOTERO_LIBRARY_ID'],
75 library_type='user',
76 api_key=os.environ['ZOTERO_API_KEY'],
77 locale='en-US', # localise field names (e.g. 'fr-FR' for French)
78)
79```
80
81## Key Permissions
82
83Check what the current API key can access:
84
85```python
86info = zot.key_info()
87# Returns dict with user info and group access permissions
88```
89
90Check accessible groups:
91
92```python
93# Call on a USER client: groups() builds /users/{library_id}/groups.
94groups = zot.everything(zot.groups())
95# Includes accessible groups and public groups the key owner belongs to.
96```
97
98## API Key Scopes
99
100When creating an API key at https://www.zotero.org/settings/keys/new, choose appropriate permissions:
101
102- **Read Only**: For retrieving items and collections
103- **Write Access**: For creating, updating, and deleting items
104- **Notes Access**: To include notes in read/write operations
105- **Files Access**: Required for private stored-file access; uploads also need write access.
106
107Public library reads can omit `api_key`. Pyzotero sends `Zotero-API-Version: 3` and uses bearer authentication for a supplied Web API key. `key_info()` uses `/keys/{key}` internally in 1.15.2, so avoid logging request URLs or exception bodies from that call. The API also supports `/keys/current` with header authentication.
108
109`preserve_json_order` is deprecated; ordinary Python dictionaries retain insertion order.
110
111## Local Writes (Zotero 10+)
112
113Local keys are distinct from zotero.org keys. For an authorized write workflow:
114
115```python
116zot = Zotero('0', 'user', local=True)
117auth = zot.authorize_local('Research library tool') # Zotero displays its permission dialog
118item = {
119 'itemType': 'journalArticle', 'title': 'Example reference',
120 'creators': [], 'tags': [], 'collections': [], 'relations': {},
121}
122result = zot.create_items([item])
123if result.get('failed'):
124 raise RuntimeError(result['failed'])
125```
126
127“Allow” yields a single-use key; “Always Allow” yields a reusable key (`auth['remember']`). Persist a reusable key securely together with `zot.server_id`, then pass `local_api_key` and `server_id` on the next run. Pyzotero discovers and sends `Zotero-Server-ID` before writes. A mismatch requires discarding that server's cached objects and versions. Local versions must never be reused for Web API writes or another local database. Do not loop on authorization dialogs.
128
129The local API lacks `/items/new`: `item_template()`, `attachment_simple()`, and `attachment_both()` are unavailable. Build data using `item_types()` / `item_type_fields()`; for files use `upload_attachments()`. The local API also lacks Atom, so use `format='json'` with `include`, not Pyzotero's `content` exports.
130
131Sources: [local API](https://www.zotero.org/support/dev/web_api/v3/local_api), [Pyzotero local guidance](https://pyzotero.readthedocs.io/en/latest/#the-local-zotero-api).

文件内容是平台检查时读出的文本,只供查看。平台不会运行包里的任何脚本或程序。