Class: TiktokBusinessApi::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/tiktok_business_api/auth.rb

Overview

Handles authentication with the TikTok Business API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Auth

Initialize the Auth handler

Parameters:



12
13
14
# File 'lib/tiktok_business_api/auth.rb', line 12

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientTiktokBusinessApi::Client (readonly)

Returns Client instance.

Returns:



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

Parameters:

  • redirect_uri (String)

    Redirect URI where the authorization code will be sent

  • state (String) (defaults to: nil)

    Optional state parameter for security

  • scope (Array<String>) (defaults to: nil)

    Optional array of permission scopes

Returns:

  • (String)

    Authorization URL



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/tiktok_business_api/auth.rb', line 103

def authorization_url(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

Parameters:

  • auth_code (String)

    Authorization code from TikTok

  • redirect_uri (String)

    Redirect URI used in the authorization request

Returns:

  • (Hash)

    Access token response



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

Parameters:

  • app_id (String) (defaults to: nil)

    App ID (defaults to the configured app_id)

  • secret (String) (defaults to: nil)

    App secret (defaults to the configured secret)

  • access_token (String) (defaults to: nil)

    Access token (defaults to the configured access_token)

Returns:

  • (Hash)

    List of authorized advertisers



87
88
89
90
91
92
93
94
95
# File 'lib/tiktok_business_api/auth.rb', line 87

def get_authorized_advertisers(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)

Parameters:

  • refresh_token (String)

    Refresh token

Returns:

  • (Hash)

    New access token response



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

Parameters:

  • token (String) (defaults to: nil)

    Access token to revoke (defaults to the current access token)

Returns:

  • (Hash)

    Response



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