YouTube oauth如何申请帐户详细信息

时间:2016-09-08 15:23:56

标签: php oauth youtube-api youtube-data-api google-oauth2

因此,通过使用youtube oauth文档,我想出了获取访问令牌的方法:

      /**
         * 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的{​​p> $youtube返回一个空数组:

array {  
       }

所以现在我已经成功保存了Google帐户但是如何从该帐户获取youtube帐户ID或渠道ID?我试着这样做:

            //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);

但是两个变量:$youtube$channelId都返回一个空数组。谁能告诉我为什么,拜托?谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

我发现这个SO post可能有助于在通过google oauth登录后获取用户ID。您需要使用正确的访问令牌对https://www.googleapis.com/oauth2/v1/userinfo进行GET调用。在响应中,包括用户标识。这是documentation

answer也可能会有所帮助。

  

如果您询问如何获取当前经过身份验证的用户的YouTube用户名或YouTube用户ID,则可以在对properly authenticatedhttp://gdata.youtube.com/feeds/api/users/default?v=2请求的回复中找到该用户ID。 / p>