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 | |
| 7 | Obtain 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 | |
| 17 | Store in `.env` or export in shell: |
| 18 | |
| 19 | ``` |
| 20 | ZOTERO_LIBRARY_ID=436 |
| 21 | ZOTERO_API_KEY=your_api_key_here |
| 22 | ZOTERO_LIBRARY_TYPE=user |
| 23 | ``` |
| 24 | |
| 25 | For `.env` loading, install `python-dotenv` (`uv add python-dotenv`). It does not come with Pyzotero. Load in Python: |
| 26 | |
| 27 | ```python |
| 28 | import os |
| 29 | from dotenv import load_dotenv |
| 30 | from pyzotero import Zotero |
| 31 | |
| 32 | load_dotenv() |
| 33 | |
| 34 | zot = 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 |
| 44 | import os |
| 45 | |
| 46 | # Personal library |
| 47 | zot = 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 |
| 54 | zot = 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 | |
| 61 | Zotero 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 |
| 64 | zot = Zotero(library_id='0', library_type='user', local=True) |
| 65 | items = zot.items(limit=10) # reads from local Zotero |
| 66 | ``` |
| 67 | |
| 68 | Enable 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 |
| 73 | zot = 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 | |
| 83 | Check what the current API key can access: |
| 84 | |
| 85 | ```python |
| 86 | info = zot.key_info() |
| 87 | # Returns dict with user info and group access permissions |
| 88 | ``` |
| 89 | |
| 90 | Check accessible groups: |
| 91 | |
| 92 | ```python |
| 93 | # Call on a USER client: groups() builds /users/{library_id}/groups. |
| 94 | groups = zot.everything(zot.groups()) |
| 95 | # Includes accessible groups and public groups the key owner belongs to. |
| 96 | ``` |
| 97 | |
| 98 | ## API Key Scopes |
| 99 | |
| 100 | When 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 | |
| 107 | Public 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 | |
| 113 | Local keys are distinct from zotero.org keys. For an authorized write workflow: |
| 114 | |
| 115 | ```python |
| 116 | zot = Zotero('0', 'user', local=True) |
| 117 | auth = zot.authorize_local('Research library tool') # Zotero displays its permission dialog |
| 118 | item = { |
| 119 | 'itemType': 'journalArticle', 'title': 'Example reference', |
| 120 | 'creators': [], 'tags': [], 'collections': [], 'relations': {}, |
| 121 | } |
| 122 | result = zot.create_items([item]) |
| 123 | if 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 | |
| 129 | The 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 | |
| 131 | Sources: [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). |
文件内容是平台检查时读出的文本,只供查看。平台不会运行包里的任何脚本或程序。

