W
W
walkerstech2021-03-05 21:03:20
YouTube
walkerstech, 2021-03-05 21:03:20

Authorization through youtube?

Authorization through google does not work, I did not find anything sensible in the documentation. I get an access token, but the problem occurs after the line " if(isset($tokenInfo['access_token'])) { ".

object(stdClass)#19 (1) {
  ["error"]=>
  object(stdClass)#20 (4) {
    ["code"]=>
    int(403)
    ["message"]=>
    string(42) "Requests from referer  are blocked."
    ["errors"]=>
    array(1) {
      [0]=>
      object(stdClass)#21 (3) {
        ["message"]=>
        string(42) "Requests from referer  are blocked."
        ["domain"]=>
        string(6) "global"
        ["reason"]=>
        string(9) "forbidden"
      }
    }
    ["status"]=>
    string(17) "PERMISSION_DENIED"
  }
}

private function LoginWithYouTube()
    {
        if(empty(Request::get("code"))) {

            $url = 'https://accounts.google.com/o/oauth2/auth';

            $params = array(
                'redirect_uri' => config()->youtube['redirect_uri'],
                'response_type' => 'code',
                'client_id' => config()->youtube['client_id'],
                'scope' => 'https://www.googleapis.com/auth/youtube.readonly https://www.googleapis.com/auth/youtube.channel-memberships.creator'
            );

            redirect($url . '?' . urldecode(http_build_query($params)));
        } else {
            $result = false;

            $params = array(
                'client_id' => config()->youtube['client_id'],
                'client_secret' => config()->youtube['client_secret'],
                'redirect_uri' => config()->youtube['redirect_uri'],
                'grant_type' => 'authorization_code',
                'code' => Request::get('code')
            );

            $url = 'https://accounts.google.com/o/oauth2/token';

            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_POST, 1);
            //curl_setopt($curl, CURLOPT_POSTFIELDS, urldecode(http_build_query($params)));
            curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            $result = curl_exec($curl);
            curl_close($curl);

            $tokenInfo = json_decode($result, true);
            //dd($tokenInfo);
            if(isset($tokenInfo['access_token'])) {
                $ch1 = curl_init('https://youtube.googleapis.com/youtube/v3/channels?part=snippet%2CcontentDetails%2Cstatistics&mine=true&key='.config()->youtube['api_key']);
                curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch1, CURLOPT_HTTPHEADER, array(
                    'Authorization: Bearer ' . $tokenInfo['access_token'],
                    'Accept: application/json'
                ));

                $r1 = curl_exec($ch1);
                $i1 = curl_getinfo($ch1);

                curl_close($ch1);

                $userInfo = json_decode($r1);
                dd($userInfo);

                if(!($user = $this->UserModel->getUser($userInfo['id'], "user_youtube"))) {
                    $data = [
                        "user_login" 	            =>	"youtube_" . $userInfo['items'][0]['id'],
                        "user_login_show"           =>	$userInfo['items'][0]['snippet']['title'],
                        //'user_email'                =>  $userInfo['email'],
                        "user_domain"               =>  "youtube_" . $userInfo['items'][0]['id'],
                        "user_avatar"	            =>	(!empty($userInfo['items'][0]['snippet']['thumbnails']['default']['url'])) ? $userInfo['items'][0]['snippet']['thumbnails']['default']['url'] : "/assets/images/no_avatar.png",
                        "user_youtube"	            =>	$userInfo['items'][0]['id'],
                        "user_reg_ip"	            =>  $_SERVER["REMOTE_ADDR"],
                        "user_youtube_token"        =>  $tokenInfo['access_token'],
                        "user_youtube_subs"         =>  $userInfo['items'][0]['statistics']['subscriberCount'],
                        "user_donate_page"          =>  "{\"min_sum\":\"1\",\"rec_sum\":\"50\",\"text\":\"\",\"fuck_filter\":\"0\",\"fuck_name_filter\":\"0\",\"fuck_words\":\"\",\"bg_color\":\"#e0e0e0\",\"bg_type\":\"1\",\"bg_size\":\"auto\",\"bg_image\":\"\",\"bg_image_name\":\"\",\"bg_repeat\":\"no-repeat\",\"bg_position\":\"center\",\"bg_header_type\":\"1\",\"bg_header_image\":\"\",\"bg_header_size\":\"auto\",\"bg_header_repeat\":\"no-repeat\",\"bg_header_position\":\"center\",\"bg_header_color\":\"#f2f2f2\",\"text_header_color\":\"#000000\",\"btn_color\":\"#ff5400\",\"btn_text_color\":\"#ffffff\"}",
                        "user_reg_time"             => "NOW()",
                    ];
                    $id = $this->UserModel->addUser($data);
                    $this->UserModel->trackIP($id, 1);
                    $this->createDefaultWidgets($id);
                    $this->ToOnline($id);
                }

                $this->UserModel->editUser($user['user_id'], ['user_youtube_token' => $tokenInfo['access_token']]);
                return $this->ToOnline($user['user_id']);
            }
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
part_os, 2021-03-09
@part_os

Try to take a ready-made library.
In it you can see how authorization is implemented and what is wrong with you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question