2

So by using the youtube oauth documentation I came up with this way of getting the access token:

      /**
         * Get api authorization
         *
         * @return string
         */
        public function getAuthorizationUrl()
        {
            // Make redirect
            $this->params = [
                'client_id'    => '######',
                'redirect_uri' => '######',
                'scope'        => 'https://www.googleapis.com/auth/youtube',
                'response_type'=> 'code',
                'access_type'  => 'offline'
            ];
            $redirect_url = 'https://accounts.google.com/o/oauth2/auth?' . http_build_query($this->params);
            return $redirect_url;
        }

        /**
         * Get API token and save account list to db
         *
         * @param $code
         *
         * @return \App\Models\DynamicDashboard\ThirdPartyAccounts
         */
        public function getCallbackUrl($code)
        {

            // Grab the returned code and extract the access token.
            $this->params = [
                'code'          => $code,
                'client_id'     => '#####',
                'client_secret' => '######',
                'redirect_uri'  => '######',
                'grant_type'    => 'authorization_code'
            ];

        // Get access token
        $command = 'curl --data "' . http_build_query($this->params) . '" https://accounts.google.com/o/oauth2/token';
        exec($command, $token);
        $output = implode('', $token);
        $token = json_decode($output);

        // Do a request using the access token to get the list of accounts.
        $command = 'curl -H "Authorization: Bearer ' . $token->access_token . '" https://www.googleapis.com/oauth2/v1/userinfo';
        $result = $this->getRequest($command);

        //Do a request using the access token to get youtube account id.
        $command = 'curl -H "Authorization: Bearer ' . $token->access_token  . '"http://gdata.youtube.com/feeds/api/users/default?v=2';
        exec($command, $youtube);
        var_dump($youtube); exit;

        //Do a request using the access token to get channel id.
        $command = 'curl -H "Authorization: Bearer ' . $token->access_token  . '"https://www.googleapis.com/youtube/v3/channels?part=id&mine=true';
        exec($command, $channel);
        $outputChannel = implode('', $channel);
        $channelId = json_decode($outputChannel);
    }

var_dump of $youtube return an empty array:

array {  
       }

So right now I have succeded in saving the google account but how can I get from that account the youtube account id or channel id? I tried to do that like this:

            //Do a request using the access token to get youtube account id.
            $command = 'curl -H "Authorization: Bearer ' . $token->access_token  . '"http://gdata.youtube.com/feeds/api/users/default?v=2';
            exec($command, $youtube);
            var_dump($youtube); exit;

            //Do a request using the access token to get channel id.
            $command = 'curl -H "Authorization: Bearer ' . $token->access_token  . '"https://www.googleapis.com/youtube/v3/channels?part=id&mine=true';
            exec($command, $channel);
            $outputChannel = implode('', $channel);
            $channelId = json_decode($outputChannel);

But both variables: $youtube and $channelId return an empty array. Can anyone tell me why, please? Thank you for your help!

Alan
  • 213
  • 1
  • 13

1 Answers1

0

I have found this SO post which might help in getting user id after login via google oauth. You need a GET call to https://www.googleapis.com/oauth2/v1/userinfo with a correct access token. In the response, the user id is included. Here is the documentation.

This answer might also help.

If you're asking about how to get either the YouTube username or the YouTube user ID for the currently authenticated user, it can be found in the response to a properly authenticated request to http://gdata.youtube.com/feeds/api/users/default?v=2.

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59
  • I have saved the google plus account but I don't know how to get details like youtube channel id. When I try the request it returns an empty array. I have edited my question. – Alan Sep 12 '16 at 10:37