smarterqueue / threads-api
提供对Meta的Threads OAuth和API的包装
0.2.0
2024-07-16 19:05 UTC
Requires
- php: >=8.1
- guzzlehttp/guzzle: ^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.59
- phpunit/phpunit: ^9
- vimeo/psalm: ^5.25.0
This package is auto-updated.
Last update: 2024-09-16 19:33:06 UTC
README
这提供了一个PHP包装器,用于Meta的Threads API。
使用方法
有2个主要的API服务类 - ThreadsApi
和 ThreadsOAuthHelper
ThreadsApi
- 发送请求和解析响应以及处理错误的基类ThreadsOAuthHelper
- 一个辅助类,包含以下方法:- 构建登录URL
- 将授权代码交换为短期令牌
- 将短期令牌交换为长期令牌
- 刷新长期令牌
在成功响应中,API服务将返回一个包含http代码、头信息和解码数据的ThreadsResponse
对象。
在请求失败或PHP异常时,API服务将抛出一个包含消息、http代码、状态代码(如有)、子代码(如有)、错误类型(如有)、fb跟踪ID(如有)和前一个异常(如有)的ThreadsException
对象。
创建登录页面的链接
use SmarterQueue\ThreadsApi; use SmarterQueue\ThreadsOAuthHelper; // Set up the API instances. $threadsApi = new ThreadsApi($clientId, $clientSecret); $threadsOAuthHelper = new ThreadsOAuthHelper($threadsApi); // Setup CSRF protection. $state = bin2hex(random_bytes(32)); $_SESSION['threads_state'] = $state; // Setup scopes and callback. $scopes = ['threads_basic', 'threads_content_publish']; $redirectUri = 'https://my-site.com/oauth-callback'; // Generate login URL & redirect to it. $loginUrl = $threadsOAuthHelper->getLoginUrl($scopes, $redirectUri, $state); header('Location: ' . $loginUrl);
交换代码以获取访问令牌
use SmarterQueue\ThreadsApi; use SmarterQueue\ThreadsOAuthHelper; // Set up the API instances. $threadsApi = new ThreadsApi($clientId, $clientSecret); $threadsOAuthHelper = new ThreadsOAuthHelper($threadsApi); // GET params. $code = $_GET['code'] ?? null; $state = $_GET['state'] ?? null; // CSRF Check. if (!isset($_SESSION['threads_state']) || $state === null || $state !== $_SESSION['threads_state']) { throw new \Exception('CSRF Error - State mismatch'); } // Exchange authorization code for short-lived token. $redirectUri = 'https://my-site.com/oauth-callback'; $response = $threadsOAuthHelper->getShortLivedAccessToken($code, $redirectUri); $shortLivedToken = $response->decodedData['access_token']; // Exchange short-lived token for long-lived token. $response = $threadsOAuthHelper->getLongLivedAccessToken($shortLivedToken); $longLivedToken = $response->decodedData['access_token'];
刷新长期访问令牌
use SmarterQueue\ThreadsApi; use SmarterQueue\ThreadsOAuthHelper; // Set up the API instances. $threadsApi = new ThreadsApi($clientId, $clientSecret); $threadsOAuthHelper = new ThreadsOAuthHelper($threadsApi); $longLivedToken = 'Replace with your token here'; $threadsApi->setAccessToken($longLivedToken); // Refresh the token. $response = $threadsOAuthHelper->refreshLongLivedAccessToken($longLivedToken); $refreshedToken = $response->decodedData['access_token'];
通用请求
use SmarterQueue\ThreadsApi; use SmarterQueue\ThreadsOAuthHelper; // Set up the API instance. $threadsApi = new ThreadsApi($clientId, $clientSecret); $longLivedToken = 'Replace with your token here'; $threadsApi->setAccessToken($longLivedToken); // Get my details $response = $threadsApi->get('me', ['fields' => 'id,username,threads_profile_picture_url']); // Publish a post $containerResponse = $threadsApi->post('me/threads', ['media_type' => 'TEXT', 'text' => "Test post"]); $threadsApi->post('me/threads_publish', ['creation_id' => $containerResponse->decodedData['id']]);
错误处理
try { $containerResponse = $threadsApi->post('me/threads', ['media_type' => 'TEXT', 'text' => "Test post"]); } catch (ThreadsApiException $e) { $logger->log('Error getting my details', [ 'message' => $e->getMessage(), 'code' => $e->getCode(), 'subCode' => $e->subcode, 'httpCode' => $e->httpCode, ]); }
由 SmarterQueue 提供