# @fabianlars/tauri-plugin-oauth 中文文档

规范地址：https://npmcn.com/package/%40fabianlars/tauri-plugin-oauth/

版本：2\.1\.0
npm 元数据更新时间：2026-07-07T10:31:54.640Z
中文译文更新时间：2026-11-14T07:46:25.328Z
软件包许可：MIT OR Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40fabianlars/tauri-plugin-oauth
原文对照：https://npmcn.com/package/%40fabianlars/tauri-plugin-oauth/?lang=original
项目主页：https://github.com/FabianLars/tauri-plugin-oauth#readme
源码仓库：https://github.com/FabianLars/tauri-plugin-oauth

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：15.3 kB，6 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：1 个
- 首次发布：2024-11-05T15:18:25.386Z
- 最新版本发布：2026-07-07T10:31:54.530Z
- 近 30 天下载量：91019（2026-10-14 至 2026-11-12，npm 官方下载统计）
- 已发布版本数：2（近 12 个月 1 个）

## 中文简介

用于产入本地主机服务器的 Tauri 插件 。 需要一些欧斯流 (Login with X).

## 安装

```sh
npm install @fabianlars/tauri-plugin-oauth
```

也可以使用：`pnpm add @fabianlars/tauri-plugin-oauth`、`yarn add @fabianlars/tauri-plugin-oauth`、`bun add @fabianlars/tauri-plugin-oauth`

## README

# Tauri 插件 OAuth 设备

在桌面上处理基于浏览器的 OAuth 流的最小化 Rust 库和 Tauri 插件 应用。 此插件将生成一个临时本地主机服务器, 以捕获 OAuth 重定向, 解析 使用桌面应用程序 OAuth 的挑战.

## 为什么是这个插件?

许多OAuth供应商(如Google和GitHub)不允许自定义的URI计划("深链接")作为 重定向 URL 。 此插件提供了一个解决方案 :

1.  复制临时本地服务器
2.  抓取 OAuth 重定向
3.  将授权数据传送回您的应用程序

> **说明**: 关于采用深层联系的替代办法, 见 [tauri-plugin-深链路](https://github.com/tauri-apps/plugins-workspace/tree/v2/plugins/deep-link). 深层联系 如果没有打开实例, 插件可以自动启动您的应用程序 .

## 安装

```toml
# Cargo.toml
[dependencies]
tauri-plugin-oauth = "2"
```

对于使用 npm 或 纱线的 Tauri 项目 :

```bash
npm install @fabianlars/tauri-plugin-oauth@2
# or
yarn add @fabianlars/tauri-plugin-oauth@2
```

## 使用量

### 锈

```rust
use tauri::{command, Emitter, Window};
use tauri_plugin_oauth::start;

#[command]
async fn start_server(window: Window) -> Result<u16, String> {
    start(move |url| {
        // Because of the unprotected localhost port, you must verify the URL here.
        // Preferebly send back only the token, or nothing at all if you can handle everything else in Rust.
        let _ = window.emit("redirect_uri", url);
    })
        .map_err(|err| err.to_string())
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()

        .plugin(tauri_plugin_oauth::init())
        .invoke_handler(tauri::generate_handler![start_server])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

```

### 类型脚本

```typescript
import {
  start,
  cancel,
  onUrl,
  onInvalidUrl,
} from "@fabianlars/tauri-plugin-oauth";

async function startOAuthFlow() {
  try {
    const port = await start();
    console.log(`OAuth server started on port ${port}`);

    // Set up listeners for OAuth results
    await onUrl((url) => {
      console.log("Received OAuth URL:", url);
      // Handle the OAuth redirect
    });

    // Initiate your OAuth flow here
    // ...
  } catch (error) {
    console.error("Error starting OAuth server:", error);
  }
}

// Don't forget to stop the server when you're done
async function stopOAuthServer() {
  try {
    await cancel(port);
    console.log("OAuth server stopped");
  } catch (error) {
    console.error("Error stopping OAuth server:", error);
  }
}
```

## 配置

您可以使用 `OauthConfig` 结构 :

```rust
use tauri_plugin_oauth::OauthConfig;

let config = OauthConfig {
    ports: Some(vec![8000, 8001, 8002]),
    response: Some("OAuth process completed. You can close this window.".into()),
    ..Default::default()
};

start_with_config(config, |url| {
    // Handle OAuth URL
})
.expect("Failed to start OAuth server");
```

### 回调后重定向浏览器

如果你设定 `redirect_uri` 字段中,插件将以一个 `302 Found` 用于该 URL 而不是服务 HTML 响应。 这是有用的,当你想要 浏览器, 一旦供应商被击中, 即可登陆您的应用程序的真实页面( 例如 Tauri 窗口 URL) 本地主机服务器.

```rust
let config = OauthConfig {
    redirect_uri: Some("http://tauri.localhost/oauth/done".into()),
    ..Default::default()
};
```

> **说明:** 何时 `redirect_uri` 设置, 处理器关闭接收本地主机 URL 供应商击出( 例如 ). `http://127.0.0.1:<port>/?code=...&state=...`(b) —— **没有** 完整 URL 带有 碎片(`#...`),只有浏览器看到。 如果您的 OAuth 流量返回一个符号 URL 片段( 暗流), 剖析 `window.location` 并转发 返回您的应用程序; 默认值( 无) `redirect_uri`) 行为使用内线 脚本已经为你做了这个.

## 安全考虑

-   总是在您服务器侧验证收到的 OAuth URL , 然后再认为它是真实的 .
-   在 OAuth 流中使用 HTTPS 来防止中层人的攻击 .
-   在您的应用程序中执行适当的代号存储并刷新机制 .

## 捐款

总是欢迎捐款! 请随意提交拉力请求.

## 许可证

这个项目根据下列任何一种许可证持有双重许可证,可自行选择:

-   [Apache 许可证,2.0版本](https://npmcn.com/package/%40fabianlars/tauri-plugin-oauth/LICENSE_APACHE-2.0)
-   [MIT 许可证](https://npmcn.com/package/%40fabianlars/tauri-plugin-oauth/LICENSE_MIT)

## 常见问题

### @fabianlars/tauri-plugin-oauth 是什么？

用于产入本地主机服务器的 Tauri 插件 。 需要一些欧斯流 \(Login with X\)\.

### 如何安装 @fabianlars/tauri-plugin-oauth？

在项目目录运行 npm install @fabianlars/tauri-plugin-oauth，或使用 pnpm add / yarn add / bun add。

### @fabianlars/tauri-plugin-oauth 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @fabianlars/tauri-plugin-oauth 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @fabianlars/tauri-plugin-oauth 可以商用吗？

@fabianlars/tauri-plugin-oauth 使用 MIT OR Apache-2\.0 许可。可在 MIT、Apache-2\.0 中任选其一遵守。选择 MIT 时，宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @fabianlars/tauri-plugin-oauth 的下载量有多少？

根据 npm 官方下载统计，@fabianlars/tauri-plugin-oauth 在 2026/10/14 – 2026/11/12 这 30 天内被下载 91,019 次，日均约 3,034 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @fabianlars/tauri-plugin-oauth 还在维护吗？

最新版本 2\.1\.0 发布于 2026/07/07（4 个月前）；至今共发布 2 个版本，其中过去 12 个月发布了 1 个；首次发布于 2024/11/05。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

