afterlogic/mastodon-api

Mastodon API 的 PHP 封装

0.1.1 2022-12-08 12:23 UTC

This package is auto-updated.

Last update: 2024-09-26 18:56:05 UTC


README

Mastodon API 的 PHP 封装,包含 oAuth 辅助工具和基于 Guzzle。

Build Status

入门

通过 Composer 安装。

composer require colorfield/mastodon-api

Mastodon API 和实例

这是一个简单的 API 封装,所以目的是通过让开发者传递所需的端点来支持 API 的进一步更改。

  1. 获取REST Mastodon 文档
  2. 实例列表中获取实例。

快速测试

oAuth

有一个交互式演示可用。

  1. 克隆GitHub 仓库
  2. 进入克隆的目录
  3. 运行composer install
  4. 运行php -S localhost:8000
  5. 在浏览器中,访问https://:8000/test_oauth.php
  6. 您将获得 client_id 和 client_secret,点击授权 URL 链接,然后在 Mastodon 下确认授权并复制授权码。
  7. 获取 bearer:点击“获取访问令牌”按钮。
  8. 使用您的 Mastodon 用户名(电子邮件)和密码进行认证:点击“登录”。

Authorize your application

Authorize your application

Mastodon API

  1. test_credentials.example.php 复制为 test_credentials.php
  2. test_credentials.php 中定义通过 oAuth 获得的信息以及您的 Mastodon 电子邮件和密码。
  3. 在浏览器中,访问 https://:8000/test_api.php

通过 oAuth 进行认证

注册您的应用程序

给它一个名称和一个可选的实例。默认实例为 mastodon.social。

$name = 'MyMastodonApp';
$instance = 'mastodon.social';
$oAuth = new Colorfield\Mastodon\MastodonOAuth($name, $instance);

默认配置仅限于 'read' 和 'write' 范围。您可以通过以下方式修改它:

$oAuth->config->setScopes(['read', 'write', 'follow']);

请注意,必须在获取令牌时完成此操作,因此您不能在之后覆盖它。关于范围更多信息

获取授权码

  1. 获取授权 URL $authorizationUrl = $oAuth->getAuthorizationUrl();
  2. 转到此 URL,授权并复制授权码。

获取 bearer

  1. 将授权码存储在配置值对象中。$oAuth->config->setAuthorizationCode(xxx);

  2. 然后获取访问令牌。作为副作用,将其存储在配置值对象中。$oAuth->getAccessToken();

使用 Mastodon API

使用配置实例化 Mastodon API

OAuth 凭证应从配置值对象中存储以供以后检索。然后您可以使用它如下。

$name = 'MyMastodonApp';
$instance = 'mastodon.social';
$oAuth = new Colorfield\Mastodon\MastodonOAuth($name, $instance);
$oAuth->config->setClientId('...');
$oAuth->config->setClientSecret('...');
$oAuth->config->setBearer('...');
$mastodonAPI = new Colorfield\Mastodon\MastodonAPI($oAuth->config);

用户登录

使用 Mastodon 电子邮件和密码登录。$oAuth->authenticateUser($email, $password);

使用 API 封装

这里有一些 API 封装使用示例。请阅读完整文档

获取

获取凭证

$credentials = $mastodonAPI->get('/accounts/verify_credentials');

获取关注者

$followers = $mastodonAPI->get('/accounts/USER_ID/followers');

发布

清除通知

$clearedNotifications = $mastodonAPI->post('/notifications/clear');

待办:完成删除和流操作。