> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-docs-zh.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Gmail toolkit 集成

> 使用 LangChain Python 集成 Gmail 工具包。

这将帮助您开始使用 Gmail [工具包](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/tools/google_gmail.md)。此工具包与 Gmail API 交互，以读取消息、起草并发送消息等。要查看所有 `GmailToolkit` 功能和配置的详细文档，请访问 [API reference](https://reference.langchain.com/python/langchain-google-community/gmail/toolkit/GmailToolkit)。

## 设置

要使用此工具包，您需要按照 [Gmail API 文档](https://developers.google.com/gmail/api/quickstart/python#authorize_credentials_for_a_desktop_application) 中说明的凭据进行配置。下载完 `credentials.json` 文件后，即可开始使用 Gmail API。

### 安装

该工具包位于 `langchain-google-community` 包中。需要添加 `gmail` 扩展：

```python theme={null}
pip install -qU langchain-google-community\[gmail\]
```

为启用对单个工具的自动化追踪，设置您的 [LangSmith](https://docs.langchain.com/langsmith/observability) API 密钥：

```python theme={null}
os.environ["LANGSMITH_TRACING"] = "true"
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
```

## 初始化

默认情况下，工具包会读取本地 `credentials.json` 文件。您也可以手动提供 `Credentials` 对象。

```python theme={null}
from langchain_google_community import GmailToolkit

toolkit = GmailToolkit()
```

### 自定义身份验证

底层会通过以下方法创建 `googleapi` 资源。您也可以手动构建 `googleapi` 资源以获得更精细的身份验证控制。

```python theme={null}
from langchain_google_community.gmail.utils import (
    build_resource_service,
    get_google_credentials,
)

# Can review scopes here https://developers.google.com/gmail/api/auth/scopes
# For instance, readonly scope is 'https://www.googleapis.com/auth/gmail.readonly'
credentials = get_google_credentials(
    token_file="token.json",
    scopes=["https://mail.google.com/"],
    client_secrets_file="credentials.json",
)
api_resource = build_resource_service(credentials=credentials)
toolkit = GmailToolkit(api_resource=api_resource)
```

## 工具

查看可用工具：

```python theme={null}
tools = toolkit.get_tools()
tools
```

```text theme={null}
[GmailCreateDraft(api_resource=<googleapiclient.discovery.Resource object at 0x1094509d0>),
 GmailSendMessage(api_resource=<googleapiclient.discovery.Resource object at 0x1094509d0>),
 GmailSearch(api_resource=<googleapiclient.discovery.Resource object at 0x1094509d0>),
 GmailGetMessage(api_resource=<googleapiclient.discovery.Resource object at 0x1094509d0>),
 GmailGetThread(api_resource=<googleapiclient.discovery.Resource object at 0x1094509d0>)]
```

* [GmailCreateDraft](https://reference.langchain.com/python/langchain-google-community/gmail/create_draft/GmailCreateDraft)
* [GmailSendMessage](https://reference.langchain.com/python/langchain-google-community/gmail/send_message/GmailSendMessage)
* [GmailSearch](https://reference.langchain.com/python/langchain-google-community/gmail/search/GmailSearch)
* [GmailGetMessage](https://reference.langchain.com/python/langchain-google-community/gmail/get_message/GmailGetMessage)
* [GmailGetThread](https://reference.langchain.com/python/langchain-google-community/gmail/get_thread/GmailGetThread)

## 在智能体中集成

以下示例展示如何将工具包集成到 [智能体](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/agents.md) 中。

需要 LLM 或对话模型：

```python theme={null}
# | output: false
# | echo: false

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-5.4-mini", temperature=0)
```

```python theme={null}
from langchain.agents import create_agent


agent_executor = create_agent(llm, tools)
```

```python theme={null}
example_query = "Draft an email to fake@fake.com thanking them for coffee."

stream = agent_executor.stream_events(
    {"messages": [("user", example_query)]},
    version="v3",
)
for snapshot in stream.values:
    snapshot["messages"][-1].pretty_print()
```

```text theme={null}
================================ Human Message =================================

Draft an email to fake@fake.com thanking them for coffee.
================================== Ai Message ==================================
Tool Calls:
  create_gmail_draft (call_slGkYKZKA6h3Mf1CraUBzs6M)
 Call ID: call_slGkYKZKA6h3Mf1CraUBzs6M
  Args:
    message: Dear Fake,

I wanted to take a moment to thank you for the coffee yesterday. It was a pleasure catching up with you. Let's do it again soon!

Best regards,
[Your Name]
    to: ['fake@fake.com']
    subject: Thank You for the Coffee
================================= Tool Message =================================
Name: create_gmail_draft

Draft created. Draft Id: r-7233782721440261513
================================== Ai Message ==================================

I have drafted an email to fake@fake.com thanking them for the coffee. You can review and send it from your email draft with the subject "Thank You for the Coffee".
```

***

## API 参考

要查看所有 `GmailToolkit` 功能和配置的详细文档，请访问 [API reference](https://reference.langchain.com/python/langchain-google-community/gmail/toolkit/GmailToolkit)。

***

> 官方原文：[src/oss/python/integrations/tools/google\_gmail.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/python/integrations/tools/google_gmail.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.