aeknasd145 / tiktokoauth2
TikTok API 客户端。处理 OAuth,CSRF 保护。易于实现和扩展。这是一个任何 composer 项目的独立库。
dev-main
2024-06-11 22:21 UTC
Requires
- php: ^7.4 || ^8.0 || ^8.1
- ext-curl: *
- composer/ca-bundle: ^1.2
Requires (Dev)
- guzzlehttp/psr7: ^1.2
- php-http/guzzle5-adapter: ^1.0
This package is auto-updated.
Last update: 2024-09-11 22:49:16 UTC
README
TikTok OAuth 2 PHP8 API 是基于 TikTok 开发者文档 创建的。该 API 目前仅支持 Login Kit。其他功能将很快添加。
如何通过 Composer 安装?
composer require aeknasd145/tiktokoauth2
如何获取 TikTok 登录的 OAuth URL?
$tikTok = new TikTok($tikTokClientKey, $tikTokClientSecret, $tikTokCallbackUrl); $tikTokAuthUrl = $tikTok->getAuthUrl();
当回调返回时,应采取以下步骤
$accessTokenData = $tikTok->fetchAccessToken($code); $accessToken = $accessTokenData['access_token']; $expiresIn = $accessTokenData['expires_in']; $openId = $accessTokenData['open_id']; $refreshExpiresIn = $accessTokenData['refresh_expires_in']; $refreshToken = $accessTokenData['refresh_token']; $scope = $accessTokenData['scope']; $tokenType = $accessTokenData['token_type'];
如何需要时刷新访问令牌?
$refreshTokenData = $tikTok->refreshAccessToken($refreshToken); $accessToken = $refreshTokenData['access_token']; $expiresIn = $refreshTokenData['expires_in']; $openId = $refreshTokenData['open_id']; $refreshExpiresIn = $refreshTokenData['refresh_expires_in']; $refreshToken = $refreshTokenData['refresh_token']; $scope = $refreshTokenData['scope']; $tokenType = $refreshTokenData['token_type'];
获取用户基本信息(open_id,union_id,avatar_url,avatar_url_100,avatar_large_url,display_name)
$userInfoBasicData = $tiktok->getUserInfoBasic($accessToken)['data']['user']; $openId = $userInfoBasicData['open_id']; $unionId = $userInfoBasicData['union_id']; $avatarUrl = $userInfoBasicData['avatar_url']; $avatarUrl100 = $userInfoBasicData['avatar_url_100']; $avatarLargeUrl = $userInfoBasicData['avatar_large_url']; $displayName = $userInfoBasicData['display_name'];
如何获取查询创作者信息?
$queryCreatorInfo = $tiktok->getQueryCreatorInfo($accessToken)['data']; $stitchDisabled = $queryCreatorInfo['stitch_disabled']; $commentDisabled = $queryCreatorInfo['comment_disabled']; $creatorAvatarUrl = $queryCreatorInfo['creator_avatar_url']; $creatorNickname = $queryCreatorInfo['creator_nickname']; $creatorUsername = $queryCreatorInfo['creator_username']; $duetDisabled = $queryCreatorInfo['duet_disabled']; $maxVideoPostDurationSec = $queryCreatorInfo['max_video_post_duration_sec']; $privacyLevelOptions = $queryCreatorInfo['privacy_level_options']; // array, [0]=> string(18) "PUBLIC_TO_EVERYONE" [1]=> string(21) "MUTUAL_FOLLOW_FRIENDS" [2]=> string(9) "SELF_ONLY"
如何直接发布照片?
$title = 'Content Title'; $caption = 'Content caption'; // $contentFiles has to be array and at least 2 image $contentFiles = [ 'https://site.com/file-url.png', 'https://site.com/file-url-2.png, ]; $photoData = $this->publishTikTokPhoto($accessToken, $title, $caption, $contentFiles); // TODO: if status is PROCESSING_DOWNLOAD, it should try again, you should use sleep and var getPostStatus inside do while sleep(5); // wait a few seconds $postStatusData = $this->getPostStatus($accessToken, $photoData['data']['publish_id']); $postStatus = $postStatusData['data']['status']; $postStatusErrorCode = $postStatusData['error']['code']; $postStatusErrorMessage = $postStatusData['error']['message']; $postStatusErrorLogId = $postStatusData['error']['log_id'];
如何直接发布视频?
$title = 'Content Title'; $videoUrl = 'https://site.com/video.mp4'; $videoCoverTimestampMs = 1000; // ms of the cover picture $videoData = $this->publishTiktokVideo($accessToken, $title, $videoUrl, $videoCoverTimestampMs); $videoPublishId = $videoData['data']['publish_id']; $videoErrorCode = $videoData['error']['code']; $videoErrorMessage = $videoData['error']['message']; $videoErrorLogId = $videoData['error']['log_id'];
错误信息与官方文档不同
- 如果您收到 'unaudited_client_can_only_post_to_private_accounts' 错误,这意味着您已通过 TikTok 对 'video.publish' 范围的审核,但您需要申请高级访问权限。现在,您只能为私人页面发布内容,您有这个范围主要是用于测试目的。请参阅 https://developers.tiktok.com/doc/content-sharing-guidelines#direct_post_api_-_developer_guidelines
- 在直接发布视频部分您将看到 '--header 'Content-Type: application/json; charset=UTF-8' ' 但不要介意,我在使用它时遇到了错误,而使用 'Content-Type: application/json' 时则正常工作