Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help improve authorization through TikTok #1379

Open
Morbids opened this issue Aug 3, 2023 · 2 comments
Open

Help improve authorization through TikTok #1379

Morbids opened this issue Aug 3, 2023 · 2 comments

Comments

@Morbids
Copy link

Morbids commented Aug 3, 2023

Help improve authorization through TikTok
When authorizing through TikTok, an authorization window opens and when you click on the "Allow" button, this page is reloaded.

I suspect this is happening here

$adapter = $hybridauth->authenticate('TikTok');

For other providers it works but for TikTok it causes a reload...

TikTok provider class

<?php

namespace Hybridauth\Provider;

use Hybridauth\Adapter\OAuth2;
use Hybridauth\Data;
use Hybridauth\Exception\UnexpectedApiResponseException;
use Hybridauth\User;

class TikTok extends OAuth2
{
    public $scope = 'user.info.basic';
    protected $apiBaseUrl = 'https://open.tiktokapis.com/v2/';
    protected $authorizeUrl = 'https://www.tiktok.com/v2/auth/authorize/';
    protected $accessTokenUrl = 'https://open.tiktokapis.com/v2/oauth/token/';
    protected $refreshTokenUrl = 'https://open.tiktokapis.com/v2/oauth/token/';
    protected $apiDocumentation = 'https://developers.tiktok.com/doc/overview/';

    public function initialize()
    {
        parent::initialize();

        $this->AuthorizeUrlParameters = array(
            'response_type' => 'code',
            'client_key' => $this->clientId,
            'redirect_uri' => $this->callback,
            'scope' => $this->scope,
        );

        $this->tokenExchangeParameters = array(
            'client_key' => $this->clientId,
            'client_secret' => $this->clientSecret,
            'grant_type' => 'authorization_code',
            'redirect_uri' => $this->callback,
        );

        $this->tokenRefreshParameters = array(
            'client_key' => $this->clientId,
            'grant_type' => 'refresh_token',
            'refresh_token' => $this->getStoredData('refresh_token'),
        );
    }

    public function getUserProfile()
    {
        $response = $this->apiRequest('user/info/?fields=open_id,union_id,avatar_url,display_name,profile_deep_link');
        if (!property_exists($response, 'data') || !property_exists($response->data, 'user') || !property_exists($response->data->user, 'union_id')) {
            throw new UnexpectedApiResponseException('Provider API returned an unexpected response.');
        }
        $data = new Data\Collection($response->data->user);
        $userProfile = new User\Profile();

        $userProfile->identifier = $data->get('union_id');
        $userProfile->displayName = $data->get('display_name');
        $userProfile->description = $data->get('bio_description');
        $userProfile->profileURL = $data->get('profile_deep_link');
        $userProfile->photoURL = $data->get('avatar_url');

        return $userProfile;
    }
}

Call

<?php
require '/vendor/autoload.php';

use Hybridauth\Hybridauth;
use Hybridauth\HttpClient;

$config = [
    'callback' => 'your-callback-url',
    'providers' => [
        'TikTok' => [
            'enabled' => true,
            'keys' => ['id' => 'YOUR_TIKTOK_CLIENT_ID', 'secret' => 'YOUR_TIKTOK_CLIENT_SECRET'],
        ],
    ],
];

$hybridauth = new Hybridauth($config);

try {
    $adapter = $hybridauth->authenticate('TikTok');
    $userProfile = $adapter->getUserProfile();
    echo 'Identifier: ' . $userProfile->identifier . '<br>';
    echo 'Display Name: ' . $userProfile->displayName . '<br>';
    echo 'Photo URL: ' . $userProfile->photoURL . '<br>';
} catch (\Exception $e) {
    echo 'Error: ' . $e->getMessage();
}
@KundKMC
Copy link

KundKMC commented Aug 30, 2023

It would be really nice to have TikTok as a provider. Have you made any progress or is it still open?

@Morbids
Copy link
Author

Morbids commented Aug 30, 2023

The above code works
TikTok just needs a callback without parameters (https://developers.tiktok.com/doc/login-kit-web/)
Link restriction doesn't work for my site...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants