Class: TiktokBusinessApi::Auth
- Inherits:
-
Object
- Object
- TiktokBusinessApi::Auth
- Defined in:
- lib/tiktok_business_api/auth.rb
Overview
Handles authentication with the TikTok Business API
Instance Attribute Summary collapse
-
#client ⇒ TiktokBusinessApi::Client
readonly
Client instance.
Instance Method Summary collapse
-
#authorization_url(redirect_uri, state = nil, scope = nil) ⇒ String
Generate authorization URL for TikTok Business API.
-
#generate_access_token(auth_code, redirect_uri) ⇒ Hash
Generate an access token using an authorization code.
-
#get_authorized_advertisers(app_id = nil, secret = nil, access_token = nil) ⇒ Hash
Get a list of authorized advertiser accounts.
-
#initialize(client) ⇒ Auth
constructor
Initialize the Auth handler.
-
#refresh_access_token(refresh_token) ⇒ Hash
Refresh an access token (for TikTok account access tokens).
-
#revoke_access_token(token = nil) ⇒ Hash
Revoke an access token.
Constructor Details
#initialize(client) ⇒ Auth
Initialize the Auth handler
12 13 14 |
# File 'lib/tiktok_business_api/auth.rb', line 12 def initialize(client) @client = client end |
Instance Attribute Details
#client ⇒ TiktokBusinessApi::Client (readonly)
Returns Client instance.
7 8 9 |
# File 'lib/tiktok_business_api/auth.rb', line 7 def client @client end |
Instance Method Details
#authorization_url(redirect_uri, state = nil, scope = nil) ⇒ String
Generate authorization URL for TikTok Business API
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/tiktok_business_api/auth.rb', line 103 def (redirect_uri, state = nil, scope = nil) params = { app_id: client.config.app_id, redirect_uri: redirect_uri } params[:state] = state if state params[:scope] = scope.join(',') if scope && !scope.empty? query = params.map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&') "https://ads.tiktok.com/marketing_api/auth?#{query}" end |
#generate_access_token(auth_code, redirect_uri) ⇒ Hash
Generate an access token using an authorization code
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/tiktok_business_api/auth.rb', line 21 def generate_access_token(auth_code, redirect_uri) params = { app_id: client.config.app_id, secret: client.config.secret, auth_code: auth_code, grant_type: 'auth_code' } # Add redirect_uri if provided params[:redirect_uri] = redirect_uri if redirect_uri response = client.request(:post, 'v1.3/oauth2/access_token/', params) # Store the access token in the client config client.config.access_token = response['data']['access_token'] if response['data'] && response['data']['access_token'] response end |
#get_authorized_advertisers(app_id = nil, secret = nil, access_token = nil) ⇒ Hash
Get a list of authorized advertiser accounts
87 88 89 90 91 92 93 94 95 |
# File 'lib/tiktok_business_api/auth.rb', line 87 def (app_id = nil, secret = nil, access_token = nil) params = {} # Use provided values or fallback to client config headers = {} headers['Access-Token'] = access_token || client.config.access_token if access_token || client.config.access_token client.request(:get, 'v1.3/oauth2/advertiser/get/', params, headers) end |
#refresh_access_token(refresh_token) ⇒ Hash
Refresh an access token (for TikTok account access tokens)
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/tiktok_business_api/auth.rb', line 44 def refresh_access_token(refresh_token) params = { app_id: client.config.app_id, secret: client.config.secret, refresh_token: refresh_token, grant_type: 'refresh_token' } response = client.request(:post, 'v1.3/tt_user/oauth2/refresh_token/', params) # Update the access token in the client config client.config.access_token = response['data']['access_token'] if response['data'] && response['data']['access_token'] response end |
#revoke_access_token(token = nil) ⇒ Hash
Revoke an access token
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/tiktok_business_api/auth.rb', line 64 def revoke_access_token(token = nil) token ||= client.config.access_token params = { app_id: client.config.app_id, secret: client.config.secret, access_token: token } response = client.request(:post, 'v1.3/oauth2/revoke_token/', params) # Clear the access token if it was revoked client.config.access_token = nil if response['code'] == 0 response end |