> ## Documentation Index
> Fetch the complete documentation index at: https://www.ayrshare.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Python PyPI

> Ayrshare 的 Python PyPI 客户端包

## 概览

Ayrshare 的 [Social-Post-API PyPI 包](https://pypi.org/project/social-post-api/) 让你可以将 Ayrshare API 集成到 Python 应用中。

### 安装

如果你在服务器端使用 Python，请安装 [Social-Post-API PyPI 包](https://pypi.org/project/social-post-api/)。该包对 RESTful 调用做了封装，简化了调用方式。

首先在 [Ayrshare Dashboard](https://app.ayrshare.com/api) 中获取你的密钥 API Key。

然后，安装 Python 包：

```bash theme={"system"}
pip install social-post-api
```

### 一般用法

发帖、历史记录和删除的示例。更多信息请参见 [PyPI 包](https://pypi.org/project/social-post-api/)。

<Info>
  **发布到 X/Twitter？** 自 2026 年 3 月 31 日起，通过 Ayrshare 进行 X/Twitter 的操作需要使用你自己的 X Developer App 凭据——Ayrshare 会在每个 X 相关调用上强制执行这一点。请在请求中添加两个 BYO 请求头。详情请参见[搭建指南](/dashboard/connect-social-accounts/x-twitter-byo-keys)。
</Info>

从 v1.3.0 开始，SDK 提供了一个 `set_twitter_byo(api_key, api_secret)` 辅助方法，会将两个必需的 `X-Twitter-OAuth1-*` 请求头附加到之后的所有请求上：

```python theme={"system"}
from ayrshare import SocialPost

social = SocialPost(API_KEY)
social.set_twitter_byo(MY_X_API_KEY, MY_X_API_SECRET)

social.post({"post": "Hello from BYO", "platforms": ["twitter"]})
```

使用 `clear_twitter_byo()` 移除这些请求头——在使用同一个 SDK 实例服务多个租户时很有用：

```python theme={"system"}
social.clear_twitter_byo().set_twitter_byo(next_tenant_key, next_tenant_secret)
```

```python theme={"system"}
from ayrshare import SocialPost
social = SocialPost('8jKj782Aw8910dCN') # 在 ayrshare.com 获取 API Key

# 任何 platforms 中包含 'twitter' 的发帖都需要此项（强制 BYO）。
social.set_twitter_byo('YOUR_X_CONSUMER_KEY', 'YOUR_X_CONSUMER_SECRET')

# 发布到 Twitter、Facebook 和 LinkedIn 平台
postResult = social.post({'post': 'Nice Posting 2', 'platforms': ['twitter', 'facebook', 'linkedin'], 'profileKey': 'JKSDJI-JKKJKKJ'})
print(postResult)

# 删除（使用 postResult['id'] 中的 Ayrshare 顶层 post id）
deleteResult = social.delete({'id': postResult['id']})
print(deleteResult)

# 历史
print(social.history())
```

### Profile Key

你可以通过 `profileKey` 字段在 POST 请求的请求体或 GET 请求的查询参数中指定用户配置的 Profile Key。

### 更多信息与文档

<ul class="custom-bullets">
  <li>[PyPI 包](https://pypi.org/project/social-post-api/)</li>
  <li>[Github 仓库](https://github.com/ayrshare/social-post-api-python)</li>
  <li>[Issue 讨论](https://github.com/ayrshare/social-post-api-python/issues)</li>
</ul>
