tiktok-open-sdk 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/CHANGELOG.md +10 -0
- data/README.md +134 -16
- data/lib/tiktok/open/omniauth/strategies/tiktok_open_sdk.rb +177 -0
- data/lib/tiktok/open/sdk/config.rb +12 -2
- data/lib/tiktok/open/sdk/http_client.rb +1 -1
- data/lib/tiktok/open/sdk/open_api/auth/user.rb +1 -1
- data/lib/tiktok/open/sdk/open_api/post/publish.rb +39 -0
- data/lib/tiktok/open/sdk/version.rb +1 -1
- data/lib/tiktok/open/sdk.rb +42 -0
- metadata +4 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 804836be4e23b6a46c29c263b7b6d802cd2e6a70c61ed02a8f70328715649236
|
|
4
|
+
data.tar.gz: d2dee367fb303f07078a5e07a5a303f163db4895bb3e7468bbdeb2678308e1db
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 860dfd7da6a964785d661f1e6c9b2fd84bb324f1c8511207c0b3f981239b5d361490b8c410f31d0600bd5b4302c21b3b0cc69e370ad80814c1dc24c05b03ab06
|
|
7
|
+
data.tar.gz: 7707f1602f9c088c7d84aff5c20101138fd30e9bfb91569f13062c7e56591361fe4b1ecff06ee6b26b82ee89f362ffb2b5244fb4823c7989b8d9c125df9f9bd2
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [0.4.0] - 2025-10-06
|
|
10
|
+
### Added
|
|
11
|
+
- **OmniAuth Strategy** - Ready-to-use OmniAuth strategy for TikTok Open Platform integration in Rails applications
|
|
12
|
+
- Supports multiple TikTok OAuth scopes (user.info.basic, user.info.profile, user.info.stats)
|
|
13
|
+
- Automatic token handling and user info retrieval
|
|
14
|
+
- Rails/Devise integration examples and callbacks
|
|
15
|
+
- **Post API** - New module for interacting with TikTok's Post API endpoints
|
|
16
|
+
- Creator info query functionality for video publishing workflows
|
|
17
|
+
- Support for querying creator settings and capabilities
|
|
18
|
+
|
|
9
19
|
## [0.3.0] - 2025-09-20
|
|
10
20
|
### Added
|
|
11
21
|
- User information retrieval from TikTok API
|
data/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# TikTok Open SDK
|
|
2
2
|
|
|
3
|
-
[](https://rubygems.org/gems/tiktok-open-sdk)
|
|
4
4
|
[](https://www.ruby-lang.org/en/downloads/)
|
|
5
5
|
[](LICENSE.txt)
|
|
6
6
|
[](https://github.com/pochkuntaras/tiktok-open-sdk/actions/workflows/main.yml)
|
|
@@ -10,9 +10,11 @@ A comprehensive Ruby SDK for integrating with TikTok Open API. This gem provides
|
|
|
10
10
|
## Features
|
|
11
11
|
|
|
12
12
|
- **OAuth 2.0 Authentication** – Seamless OAuth flow for secure integration
|
|
13
|
+
- **OmniAuth Strategy** – Ready-to-use OmniAuth strategy for Rails applications
|
|
13
14
|
- **Client Authentication** – Server-to-server authentication with client credentials
|
|
14
15
|
- **Token Management** – Easy access token exchange and refresh
|
|
15
16
|
- **User API** – Convenient methods to access user information
|
|
17
|
+
- **Post API** – Methods for querying creator information and video publishing
|
|
16
18
|
- **HTTP Client** – Built-in client for interacting with TikTok APIs
|
|
17
19
|
|
|
18
20
|
## Installation
|
|
@@ -56,6 +58,10 @@ Tiktok::Open::Sdk.configure do |config|
|
|
|
56
58
|
config.user_auth.token_url = 'https://open.tiktokapis.com/v2/oauth/token/'
|
|
57
59
|
config.user_auth.revoke_token_url = 'https://open.tiktokapis.com/v2/oauth/revoke/'
|
|
58
60
|
config.user_info_url = 'https://open.tiktokapis.com/v2/user/info/'
|
|
61
|
+
config.creator_info_query_url = 'https://open.tiktokapis.com/v2/post/publish/creator_info/query/'
|
|
62
|
+
|
|
63
|
+
# Optional: Enable OmniAuth strategy auto-loading
|
|
64
|
+
config.load_omniauth = true
|
|
59
65
|
end
|
|
60
66
|
```
|
|
61
67
|
|
|
@@ -158,13 +164,37 @@ end
|
|
|
158
164
|
|
|
159
165
|
**Note:** Client tokens are used for server-to-server authentication and have different scopes and permissions than user tokens.
|
|
160
166
|
|
|
167
|
+
### Using the Post API
|
|
168
|
+
|
|
169
|
+
The SDK provides convenient methods for interacting with TikTok's Post API:
|
|
170
|
+
|
|
171
|
+
#### Creator Info Query
|
|
172
|
+
|
|
173
|
+
Query creator information for video publishing:
|
|
174
|
+
|
|
175
|
+
```ruby
|
|
176
|
+
# Get creator information
|
|
177
|
+
response = Tiktok::Open::Sdk.post.creator_info_query(access_token: access_token)
|
|
178
|
+
|
|
179
|
+
if response[:success]
|
|
180
|
+
creator_data = response[:response][:data]
|
|
181
|
+
|
|
182
|
+
puts "Creator Avatar: #{creator_data[:creator_avatar_url]}"
|
|
183
|
+
puts "Creator Nickname: #{creator_data[:creator_nickname]}"
|
|
184
|
+
puts "Max Video Duration: #{creator_data[:max_video_post_duration_sec]} seconds"
|
|
185
|
+
puts "Privacy Options: #{creator_data[:privacy_level_options]}"
|
|
186
|
+
else
|
|
187
|
+
puts "Error: #{response[:response][:error][:message]}"
|
|
188
|
+
end
|
|
189
|
+
```
|
|
190
|
+
|
|
161
191
|
### Using the User API
|
|
162
192
|
|
|
163
193
|
The SDK provides a convenient way to access user information:
|
|
164
194
|
|
|
165
195
|
```ruby
|
|
166
196
|
# Get user information
|
|
167
|
-
response = Tiktok::Open::Sdk
|
|
197
|
+
response = Tiktok::Open::Sdk.user.get_user_info(
|
|
168
198
|
access_token: access_token,
|
|
169
199
|
fields: %w[open_id union_id avatar_url display_name]
|
|
170
200
|
)
|
|
@@ -187,6 +217,73 @@ Available user fields include:
|
|
|
187
217
|
- `username` - User's username
|
|
188
218
|
- And more (see documentation for full list)
|
|
189
219
|
|
|
220
|
+
### Using OmniAuth Strategy
|
|
221
|
+
|
|
222
|
+
The SDK provides a ready-to-use OmniAuth strategy for Rails applications:
|
|
223
|
+
|
|
224
|
+
#### Rails Setup
|
|
225
|
+
|
|
226
|
+
Add the OmniAuth strategy to your Rails application:
|
|
227
|
+
|
|
228
|
+
```ruby
|
|
229
|
+
# config/initializers/devise.rb
|
|
230
|
+
Devise.setup do |config|
|
|
231
|
+
config.omniauth(
|
|
232
|
+
:tiktok_open_sdk,
|
|
233
|
+
Rails.application.credentials.dig(:tiktok, :client_key),
|
|
234
|
+
Rails.application.credentials.dig(:tiktok, :client_secret),
|
|
235
|
+
scope: 'user.info.basic,user.info.profile,user.info.stats',
|
|
236
|
+
)
|
|
237
|
+
end
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
Or use the SDK configuration:
|
|
241
|
+
|
|
242
|
+
```ruby
|
|
243
|
+
# config/initializers/tiktok_sdk.rb
|
|
244
|
+
Tiktok::Open::Sdk.configure do |config|
|
|
245
|
+
config.client_key = Rails.application.credentials.dig(:tiktok, :client_key)
|
|
246
|
+
config.client_secret = Rails.application.credentials.dig(:tiktok, :client_secret)
|
|
247
|
+
config.user_auth.scopes = %w[user.info.basic video.publish]
|
|
248
|
+
config.user_auth.redirect_uri = "#{Rails.application.config.action_mailer.asset_host.chomp('/')}/users/auth/tiktok_open_sdk/callback"
|
|
249
|
+
config.load_omniauth = true
|
|
250
|
+
end
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
#### OmniAuth Callback
|
|
254
|
+
|
|
255
|
+
Handle the OmniAuth callback in your controller:
|
|
256
|
+
|
|
257
|
+
```ruby
|
|
258
|
+
# app/controllers/omniauth_callbacks_controller.rb
|
|
259
|
+
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
|
|
260
|
+
def tiktok_open_sdk
|
|
261
|
+
@auth = request.env['omniauth.auth']
|
|
262
|
+
|
|
263
|
+
# Find or create user based on TikTok auth data
|
|
264
|
+
@user = User.find_for_oauth(@auth)
|
|
265
|
+
|
|
266
|
+
# ...
|
|
267
|
+
|
|
268
|
+
if @user.persisted?
|
|
269
|
+
sign_in_and_redirect @user, event: :authentication
|
|
270
|
+
set_flash_message :notice, :success, kind: 'TikTok'
|
|
271
|
+
else
|
|
272
|
+
session['devise.provider_data'] = @auth.except('extra')
|
|
273
|
+
redirect_to new_user_registration_url
|
|
274
|
+
end
|
|
275
|
+
end
|
|
276
|
+
end
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
#### Supported Scopes
|
|
280
|
+
|
|
281
|
+
The OmniAuth strategy supports the following TikTok scopes:
|
|
282
|
+
|
|
283
|
+
- `user.info.basic` - Basic user info: open_id, union_id, display_name, avatar URLs
|
|
284
|
+
- `user.info.profile` - Profile info: username, bio_description, profile_deep_link, is_verified
|
|
285
|
+
- `user.info.stats` - Statistics: follower_count, following_count, likes_count, video_count
|
|
286
|
+
|
|
190
287
|
### Using the HTTP Client
|
|
191
288
|
|
|
192
289
|
The SDK includes a flexible HTTP client for making API calls:
|
|
@@ -410,20 +507,41 @@ Retrieves user information from the TikTok Open API.
|
|
|
410
507
|
**Returns:** Hash with `:success`, `:code`, and `:response` keys
|
|
411
508
|
|
|
412
509
|
**Available Fields:**
|
|
413
|
-
- `open_id` -
|
|
414
|
-
- `union_id` -
|
|
415
|
-
- `avatar_url` -
|
|
416
|
-
- `avatar_url_100` -
|
|
417
|
-
- `avatar_large_url` -
|
|
418
|
-
- `display_name` - User's
|
|
419
|
-
- `bio_description` - User's
|
|
420
|
-
- `profile_deep_link` -
|
|
421
|
-
- `is_verified` -
|
|
422
|
-
- `username` - User's username
|
|
423
|
-
- `follower_count` -
|
|
424
|
-
- `following_count` -
|
|
425
|
-
- `likes_count` -
|
|
426
|
-
- `video_count` -
|
|
510
|
+
- `open_id` - Unique identifier for the user within the current application
|
|
511
|
+
- `union_id` - Persistent identifier for the user across different applications from the same developer
|
|
512
|
+
- `avatar_url` - URL to the user's profile image
|
|
513
|
+
- `avatar_url_100` - URL to the user's profile image in 100x100 pixel size
|
|
514
|
+
- `avatar_large_url` - URL to the user's profile image in higher resolution
|
|
515
|
+
- `display_name` - User's display name shown on their TikTok profile
|
|
516
|
+
- `bio_description` - User's biography text (if available)
|
|
517
|
+
- `profile_deep_link` - Direct link to the user's TikTok profile page
|
|
518
|
+
- `is_verified` - Boolean indicating if the account is verified by TikTok
|
|
519
|
+
- `username` - User's unique TikTok username
|
|
520
|
+
- `follower_count` - Number of followers the user has
|
|
521
|
+
- `following_count` - Number of accounts the user is following
|
|
522
|
+
- `likes_count` - Total number of likes received across all user's videos
|
|
523
|
+
- `video_count` - Total number of publicly posted videos by the user
|
|
524
|
+
|
|
525
|
+
### Post API
|
|
526
|
+
|
|
527
|
+
#### `creator_info_query(access_token:)`
|
|
528
|
+
|
|
529
|
+
Queries creator information from the TikTok Open API for video publishing.
|
|
530
|
+
|
|
531
|
+
**Parameters:**
|
|
532
|
+
- `access_token` (String, required) - OAuth2 access token for authentication
|
|
533
|
+
|
|
534
|
+
**Returns:** Hash with `:success`, `:code`, and `:response` keys
|
|
535
|
+
|
|
536
|
+
**Response Data:**
|
|
537
|
+
- `creator_avatar_url` - Creator's avatar URL
|
|
538
|
+
- `creator_nickname` - Creator's display name
|
|
539
|
+
- `creator_username` - Creator's username
|
|
540
|
+
- `stitch_disabled` - Whether stitch is disabled for the creator
|
|
541
|
+
- `comment_disabled` - Whether comments are disabled for the creator
|
|
542
|
+
- `duet_disabled` - Whether duet is disabled for the creator
|
|
543
|
+
- `max_video_post_duration_sec` - Maximum video duration in seconds
|
|
544
|
+
- `privacy_level_options` - Available privacy level options
|
|
427
545
|
|
|
428
546
|
### HTTP Client
|
|
429
547
|
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Tiktok
|
|
4
|
+
module Open
|
|
5
|
+
module Omniauth
|
|
6
|
+
module Strategies
|
|
7
|
+
# OmniAuth strategy for TikTok Open Platform.
|
|
8
|
+
#
|
|
9
|
+
# Integrates TikTok OAuth2 authentication with OmniAuth.
|
|
10
|
+
#
|
|
11
|
+
# @example
|
|
12
|
+
# use OmniAuth::Builder do
|
|
13
|
+
# provider :tiktok_open, 'CLIENT_KEY', 'CLIENT_SECRET'
|
|
14
|
+
# end
|
|
15
|
+
#
|
|
16
|
+
# Supported scopes and their user info fields:
|
|
17
|
+
# - user.info.basic: open_id, union_id, display_name, avatar_url, avatar_url_100, avatar_large_url
|
|
18
|
+
# - user.info.profile: profile_deep_link, bio_description, is_verified, username
|
|
19
|
+
# - user.info.stats: follower_count, following_count, likes_count, video_count
|
|
20
|
+
class TiktokOpenSdk < ::OmniAuth::Strategies::OAuth2
|
|
21
|
+
# Custom access token class for TikTok.
|
|
22
|
+
class AccessToken < ::OAuth2::AccessToken; end
|
|
23
|
+
|
|
24
|
+
# Maps TikTok OAuth scopes to user info fields.
|
|
25
|
+
SCOPE_FIELDS = {
|
|
26
|
+
'user.info.basic' => %w[open_id union_id display_name avatar_url avatar_url_100 avatar_large_url],
|
|
27
|
+
'user.info.profile' => %w[profile_deep_link bio_description is_verified username],
|
|
28
|
+
'user.info.stats' => %w[follower_count following_count likes_count video_count]
|
|
29
|
+
}.freeze
|
|
30
|
+
|
|
31
|
+
private_constant :SCOPE_FIELDS
|
|
32
|
+
|
|
33
|
+
# The name of this OmniAuth strategy.
|
|
34
|
+
option :name, :tiktok_open_sdk
|
|
35
|
+
|
|
36
|
+
# OAuth2 client options for TikTok endpoints.
|
|
37
|
+
# - :site: TikTok Open API base URL
|
|
38
|
+
# - :authorize_url: TikTok OAuth2 authorization endpoint
|
|
39
|
+
# - :token_url: TikTok OAuth2 token endpoint
|
|
40
|
+
option :client_options, {
|
|
41
|
+
site: ::Tiktok::Open::Sdk::Config::OPEN_API_BASE_URL,
|
|
42
|
+
authorize_url: ::Tiktok::Open::Sdk.config.user_auth.auth_url,
|
|
43
|
+
token_url: ::Tiktok::Open::Sdk.config.user_auth.token_url,
|
|
44
|
+
auth_scheme: :request_body,
|
|
45
|
+
auth_token_class: AccessToken
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
# List of parameters allowed in the authorization request.
|
|
49
|
+
option :authorize_options, %i[scope state redirect_uri]
|
|
50
|
+
|
|
51
|
+
# Default scope and redirect_uri from SDK config.
|
|
52
|
+
option :scope, ::Tiktok::Open::Sdk.config.user_auth.scopes.join(',')
|
|
53
|
+
option :redirect_uri, ::Tiktok::Open::Sdk.config.user_auth.redirect_uri
|
|
54
|
+
|
|
55
|
+
# Returns the unique TikTok user ID (open_id).
|
|
56
|
+
#
|
|
57
|
+
# @return [String] TikTok user's open_id.
|
|
58
|
+
uid { raw_info[:open_id].to_s }
|
|
59
|
+
|
|
60
|
+
# Returns a hash of user information.
|
|
61
|
+
#
|
|
62
|
+
# @return [Hash] User info with :name and :image keys, and profile fields if scope is present.
|
|
63
|
+
info do
|
|
64
|
+
{ name: raw_info[:display_name], image: raw_info[:avatar_url_100] }.tap do |info|
|
|
65
|
+
if request_scopes.include?('user.info.profile')
|
|
66
|
+
info.merge!(raw_info.slice(:username, :bio_description, :profile_deep_link))
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Returns extra raw user information from TikTok.
|
|
72
|
+
#
|
|
73
|
+
# @return [Hash] Raw user info data from TikTok API.
|
|
74
|
+
extra { raw_info }
|
|
75
|
+
|
|
76
|
+
# Returns the callback URL without query parameters.
|
|
77
|
+
#
|
|
78
|
+
# @return [String] Callback URL.
|
|
79
|
+
def callback_url
|
|
80
|
+
super.split('?').first
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Builds the access token from TikTok's token endpoint.
|
|
84
|
+
#
|
|
85
|
+
# @raise [OAuth2::Error] if the token response is unsuccessful.
|
|
86
|
+
# @return [AccessToken] OAuth2 access token object.
|
|
87
|
+
def build_access_token
|
|
88
|
+
response = fetch_access_token
|
|
89
|
+
validate_token_response(response)
|
|
90
|
+
create_access_token(response[:response])
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Handles the initial OAuth2 request phase.
|
|
94
|
+
#
|
|
95
|
+
# @raise [ArgumentError] if client_secret is present in params.
|
|
96
|
+
def request_phase
|
|
97
|
+
params = authorize_params.merge('response_type' => 'code')
|
|
98
|
+
|
|
99
|
+
if params.key?(:client_secret) || params.key?('client_secret')
|
|
100
|
+
raise ArgumentError, 'client_secret is not allowed in authorize URL query params'
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
redirect client.authorize_url(params)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Builds the authorization parameters for the OAuth2 request,
|
|
107
|
+
# adding the TikTok client_key.
|
|
108
|
+
#
|
|
109
|
+
# @return [Hash] Authorization parameters.
|
|
110
|
+
def authorize_params
|
|
111
|
+
super.tap do |params|
|
|
112
|
+
params[:client_key] = options.client_id
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
private
|
|
117
|
+
|
|
118
|
+
# Fetches access token from TikTok API
|
|
119
|
+
#
|
|
120
|
+
# @return [Hash] Token response from TikTok
|
|
121
|
+
def fetch_access_token
|
|
122
|
+
Tiktok::Open::Sdk.user_auth.fetch_access_token(
|
|
123
|
+
code: request.params['code'],
|
|
124
|
+
redirect_uri: callback_url
|
|
125
|
+
)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Validates the token response
|
|
129
|
+
#
|
|
130
|
+
# @param response [Hash] Token response
|
|
131
|
+
# @raise [OAuth2::Error] if response is unsuccessful
|
|
132
|
+
def validate_token_response(response)
|
|
133
|
+
raise OAuth2::Error, response[:response] unless response[:success]
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# Creates AccessToken from response data
|
|
137
|
+
#
|
|
138
|
+
# @param data [Hash] Token data from response
|
|
139
|
+
# @return [AccessToken] OAuth2 access token object
|
|
140
|
+
def create_access_token(data)
|
|
141
|
+
AccessToken.from_hash(
|
|
142
|
+
client,
|
|
143
|
+
access_token: data[:access_token],
|
|
144
|
+
refresh_token: data[:refresh_token],
|
|
145
|
+
expires_at: Time.now.to_i + data[:expires_in].to_i
|
|
146
|
+
)
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# Returns the list of requested OAuth scopes.
|
|
150
|
+
#
|
|
151
|
+
# @return [Array<String>] List of scope strings.
|
|
152
|
+
def request_scopes
|
|
153
|
+
@request_scopes ||= request.params.fetch('scopes', 'user.info.basic').split(',')
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# Returns the list of user info fields to request from TikTok,
|
|
157
|
+
# based on the requested scopes.
|
|
158
|
+
#
|
|
159
|
+
# @return [Array<String>] List of user info field names.
|
|
160
|
+
def user_info_fields
|
|
161
|
+
request_scopes.flat_map { |scope| SCOPE_FIELDS[scope] }.compact
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# Fetches and memoizes the raw user info from the TikTok API.
|
|
165
|
+
#
|
|
166
|
+
# @return [Hash] Raw user info data, or empty hash if unavailable.
|
|
167
|
+
def raw_info
|
|
168
|
+
@raw_info ||= Tiktok::Open::Sdk.user.get_user_info(
|
|
169
|
+
access_token: access_token.token,
|
|
170
|
+
fields: user_info_fields
|
|
171
|
+
).dig(:response, :data, :user) || {}
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
end
|
|
@@ -29,14 +29,24 @@ module Tiktok
|
|
|
29
29
|
# @return [String] TikTok user info endpoint URL.
|
|
30
30
|
attr_accessor :user_info_url
|
|
31
31
|
|
|
32
|
+
# @!attribute [rw] creator_info_query_url
|
|
33
|
+
# @return [String] TikTok Query Creator Info endpoint URL.
|
|
34
|
+
attr_accessor :creator_info_query_url
|
|
35
|
+
|
|
32
36
|
# @!attribute [rw] user_auth
|
|
33
37
|
# @return [UserAuth] User authentication configuration.
|
|
34
38
|
attr_accessor :user_auth
|
|
35
39
|
|
|
40
|
+
# @!attribute [rw] load_omniauth
|
|
41
|
+
# @return [Boolean] Whether to automatically load OmniAuth strategy.
|
|
42
|
+
attr_accessor :load_omniauth
|
|
43
|
+
|
|
36
44
|
# Create a new Config with default user authentication settings.
|
|
37
45
|
def initialize
|
|
38
|
-
@user_info_url
|
|
39
|
-
@
|
|
46
|
+
@user_info_url = "#{OPEN_API_BASE_URL}/v2/user/info/"
|
|
47
|
+
@creator_info_query_url = "#{OPEN_API_BASE_URL}/v2/post/publish/creator_info/query/"
|
|
48
|
+
@user_auth = UserAuth.new
|
|
49
|
+
@load_omniauth = false
|
|
40
50
|
end
|
|
41
51
|
|
|
42
52
|
# User authentication configuration for TikTok Open SDK.
|
|
@@ -85,7 +85,7 @@ module Tiktok
|
|
|
85
85
|
case content_type
|
|
86
86
|
when 'application/x-www-form-urlencoded'
|
|
87
87
|
request.set_form_data(body)
|
|
88
|
-
when 'application/json'
|
|
88
|
+
when 'application/json', 'application/json; charset=UTF-8'
|
|
89
89
|
request.body = body.to_json
|
|
90
90
|
else
|
|
91
91
|
raise ArgumentError, "Unsupported content type: #{content_type}"
|
|
@@ -23,7 +23,7 @@ module Tiktok
|
|
|
23
23
|
# @return [URI] The constructed authorization URI.
|
|
24
24
|
def authorization_uri(params = {})
|
|
25
25
|
allowed_params = params.slice(:scope, :redirect_uri, :state)
|
|
26
|
-
uri = URI(Tiktok::Open::Sdk.config.user_auth.auth_url)
|
|
26
|
+
uri = URI.parse(Tiktok::Open::Sdk.config.user_auth.auth_url)
|
|
27
27
|
query_params = authorization_uri_default_params.merge(allowed_params)
|
|
28
28
|
uri.query = URI.encode_www_form(query_params)
|
|
29
29
|
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Provides methods to interact with TikTok Open API post endpoints.
|
|
4
|
+
module Tiktok
|
|
5
|
+
module Open
|
|
6
|
+
module Sdk
|
|
7
|
+
module OpenApi
|
|
8
|
+
module Post
|
|
9
|
+
# Provides methods for handling TikTok Open API post endpoints.
|
|
10
|
+
module Publish
|
|
11
|
+
extend self
|
|
12
|
+
|
|
13
|
+
include ::Tiktok::Open::Sdk::Helpers::ResponseHelper
|
|
14
|
+
include ::Tiktok::Open::Sdk::Helpers::Validators::TokenValidator
|
|
15
|
+
|
|
16
|
+
# Queries creator information from the TikTok Open API.
|
|
17
|
+
#
|
|
18
|
+
# @param access_token [String] OAuth2 access token for authentication.
|
|
19
|
+
# @return [Hash] Parsed API response containing creator information.
|
|
20
|
+
# @raise [::Tiktok::Open::Sdk::RequestValidationError] If the access token is invalid.
|
|
21
|
+
#
|
|
22
|
+
# @example
|
|
23
|
+
# Tiktok::Open::Sdk.post.creator_info_query(access_token: 'your_access_token')
|
|
24
|
+
def creator_info_query(access_token:)
|
|
25
|
+
validate_token!(access_token)
|
|
26
|
+
|
|
27
|
+
render_response Tiktok::Open::Sdk::HttpClient.post(
|
|
28
|
+
Tiktok::Open::Sdk.config.creator_info_query_url,
|
|
29
|
+
headers: {
|
|
30
|
+
Authorization: "Bearer #{access_token}"
|
|
31
|
+
}
|
|
32
|
+
)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
data/lib/tiktok/open/sdk.rb
CHANGED
|
@@ -6,6 +6,7 @@ require_relative 'sdk/helpers/auth_helper'
|
|
|
6
6
|
require_relative 'sdk/helpers/validators/token_validator'
|
|
7
7
|
require_relative 'sdk/open_api/auth/user'
|
|
8
8
|
require_relative 'sdk/open_api/auth/client'
|
|
9
|
+
require_relative 'sdk/open_api/post/publish'
|
|
9
10
|
require_relative 'sdk/open_api/user'
|
|
10
11
|
require_relative 'sdk/version'
|
|
11
12
|
require_relative 'sdk/http_client'
|
|
@@ -45,12 +46,15 @@ module Tiktok
|
|
|
45
46
|
# config.user_auth.token_url = 'https://open.tiktokapis.com/v2/oauth/token/'
|
|
46
47
|
# config.user_auth.scopes = %w[user.info.basic video.list]
|
|
47
48
|
# config.user_auth.redirect_uri = 'https://your-redirect-uri.example.com'
|
|
49
|
+
# config.load_omniauth = true
|
|
48
50
|
# end
|
|
49
51
|
def configure
|
|
50
52
|
self.config ||= Config.new
|
|
51
53
|
|
|
52
54
|
yield(config)
|
|
53
55
|
|
|
56
|
+
load_omniauth! if config.load_omniauth
|
|
57
|
+
|
|
54
58
|
config
|
|
55
59
|
end
|
|
56
60
|
|
|
@@ -73,6 +77,44 @@ module Tiktok
|
|
|
73
77
|
def client_auth
|
|
74
78
|
OpenApi::Auth::Client
|
|
75
79
|
end
|
|
80
|
+
|
|
81
|
+
# Convenience accessor for post publish functionality
|
|
82
|
+
#
|
|
83
|
+
# @return [OpenApi::Post::Publish] the Post publish module
|
|
84
|
+
#
|
|
85
|
+
# @example
|
|
86
|
+
# Tiktok::Open::Sdk.post.video_init(access_token: 'token', post_info: post_info, source_info: source_info)
|
|
87
|
+
def post
|
|
88
|
+
OpenApi::Post::Publish
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Convenience accessor for user functionality
|
|
92
|
+
#
|
|
93
|
+
# @return [OpenApi::User] the User module
|
|
94
|
+
#
|
|
95
|
+
# @example
|
|
96
|
+
# Tiktok::Open::Sdk.user.info(access_token: 'token')
|
|
97
|
+
def user
|
|
98
|
+
OpenApi::User
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
private
|
|
102
|
+
|
|
103
|
+
# Loads the OmniAuth strategy for TikTok Open Platform.
|
|
104
|
+
#
|
|
105
|
+
# Attempts to require the necessary OmniAuth dependencies for TikTok Open integration.
|
|
106
|
+
# Raises an error if the required gems are not available.
|
|
107
|
+
#
|
|
108
|
+
# @raise [Tiktok::Open::Sdk::Error] if 'omniauth-oauth2' or the TikTok strategy cannot be loaded
|
|
109
|
+
# @example
|
|
110
|
+
# Tiktok::Open::Sdk.load_omniauth!
|
|
111
|
+
def load_omniauth!
|
|
112
|
+
require 'omniauth-oauth2'
|
|
113
|
+
require 'tiktok/open/omniauth/strategies/tiktok_open_sdk'
|
|
114
|
+
rescue LoadError => e
|
|
115
|
+
raise ::Tiktok::Open::Sdk::Error,
|
|
116
|
+
"OmniAuth is not loaded! Error: #{e.message}"
|
|
117
|
+
end
|
|
76
118
|
end
|
|
77
119
|
end
|
|
78
120
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: tiktok-open-sdk
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Taras Pochkun
|
|
@@ -26,6 +26,7 @@ files:
|
|
|
26
26
|
- LICENSE.txt
|
|
27
27
|
- README.md
|
|
28
28
|
- Rakefile
|
|
29
|
+
- lib/tiktok/open/omniauth/strategies/tiktok_open_sdk.rb
|
|
29
30
|
- lib/tiktok/open/sdk.rb
|
|
30
31
|
- lib/tiktok/open/sdk/config.rb
|
|
31
32
|
- lib/tiktok/open/sdk/helpers/auth_helper.rb
|
|
@@ -35,6 +36,7 @@ files:
|
|
|
35
36
|
- lib/tiktok/open/sdk/http_client.rb
|
|
36
37
|
- lib/tiktok/open/sdk/open_api/auth/client.rb
|
|
37
38
|
- lib/tiktok/open/sdk/open_api/auth/user.rb
|
|
39
|
+
- lib/tiktok/open/sdk/open_api/post/publish.rb
|
|
38
40
|
- lib/tiktok/open/sdk/open_api/user.rb
|
|
39
41
|
- lib/tiktok/open/sdk/version.rb
|
|
40
42
|
- sig/tiktok/open/sdk.rbs
|
|
@@ -44,7 +46,7 @@ licenses:
|
|
|
44
46
|
metadata:
|
|
45
47
|
allowed_push_host: https://rubygems.org
|
|
46
48
|
homepage_uri: https://github.com/pochkuntaras/tiktok-open-sdk
|
|
47
|
-
source_code_uri: https://github.com/pochkuntaras/tiktok-open-sdk
|
|
49
|
+
source_code_uri: https://github.com/pochkuntaras/tiktok-open-sdk.git
|
|
48
50
|
changelog_uri: https://github.com/pochkuntaras/tiktok-open-sdk/blob/main/CHANGELOG.md
|
|
49
51
|
rubygems_mfa_required: 'true'
|
|
50
52
|
documentation_uri: https://rubydoc.info/gems/tiktok-open-sdk
|