【问题标题】:YouTube oauth how to request account detailsYouTube oauth 如何请求帐户详细信息
【发布时间】:2017-01-16 14:46:40
【问题描述】:

因此,通过使用 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 of $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 返回一个空数组。谁能告诉我为什么?感谢您的帮助!

【问题讨论】:

  • 你的意思是google plus id
  • 是的,对不起,我的错误,以及像个人资料名称这样的帐户信息
  • 表示你已经获得了访问令牌,并且通过使用访问令牌你想获得配置文件ID和名称,对吧?
  • 确实是这个意思

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


【解决方案1】:

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

这个answer 也可能有帮助。

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

【讨论】:

  • 我已经保存了 google plus 帐户,但我不知道如何获取 youtube 频道 ID 等详细信息。当我尝试请求时,它返回一个空数组。我已经编辑了我的问题。
猜你喜欢
  • 2020-02-04
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
  • 2023-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-07
相关资源
最近更新 更多