tiktok-open-sdk 0.2.0 → 0.3.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/.yardopts +12 -0
- data/CHANGELOG.md +5 -0
- data/README.md +97 -2
- data/lib/tiktok/open/sdk/config.rb +32 -15
- data/lib/tiktok/open/sdk/helpers/auth_helper.rb +56 -0
- data/lib/tiktok/open/sdk/helpers/response_helper.rb +29 -0
- data/lib/tiktok/open/sdk/helpers/string_utils_helper.rb +24 -0
- data/lib/tiktok/open/sdk/helpers/validators/token_validator.rb +38 -0
- data/lib/tiktok/open/sdk/http_client.rb +10 -2
- data/lib/tiktok/open/sdk/open_api/auth/client.rb +2 -3
- data/lib/tiktok/open/sdk/open_api/auth/user.rb +2 -1
- data/lib/tiktok/open/sdk/open_api/user.rb +74 -0
- data/lib/tiktok/open/sdk/version.rb +1 -1
- data/lib/tiktok/open/sdk.rb +7 -3
- metadata +8 -3
- data/lib/tiktok/open/sdk/open_api/auth/helpers.rb +0 -73
- data/lib/tiktok/open/sdk/string_utils.rb +0 -24
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5baf485e6b6be4c1d0541f8d8d3ff76232bba37253fe7e6d89a6a9359328dc04
|
|
4
|
+
data.tar.gz: 398eb5072ac7be7fd647679f408ecfa94fce22ff44c4993bf1891637d7cc6150
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e3ade7fbfaaea010568b79c807b52d65fa4398f486889387ab07a01db54163021a410d77a76552f865599b32db2d40cd89c19df11964ebc83a19022d163887d3
|
|
7
|
+
data.tar.gz: b512cb261c7b577a74523b501a25daddce2e1196f90ebc9351cf1472516f33fb1634bd1b501bd04649cdb10c44c9c97513fd0a0474de533bba59f8e1eb15c530
|
data/.yardopts
ADDED
data/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [0.3.0] - 2025-09-20
|
|
10
|
+
### Added
|
|
11
|
+
- User information retrieval from TikTok API
|
|
12
|
+
- Improved request validation and error handling
|
|
13
|
+
|
|
9
14
|
## [0.2.0] - 2025-09-17
|
|
10
15
|
### Added
|
|
11
16
|
- Support for obtaining a client access token directly from the TikTok Open API, enabling secure server-to-server authentication for backend integrations and service authentication flows.
|
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)
|
|
@@ -12,6 +12,7 @@ A comprehensive Ruby SDK for integrating with TikTok Open API. This gem provides
|
|
|
12
12
|
- **OAuth 2.0 Authentication** – Seamless OAuth flow for secure integration
|
|
13
13
|
- **Client Authentication** – Server-to-server authentication with client credentials
|
|
14
14
|
- **Token Management** – Easy access token exchange and refresh
|
|
15
|
+
- **User API** – Convenient methods to access user information
|
|
15
16
|
- **HTTP Client** – Built-in client for interacting with TikTok APIs
|
|
16
17
|
|
|
17
18
|
## Installation
|
|
@@ -54,6 +55,7 @@ Tiktok::Open::Sdk.configure do |config|
|
|
|
54
55
|
config.user_auth.auth_url = 'https://www.tiktok.com/v2/auth/authorize/'
|
|
55
56
|
config.user_auth.token_url = 'https://open.tiktokapis.com/v2/oauth/token/'
|
|
56
57
|
config.user_auth.revoke_token_url = 'https://open.tiktokapis.com/v2/oauth/revoke/'
|
|
58
|
+
config.user_info_url = 'https://open.tiktokapis.com/v2/user/info/'
|
|
57
59
|
end
|
|
58
60
|
```
|
|
59
61
|
|
|
@@ -156,12 +158,41 @@ end
|
|
|
156
158
|
|
|
157
159
|
**Note:** Client tokens are used for server-to-server authentication and have different scopes and permissions than user tokens.
|
|
158
160
|
|
|
161
|
+
### Using the User API
|
|
162
|
+
|
|
163
|
+
The SDK provides a convenient way to access user information:
|
|
164
|
+
|
|
165
|
+
```ruby
|
|
166
|
+
# Get user information
|
|
167
|
+
response = Tiktok::Open::Sdk::OpenApi::User.get_user_info(
|
|
168
|
+
access_token: access_token,
|
|
169
|
+
fields: %w[open_id union_id avatar_url display_name]
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
if response[:success]
|
|
173
|
+
user_data = response[:response][:data][:user]
|
|
174
|
+
|
|
175
|
+
puts "User ID: #{user_data[:open_id]}"
|
|
176
|
+
puts "Display Name: #{user_data[:display_name]}"
|
|
177
|
+
else
|
|
178
|
+
puts "Error: #{response[:response][:error][:message]}"
|
|
179
|
+
end
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Available user fields include:
|
|
183
|
+
- `open_id` - User's Open ID
|
|
184
|
+
- `union_id` - User's Union ID
|
|
185
|
+
- `avatar_url` - User's avatar URL
|
|
186
|
+
- `display_name` - User's display name
|
|
187
|
+
- `username` - User's username
|
|
188
|
+
- And more (see documentation for full list)
|
|
189
|
+
|
|
159
190
|
### Using the HTTP Client
|
|
160
191
|
|
|
161
192
|
The SDK includes a flexible HTTP client for making API calls:
|
|
162
193
|
|
|
163
194
|
```ruby
|
|
164
|
-
# GET request
|
|
195
|
+
# GET request using request method
|
|
165
196
|
response = Tiktok::Open::Sdk::HttpClient.request(
|
|
166
197
|
:get,
|
|
167
198
|
'https://open.tiktokapis.com/v2/user/info/',
|
|
@@ -173,6 +204,17 @@ response = Tiktok::Open::Sdk::HttpClient.request(
|
|
|
173
204
|
}
|
|
174
205
|
)
|
|
175
206
|
|
|
207
|
+
# GET request using get method
|
|
208
|
+
response = Tiktok::Open::Sdk::HttpClient.get(
|
|
209
|
+
'https://open.tiktokapis.com/v2/user/info/',
|
|
210
|
+
params: {
|
|
211
|
+
fields: 'open_id,union_id,avatar_url'
|
|
212
|
+
},
|
|
213
|
+
headers: {
|
|
214
|
+
'Authorization' => "Bearer #{access_token}"
|
|
215
|
+
}
|
|
216
|
+
)
|
|
217
|
+
|
|
176
218
|
# POST request
|
|
177
219
|
response = Tiktok::Open::Sdk::HttpClient.post(
|
|
178
220
|
'https://open.tiktokapis.com/v2/video/list/',
|
|
@@ -241,6 +283,18 @@ class TiktokAuthController < ApplicationController
|
|
|
241
283
|
session[:tiktok_access_token] = token_data[:access_token]
|
|
242
284
|
session[:tiktok_refresh_token] = token_data[:refresh_token]
|
|
243
285
|
|
|
286
|
+
# Fetch user information
|
|
287
|
+
user_response = Tiktok::Open::Sdk::OpenApi::User.get_user_info(
|
|
288
|
+
access_token: token_data[:access_token],
|
|
289
|
+
fields: %w[open_id display_name avatar_url]
|
|
290
|
+
)
|
|
291
|
+
|
|
292
|
+
if user_response[:success]
|
|
293
|
+
user_data = user_response[:response][:data][:user]
|
|
294
|
+
session[:tiktok_user_name] = user_data[:display_name]
|
|
295
|
+
session[:tiktok_avatar] = user_data[:avatar_url]
|
|
296
|
+
end
|
|
297
|
+
|
|
244
298
|
redirect_to dashboard_path, notice: 'Successfully connected to TikTok!'
|
|
245
299
|
else
|
|
246
300
|
redirect_to root_path, alert: 'Failed to authenticate with TikTok'
|
|
@@ -276,6 +330,7 @@ Tiktok::Open::Sdk.configure do |config|
|
|
|
276
330
|
config.client_secret = 'your_client_secret' # Required
|
|
277
331
|
config.user_auth.scopes = %w[user.info.basic] # Optional
|
|
278
332
|
config.user_auth.redirect_uri = 'https://...' # Optional
|
|
333
|
+
config.user_info_url = 'https://open.tiktokapis.com/v2/user/info/' # Optional
|
|
279
334
|
end
|
|
280
335
|
```
|
|
281
336
|
|
|
@@ -341,6 +396,35 @@ Obtains a client access token for server-to-server authentication.
|
|
|
341
396
|
}
|
|
342
397
|
```
|
|
343
398
|
|
|
399
|
+
### User API
|
|
400
|
+
|
|
401
|
+
#### `get_user_info(access_token:, fields:, validate: true)`
|
|
402
|
+
|
|
403
|
+
Retrieves user information from the TikTok Open API.
|
|
404
|
+
|
|
405
|
+
**Parameters:**
|
|
406
|
+
- `access_token` (String, required) - OAuth2 access token for authentication
|
|
407
|
+
- `fields` (Array<String>, required) - User fields to retrieve (must be valid fields)
|
|
408
|
+
- `validate` (Boolean, optional) - Whether to validate the token and fields (default: true)
|
|
409
|
+
|
|
410
|
+
**Returns:** Hash with `:success`, `:code`, and `:response` keys
|
|
411
|
+
|
|
412
|
+
**Available Fields:**
|
|
413
|
+
- `open_id` - The unique identification of the user in the current application.Open id for the client
|
|
414
|
+
- `union_id` - The unique identification of the user across different apps for the same developer. For example, if a partner has X number of clients, it will get X number of open_id for the same TikTok user, but one persistent union_id for the particular user
|
|
415
|
+
- `avatar_url` - User's profile image
|
|
416
|
+
- `avatar_url_100` - User`s profile image in 100x100 size
|
|
417
|
+
- `avatar_large_url` - User's profile image with higher resolution
|
|
418
|
+
- `display_name` - User's profile name
|
|
419
|
+
- `bio_description` - User's bio description if there is a valid one
|
|
420
|
+
- `profile_deep_link` - The link to user's TikTok profile page
|
|
421
|
+
- `is_verified` - Whether TikTok has provided a verified badge to the account after confirming that it belongs to the user it represents
|
|
422
|
+
- `username` - User's username
|
|
423
|
+
- `follower_count` - User's followers count
|
|
424
|
+
- `following_count` - The number of accounts that the user is following
|
|
425
|
+
- `likes_count` - The total number of likes received by the user across all of their videos
|
|
426
|
+
- `video_count` - The total number of publicly posted videos by the user
|
|
427
|
+
|
|
344
428
|
### HTTP Client
|
|
345
429
|
|
|
346
430
|
#### `request(method, url, params: {}, headers: {}, body: nil)`
|
|
@@ -356,6 +440,17 @@ Performs HTTP requests.
|
|
|
356
440
|
|
|
357
441
|
**Returns:** `Net::HTTPResponse` object
|
|
358
442
|
|
|
443
|
+
#### `get(url, params: {}, headers: {})`
|
|
444
|
+
|
|
445
|
+
Convenience method for GET requests.
|
|
446
|
+
|
|
447
|
+
**Parameters:**
|
|
448
|
+
- `url` (String) - Request URL
|
|
449
|
+
- `params` (Hash, optional) - Query parameters
|
|
450
|
+
- `headers` (Hash, optional) - HTTP headers
|
|
451
|
+
|
|
452
|
+
**Returns:** `Net::HTTPResponse` object
|
|
453
|
+
|
|
359
454
|
#### `post(url, params: {}, headers: {}, body: nil)`
|
|
360
455
|
|
|
361
456
|
Convenience method for POST requests.
|
|
@@ -13,44 +13,61 @@ module Tiktok
|
|
|
13
13
|
# config.client_secret = 'your_secret'
|
|
14
14
|
# config.user_auth.scopes = %w[user.info.basic]
|
|
15
15
|
class Config
|
|
16
|
-
#
|
|
16
|
+
# Base domains for constructing TikTok API URLs.
|
|
17
|
+
AUTH_BASE_URL = 'https://www.tiktok.com'
|
|
18
|
+
OPEN_API_BASE_URL = 'https://open.tiktokapis.com'
|
|
19
|
+
|
|
20
|
+
# @!attribute [rw] client_key
|
|
21
|
+
# @return [String] TikTok client key.
|
|
17
22
|
attr_accessor :client_key
|
|
18
23
|
|
|
19
|
-
#
|
|
24
|
+
# @!attribute [rw] client_secret
|
|
25
|
+
# @return [String] TikTok client secret.
|
|
20
26
|
attr_accessor :client_secret
|
|
21
27
|
|
|
22
|
-
#
|
|
28
|
+
# @!attribute [rw] user_info_url
|
|
29
|
+
# @return [String] TikTok user info endpoint URL.
|
|
30
|
+
attr_accessor :user_info_url
|
|
31
|
+
|
|
32
|
+
# @!attribute [rw] user_auth
|
|
33
|
+
# @return [UserAuth] User authentication configuration.
|
|
23
34
|
attr_accessor :user_auth
|
|
24
35
|
|
|
25
|
-
#
|
|
36
|
+
# Create a new Config with default user authentication settings.
|
|
26
37
|
def initialize
|
|
27
|
-
@
|
|
38
|
+
@user_info_url = "#{OPEN_API_BASE_URL}/v2/user/info/"
|
|
39
|
+
@user_auth = UserAuth.new
|
|
28
40
|
end
|
|
29
41
|
|
|
30
42
|
# User authentication configuration for TikTok Open SDK.
|
|
31
43
|
#
|
|
32
44
|
# Holds OAuth URLs, scopes, and redirect URI.
|
|
33
45
|
class UserAuth
|
|
34
|
-
#
|
|
46
|
+
# @!attribute [rw] auth_url
|
|
47
|
+
# @return [String] OAuth authorization URL.
|
|
35
48
|
attr_accessor :auth_url
|
|
36
49
|
|
|
37
|
-
#
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
# @return [String] The OAuth token revoke URL.
|
|
50
|
+
# @!attribute [rw] revoke_token_url
|
|
51
|
+
# @return [String] OAuth token revocation URL.
|
|
41
52
|
attr_accessor :revoke_token_url
|
|
42
53
|
|
|
43
|
-
#
|
|
54
|
+
# @!attribute [rw] token_url
|
|
55
|
+
# @return [String] OAuth token exchange URL.
|
|
56
|
+
attr_accessor :token_url
|
|
57
|
+
|
|
58
|
+
# @!attribute [rw] scopes
|
|
59
|
+
# @return [Array<String>] List of OAuth scopes.
|
|
44
60
|
attr_accessor :scopes
|
|
45
61
|
|
|
46
|
-
#
|
|
62
|
+
# @!attribute [rw] redirect_uri
|
|
63
|
+
# @return [String, nil] OAuth redirect URI.
|
|
47
64
|
attr_accessor :redirect_uri
|
|
48
65
|
|
|
49
66
|
# Initializes a new UserAuth object with default URLs and empty scopes.
|
|
50
67
|
def initialize
|
|
51
|
-
@auth_url =
|
|
52
|
-
@revoke_token_url =
|
|
53
|
-
@token_url =
|
|
68
|
+
@auth_url = "#{AUTH_BASE_URL}/v2/auth/authorize/"
|
|
69
|
+
@revoke_token_url = "#{OPEN_API_BASE_URL}/v2/oauth/revoke/"
|
|
70
|
+
@token_url = "#{OPEN_API_BASE_URL}/v2/oauth/token/"
|
|
54
71
|
@scopes = []
|
|
55
72
|
@redirect_uri = nil
|
|
56
73
|
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
|
|
5
|
+
module Tiktok
|
|
6
|
+
module Open
|
|
7
|
+
module Sdk
|
|
8
|
+
module Helpers
|
|
9
|
+
# Shared authentication helper methods for TikTok Open SDK auth modules.
|
|
10
|
+
module AuthHelper
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
# Returns the HTTP headers for requests.
|
|
14
|
+
#
|
|
15
|
+
# @return [Hash] The headers for HTTP requests.
|
|
16
|
+
def headers
|
|
17
|
+
{
|
|
18
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
19
|
+
'Cache-Control': 'no-cache'
|
|
20
|
+
}
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Returns the client credentials for authentication.
|
|
24
|
+
#
|
|
25
|
+
# @return [Hash] The client credentials.
|
|
26
|
+
def credentials
|
|
27
|
+
{
|
|
28
|
+
client_key: Tiktok::Open::Sdk.config.client_key,
|
|
29
|
+
client_secret: Tiktok::Open::Sdk.config.client_secret
|
|
30
|
+
}
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Returns the default query parameters for the authorization URI.
|
|
34
|
+
#
|
|
35
|
+
# @return [Hash] The default query parameters:
|
|
36
|
+
# - :client_key [String] The TikTok client key.
|
|
37
|
+
# - :response_type [String] Always 'code'.
|
|
38
|
+
# - :scope [String] Comma-separated scopes.
|
|
39
|
+
# - :redirect_uri [String] The redirect URI.
|
|
40
|
+
# - :state [nil] Default state is nil.
|
|
41
|
+
def authorization_uri_default_params
|
|
42
|
+
{
|
|
43
|
+
client_key: Tiktok::Open::Sdk.config.client_key,
|
|
44
|
+
response_type: 'code',
|
|
45
|
+
scope: Tiktok::Open::Sdk.config.user_auth.scopes.join(','),
|
|
46
|
+
redirect_uri: Tiktok::Open::Sdk.config.user_auth.redirect_uri,
|
|
47
|
+
state: nil
|
|
48
|
+
}
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# render_response moved to ::Tiktok::Open::Sdk::ResponseHelpers
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Tiktok
|
|
4
|
+
module Open
|
|
5
|
+
module Sdk
|
|
6
|
+
module Helpers
|
|
7
|
+
# Helper methods for formatting HTTP responses across the SDK.
|
|
8
|
+
module ResponseHelper
|
|
9
|
+
include ::Tiktok::Open::Sdk::Helpers::StringUtilsHelper
|
|
10
|
+
|
|
11
|
+
# Parses and formats the HTTP response.
|
|
12
|
+
#
|
|
13
|
+
# @param response [Net::HTTPResponse] The HTTP response object.
|
|
14
|
+
# @return [Hash] The formatted response with keys:
|
|
15
|
+
# - :success [Boolean] Whether the response is a Net::HTTPSuccess.
|
|
16
|
+
# - :code [Integer] HTTP status code.
|
|
17
|
+
# - :response [Hash] Parsed JSON body or a hash with the raw string if parsing fails.
|
|
18
|
+
def render_response(response)
|
|
19
|
+
{
|
|
20
|
+
success: response.is_a?(Net::HTTPSuccess),
|
|
21
|
+
code: response.code.to_i,
|
|
22
|
+
response: parse_json(response.body)
|
|
23
|
+
}
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
|
|
5
|
+
module Tiktok
|
|
6
|
+
module Open
|
|
7
|
+
module Sdk
|
|
8
|
+
module Helpers
|
|
9
|
+
# Utility methods for string and JSON handling.
|
|
10
|
+
module StringUtilsHelper
|
|
11
|
+
# Parses a JSON string into a Ruby hash with symbolized keys.
|
|
12
|
+
#
|
|
13
|
+
# @param str [String] JSON string to parse.
|
|
14
|
+
# @return [Hash] Parsed hash with symbolized keys, or a hash with the raw string if parsing fails.
|
|
15
|
+
def parse_json(str)
|
|
16
|
+
JSON.parse(str, symbolize_names: true)
|
|
17
|
+
rescue JSON::ParserError
|
|
18
|
+
{ raw: str }
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Tiktok
|
|
4
|
+
module Open
|
|
5
|
+
module Sdk
|
|
6
|
+
module Helpers
|
|
7
|
+
module Validators
|
|
8
|
+
# Provides token validation methods for TikTok Open SDK.
|
|
9
|
+
module TokenValidator
|
|
10
|
+
# Regular expression to validate tokens.
|
|
11
|
+
# Ensures the token consists of at least 10 printable characters.
|
|
12
|
+
TOKEN_REGEX = /\A[[:print:]]{10,}\z/
|
|
13
|
+
|
|
14
|
+
# Checks if the given token is a valid string of at least 10 printable characters.
|
|
15
|
+
#
|
|
16
|
+
# @param token [String] The token to check.
|
|
17
|
+
# @return [Boolean] true if the token is valid, false otherwise.
|
|
18
|
+
def valid_token?(token)
|
|
19
|
+
token.is_a?(String) && !token.empty? && TOKEN_REGEX.match?(token)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Validates the given token and raises an error if it is invalid.
|
|
23
|
+
#
|
|
24
|
+
# @param token [String] The token to validate.
|
|
25
|
+
# @raise [::Tiktok::Open::Sdk::RequestValidationError] if the token is invalid.
|
|
26
|
+
# @return [void]
|
|
27
|
+
def validate_token!(token)
|
|
28
|
+
return if valid_token?(token)
|
|
29
|
+
|
|
30
|
+
raise ::Tiktok::Open::Sdk::RequestValidationError,
|
|
31
|
+
'Invalid token format: must be at least 10 printable characters.'
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -13,8 +13,6 @@ module Tiktok
|
|
|
13
13
|
module HttpClient
|
|
14
14
|
extend self
|
|
15
15
|
|
|
16
|
-
include StringUtils
|
|
17
|
-
|
|
18
16
|
# Supported HTTP methods.
|
|
19
17
|
SUPPORTED_METHODS = %i[get post].freeze
|
|
20
18
|
|
|
@@ -53,6 +51,16 @@ module Tiktok
|
|
|
53
51
|
request(:post, url, params: params, headers: headers, body: body)
|
|
54
52
|
end
|
|
55
53
|
|
|
54
|
+
# Performs a GET HTTP request.
|
|
55
|
+
#
|
|
56
|
+
# @param url [String] The request URL.
|
|
57
|
+
# @param params [Hash] Query parameters.
|
|
58
|
+
# @param headers [Hash] HTTP headers.
|
|
59
|
+
# @return [Net::HTTPResponse] The HTTP response object.
|
|
60
|
+
def get(url, params: {}, headers: {})
|
|
61
|
+
request(:get, url, params: params, headers: headers)
|
|
62
|
+
end
|
|
63
|
+
|
|
56
64
|
private
|
|
57
65
|
|
|
58
66
|
# Ensures the HTTP method is supported.
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'uri'
|
|
4
|
-
|
|
5
3
|
module Tiktok
|
|
6
4
|
module Open
|
|
7
5
|
module Sdk
|
|
@@ -11,7 +9,8 @@ module Tiktok
|
|
|
11
9
|
module Client
|
|
12
10
|
extend self
|
|
13
11
|
|
|
14
|
-
include Helpers
|
|
12
|
+
include ::Tiktok::Open::Sdk::Helpers::AuthHelper
|
|
13
|
+
include ::Tiktok::Open::Sdk::Helpers::ResponseHelper
|
|
15
14
|
|
|
16
15
|
# Fetches a client access token from the TikTok Open API.
|
|
17
16
|
#
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Provides methods to interact with TikTok Open API user endpoints.
|
|
4
|
+
module Tiktok
|
|
5
|
+
module Open
|
|
6
|
+
module Sdk
|
|
7
|
+
module OpenApi
|
|
8
|
+
# Provides user-related methods for the TikTok Open API.
|
|
9
|
+
module User
|
|
10
|
+
# List of valid user info fields that can be requested from the API.
|
|
11
|
+
FIELDS = %w[
|
|
12
|
+
open_id
|
|
13
|
+
union_id
|
|
14
|
+
avatar_url
|
|
15
|
+
avatar_url_100
|
|
16
|
+
avatar_large_url
|
|
17
|
+
display_name
|
|
18
|
+
bio_description
|
|
19
|
+
profile_deep_link
|
|
20
|
+
is_verified
|
|
21
|
+
username
|
|
22
|
+
follower_count
|
|
23
|
+
following_count
|
|
24
|
+
likes_count
|
|
25
|
+
video_count
|
|
26
|
+
].freeze
|
|
27
|
+
|
|
28
|
+
extend self
|
|
29
|
+
|
|
30
|
+
include ::Tiktok::Open::Sdk::Helpers::ResponseHelper
|
|
31
|
+
include ::Tiktok::Open::Sdk::Helpers::Validators::TokenValidator
|
|
32
|
+
|
|
33
|
+
# Retrieves user information from the TikTok Open API.
|
|
34
|
+
#
|
|
35
|
+
# @param access_token [String] OAuth2 access token for authentication.
|
|
36
|
+
# @param fields [Array<String>] User fields to retrieve. Must be a subset of FIELDS.
|
|
37
|
+
# @param validate [Boolean] Whether to validate the token and fields. Defaults to true.
|
|
38
|
+
# @return [Hash] Parsed API response containing user information.
|
|
39
|
+
# @raise [::Tiktok::Open::Sdk::RequestValidationError] If the access token or any requested field is invalid.
|
|
40
|
+
def get_user_info(access_token:, fields:, validate: true)
|
|
41
|
+
if validate
|
|
42
|
+
validate_token!(access_token)
|
|
43
|
+
validate_fields!(fields)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
render_response Tiktok::Open::Sdk::HttpClient.get(
|
|
47
|
+
Tiktok::Open::Sdk.config.user_info_url,
|
|
48
|
+
params: {
|
|
49
|
+
fields: fields.join(',')
|
|
50
|
+
},
|
|
51
|
+
headers: {
|
|
52
|
+
Authorization: "Bearer #{access_token}"
|
|
53
|
+
}
|
|
54
|
+
)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
# Ensures all requested fields are supported by the API.
|
|
60
|
+
#
|
|
61
|
+
# @param fields [Array<String>] Fields to validate against FIELDS.
|
|
62
|
+
# @raise [::Tiktok::Open::Sdk::RequestValidationError] If any field is not supported.
|
|
63
|
+
def validate_fields!(fields)
|
|
64
|
+
invalid = fields - FIELDS
|
|
65
|
+
|
|
66
|
+
return if invalid.empty?
|
|
67
|
+
|
|
68
|
+
raise ::Tiktok::Open::Sdk::RequestValidationError, "Invalid fields: #{invalid.join(", ")}"
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
data/lib/tiktok/open/sdk.rb
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require_relative 'sdk/
|
|
4
|
-
|
|
5
|
-
require_relative 'sdk/
|
|
3
|
+
require_relative 'sdk/helpers/string_utils_helper'
|
|
4
|
+
require_relative 'sdk/helpers/response_helper'
|
|
5
|
+
require_relative 'sdk/helpers/auth_helper'
|
|
6
|
+
require_relative 'sdk/helpers/validators/token_validator'
|
|
6
7
|
require_relative 'sdk/open_api/auth/user'
|
|
7
8
|
require_relative 'sdk/open_api/auth/client'
|
|
9
|
+
require_relative 'sdk/open_api/user'
|
|
8
10
|
require_relative 'sdk/version'
|
|
9
11
|
require_relative 'sdk/http_client'
|
|
10
12
|
require_relative 'sdk/config'
|
|
@@ -19,6 +21,8 @@ module Tiktok
|
|
|
19
21
|
# raise Tiktok::Open::Sdk::Error, "Something went wrong"
|
|
20
22
|
class Error < StandardError; end
|
|
21
23
|
|
|
24
|
+
class RequestValidationError < Error; end
|
|
25
|
+
|
|
22
26
|
class << self
|
|
23
27
|
# SDK configuration object
|
|
24
28
|
#
|
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.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Taras Pochkun
|
|
@@ -20,6 +20,7 @@ files:
|
|
|
20
20
|
- ".rspec"
|
|
21
21
|
- ".rubocop.yml"
|
|
22
22
|
- ".tool-versions"
|
|
23
|
+
- ".yardopts"
|
|
23
24
|
- CHANGELOG.md
|
|
24
25
|
- CODE_OF_CONDUCT.md
|
|
25
26
|
- LICENSE.txt
|
|
@@ -27,11 +28,14 @@ files:
|
|
|
27
28
|
- Rakefile
|
|
28
29
|
- lib/tiktok/open/sdk.rb
|
|
29
30
|
- lib/tiktok/open/sdk/config.rb
|
|
31
|
+
- lib/tiktok/open/sdk/helpers/auth_helper.rb
|
|
32
|
+
- lib/tiktok/open/sdk/helpers/response_helper.rb
|
|
33
|
+
- lib/tiktok/open/sdk/helpers/string_utils_helper.rb
|
|
34
|
+
- lib/tiktok/open/sdk/helpers/validators/token_validator.rb
|
|
30
35
|
- lib/tiktok/open/sdk/http_client.rb
|
|
31
36
|
- lib/tiktok/open/sdk/open_api/auth/client.rb
|
|
32
|
-
- lib/tiktok/open/sdk/open_api/auth/helpers.rb
|
|
33
37
|
- lib/tiktok/open/sdk/open_api/auth/user.rb
|
|
34
|
-
- lib/tiktok/open/sdk/
|
|
38
|
+
- lib/tiktok/open/sdk/open_api/user.rb
|
|
35
39
|
- lib/tiktok/open/sdk/version.rb
|
|
36
40
|
- sig/tiktok/open/sdk.rbs
|
|
37
41
|
homepage: https://github.com/pochkuntaras/tiktok-open-sdk
|
|
@@ -43,6 +47,7 @@ metadata:
|
|
|
43
47
|
source_code_uri: https://github.com/pochkuntaras/tiktok-open-sdk
|
|
44
48
|
changelog_uri: https://github.com/pochkuntaras/tiktok-open-sdk/blob/main/CHANGELOG.md
|
|
45
49
|
rubygems_mfa_required: 'true'
|
|
50
|
+
documentation_uri: https://rubydoc.info/gems/tiktok-open-sdk
|
|
46
51
|
rdoc_options: []
|
|
47
52
|
require_paths:
|
|
48
53
|
- lib
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require 'json'
|
|
4
|
-
|
|
5
|
-
module Tiktok
|
|
6
|
-
module Open
|
|
7
|
-
module Sdk
|
|
8
|
-
module OpenApi
|
|
9
|
-
module Auth
|
|
10
|
-
# Shared authentication helper methods for TikTok Open SDK auth modules.
|
|
11
|
-
module Helpers
|
|
12
|
-
include StringUtils
|
|
13
|
-
|
|
14
|
-
private
|
|
15
|
-
|
|
16
|
-
# Returns the HTTP headers for requests.
|
|
17
|
-
#
|
|
18
|
-
# @return [Hash] The headers for HTTP requests.
|
|
19
|
-
def headers
|
|
20
|
-
{
|
|
21
|
-
'Content-Type': 'application/x-www-form-urlencoded',
|
|
22
|
-
'Cache-Control': 'no-cache'
|
|
23
|
-
}
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
# Returns the client credentials for authentication.
|
|
27
|
-
#
|
|
28
|
-
# @return [Hash] The client credentials.
|
|
29
|
-
def credentials
|
|
30
|
-
{
|
|
31
|
-
client_key: Tiktok::Open::Sdk.config.client_key,
|
|
32
|
-
client_secret: Tiktok::Open::Sdk.config.client_secret
|
|
33
|
-
}
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
# Returns the default query parameters for the authorization URI.
|
|
37
|
-
#
|
|
38
|
-
# @return [Hash] The default query parameters:
|
|
39
|
-
# - :client_key [String] The TikTok client key.
|
|
40
|
-
# - :response_type [String] Always 'code'.
|
|
41
|
-
# - :scope [String] Comma-separated scopes.
|
|
42
|
-
# - :redirect_uri [String] The redirect URI.
|
|
43
|
-
# - :state [nil] Default state is nil.
|
|
44
|
-
def authorization_uri_default_params
|
|
45
|
-
{
|
|
46
|
-
client_key: Tiktok::Open::Sdk.config.client_key,
|
|
47
|
-
response_type: 'code',
|
|
48
|
-
scope: Tiktok::Open::Sdk.config.user_auth.scopes.join(','),
|
|
49
|
-
redirect_uri: Tiktok::Open::Sdk.config.user_auth.redirect_uri,
|
|
50
|
-
state: nil
|
|
51
|
-
}
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
# Parses and formats the HTTP response.
|
|
55
|
-
#
|
|
56
|
-
# @param response [Net::HTTPResponse] The HTTP response object.
|
|
57
|
-
# @return [Hash] The formatted response with keys:
|
|
58
|
-
# - :success [Boolean] Whether the response is a Net::HTTPSuccess.
|
|
59
|
-
# - :code [Integer] HTTP status code.
|
|
60
|
-
# - :response [Hash] Parsed JSON body or a hash with the raw string if parsing fails.
|
|
61
|
-
def render_response(response)
|
|
62
|
-
{
|
|
63
|
-
success: response.is_a?(Net::HTTPSuccess),
|
|
64
|
-
code: response.code.to_i,
|
|
65
|
-
response: parse_json(response.body)
|
|
66
|
-
}
|
|
67
|
-
end
|
|
68
|
-
end
|
|
69
|
-
end
|
|
70
|
-
end
|
|
71
|
-
end
|
|
72
|
-
end
|
|
73
|
-
end
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require 'json'
|
|
4
|
-
|
|
5
|
-
module Tiktok
|
|
6
|
-
module Open
|
|
7
|
-
module Sdk
|
|
8
|
-
# Utility methods for string and JSON handling.
|
|
9
|
-
module StringUtils
|
|
10
|
-
module_function
|
|
11
|
-
|
|
12
|
-
# Parses a JSON string into a Ruby hash with symbolized keys.
|
|
13
|
-
#
|
|
14
|
-
# @param str [String] JSON string to parse.
|
|
15
|
-
# @return [Hash] Parsed hash with symbolized keys, or a hash with the raw string if parsing fails.
|
|
16
|
-
def parse_json(str)
|
|
17
|
-
JSON.parse(str, symbolize_names: true)
|
|
18
|
-
rescue JSON::ParserError
|
|
19
|
-
{ raw: str }
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
end
|