Class: OmniAuth::Strategies::TiktokBusinessOauth2

Inherits:
OAuth2
  • Object
show all
Defined in:
lib/omniauth/strategies/tiktok_business_oauth2.rb

Constant Summary collapse

USER_INFO_URL =
'https://business-api.tiktok.com/open_api/v1.3/business/get/'
USER_FIELDS =
'["username","display_name","profile_image","is_business_account"]'

Instance Method Summary collapse

Instance Method Details

#access_token_infoObject



75
76
77
# File 'lib/omniauth/strategies/tiktok_business_oauth2.rb', line 75

def access_token_info
  access_token.to_hash.each_with_object({}) { |(key, value), hash| hash[key.to_s] = value }
end

#authorize_paramsObject



15
16
17
18
19
# File 'lib/omniauth/strategies/tiktok_business_oauth2.rb', line 15

def authorize_params
  super.tap do |params|
    params[:client_key] = options.client_id
  end
end

#build_access_tokenObject

It is necessary to override this method to avoid the "Content-Type" string getting turned into a symbol. Unfortunately you can't just pass headers in token_params method because the original build_access_token deep symbolizes all parameters, and the OAuth2 gem expects the headers to be strings



81
82
83
84
# File 'lib/omniauth/strategies/tiktok_business_oauth2.rb', line 81

def build_access_token
  verifier = request.params["code"]
  client.auth_code.get_token(verifier, {:redirect_uri => callback_url}.merge(token_params), deep_symbolize(options.auth_token_params))
end

#callback_urlObject

Remove params as callback URL must match exactly the URL defined for the application



42
43
44
# File 'lib/omniauth/strategies/tiktok_business_oauth2.rb', line 42

def callback_url
  super.split('?').first
end

#raw_infoObject



56
57
58
# File 'lib/omniauth/strategies/tiktok_business_oauth2.rb', line 56

def raw_info
  @raw_info ||= access_token_info.merge('user' => )
end

#tiktok_user_infoObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/omniauth/strategies/tiktok_business_oauth2.rb', line 60

def 
  @tiktok_user_info ||= begin
    headers = {
      'Access-Token' => access_token.token
    }

    params = {
      business_id: access_token_info['open_id'],
      fields: USER_FIELDS
    }
    
    access_token.get(USER_INFO_URL, headers: headers, params: params, parse: :tiktok_business).parsed
  end
end

#token_paramsObject



46
47
48
49
50
51
52
53
54
# File 'lib/omniauth/strategies/tiktok_business_oauth2.rb', line 46

def token_params
  options.token_params.merge(
    headers: { 'Content-Type' => 'application/json' },
    client_id: options.client_id,
    client_secret: options.client_secret,
    auth_code: request.params["code"],
    parse: :tiktok_business,
  )
end