Youtube PHP API:验证 YouTube 用户名与身份验证令牌是否匹配

发布于 2024-11-27 08:36:46 字数 2271 浏览 0 评论 0原文

我有一个网络应用程序,我让用户登录 youtube 来授权我的网络应用程序访问他们的帐户。但是,我需要他们的 YouTube 用户名来实际列出他们上传的视频,并且我想确保他们提供的 YouTube 用户名与他们用于授权的帐户匹配。换句话说,我只希望用户能够分享他们上传的视频,而不是其他人的。有办法做到这一点吗?

我之前使用 C# .Net 使用以下代码完成了此操作:

YouTubeRequestSettings yt_settings = new YouTubeRequestSettings(<devID name>, devKey, auth);
YouTubeRequest yt_request = new YouTubeRequest(yt_settings);

Uri uri = new Uri("http://gdata.youtube.com/feeds/api/users/default/uploads/?start-index=1&max-results=1");
Feed<Video> videoFeed = yt_request.Get<Video>(uri);
string uploader = "";
if (videoFeed.Entries.Count() > 0)
    uploader = videoFeed.Entries.ElementAt(0).Uploader;

但是当我尝试使用 php 进行类似操作时,我得到的似乎是标准提要:

    $yt = new Zend_Gdata_YouTube($http_client,<devID name>,null,$_yt_dev_key);
$feed_url = urlencode("http://gdata.youtube.com/feeds/api/users/default/uploads/?start-index=1&max-results=1");
$videoFeed = $yt->getVideoFeed();
if(count($videoFeed) > 0)
{
    $videoEntry = $videoFeed[0];
    echo var_dump($videoEntry);
}

任何人都知道我是否做错了什么?

------------------ 更新 ----------------------

我有获取 YouTube 视频源的解决方案对于经过身份验证的会话中的用户。 $videoFeed = $yt->getuserFeed("默认"); 不过,我仍在研究如何从中获取上传者名称,以便我可以直接从 javascript 执行进一步的视频列表(就像我对旧的 C#/Asp .Net Web 应用程序所做的那样)。

----------- 一个相当粗糙的解决方案 ------------

嗯,这不完全是一个优雅的解决方案,但这是我正在努力的。

下面将从 VideoEntry 对象中提取 youtube 用户名...

$videoFeed = $yt->getuserUploads("default");
if(count($videoFeed) > 0)
{
    $videoEntry = $videoFeed[0];
    $v_dump = var_export($videoEntry, true);
    $check_for = "http://gdata.youtube.com/feeds/api/users/";
    $pos = strpos($v_dump,$check_for);
    $start_pos = $pos + strlen($check_for);
    $user_name = "";
    for($i = $start_pos; $i < strlen($v_dump); $i++)
    {
        if(($v_dump[$i] == '/') or ($v_dump[$i] == '?'))
            break;
        $user_name .= $v_dump[$i];
    }
    echo $user_name;
}

基本上,我正在解析 feed url 的第一个通用部分的整个视频条目变量字符串表示形式,然后获取 url 中的下一个标记,即用户名。换句话说,我在 var 转储中找到这个 url 并解析出用户名:

http://gdata.youtube.com/feeds/api/users/<username>/uploads

如果有人能找到更好、更干净的方法来做到这一点,那就太棒了。像这样解析一个大字符串似乎是一种非常肮脏的方法。

I have a web application where I'm having the user log into youtube to authorize my web application to access their account. However, I need their youtube user name to actually list their uploaded videos, and I want to ensure that the youtube username they provide matches up with the account they used to authorize with. In other words, I only want the user to be able to share videos that they've uploaded, and not someone else's. Is there a way to do this?

I had done this before with C# .Net with the following code:

YouTubeRequestSettings yt_settings = new YouTubeRequestSettings(<devID name>, devKey, auth);
YouTubeRequest yt_request = new YouTubeRequest(yt_settings);

Uri uri = new Uri("http://gdata.youtube.com/feeds/api/users/default/uploads/?start-index=1&max-results=1");
Feed<Video> videoFeed = yt_request.Get<Video>(uri);
string uploader = "";
if (videoFeed.Entries.Count() > 0)
    uploader = videoFeed.Entries.ElementAt(0).Uploader;

But when I try something similar with php I get what appears to be a standard feed:

    $yt = new Zend_Gdata_YouTube($http_client,<devID name>,null,$_yt_dev_key);
$feed_url = urlencode("http://gdata.youtube.com/feeds/api/users/default/uploads/?start-index=1&max-results=1");
$videoFeed = $yt->getVideoFeed();
if(count($videoFeed) > 0)
{
    $videoEntry = $videoFeed[0];
    echo var_dump($videoEntry);
}

Anyone have any idea if I'm doing something wrong?

------------------ UPDATE ----------------------

I have the solution to getting youtube video feed for the user in the authenticated session. $videoFeed = $yt->getuserFeed("default");
Though, I'm still looking at how to get the uploader name from this so that I can perform further video listings directly from javascript (like I had done with my old C#/Asp .Net web app).

----------- A RATHER ROUGH SOLUTION ------------

Well, this isn't exactly an elegant solution, but it's what I have working.

The following will extract the youtube username from a VideoEntry object...

$videoFeed = $yt->getuserUploads("default");
if(count($videoFeed) > 0)
{
    $videoEntry = $videoFeed[0];
    $v_dump = var_export($videoEntry, true);
    $check_for = "http://gdata.youtube.com/feeds/api/users/";
    $pos = strpos($v_dump,$check_for);
    $start_pos = $pos + strlen($check_for);
    $user_name = "";
    for($i = $start_pos; $i < strlen($v_dump); $i++)
    {
        if(($v_dump[$i] == '/') or ($v_dump[$i] == '?'))
            break;
        $user_name .= $v_dump[$i];
    }
    echo $user_name;
}

Basically, I'm parsing through the entire video entry variable string representation for the first generic part of the feed url, then getting the next token in the url which is the username. In other words, I'm locating this url in the var dump and parsing the username out:

http://gdata.youtube.com/feeds/api/users/<username>/uploads

If anyone can find a better and cleaner way to do it, that'd be awesome. Parsing a large string like this seems to be a very dirty way of doing this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゃ懵逼小萝莉 2024-12-04 08:36:46

请参阅 http://code.google.com/apis/youtube/2.0/ Developers_guide_php.html#Enabling_user_interaction

它说,

Note: Using the string default instead of a username retrieves the profile of the currently authenticated user.

因此您可以使用它获取有关当前经过身份验证的用户的所有用户数据。

希望有帮助!

See http://code.google.com/apis/youtube/2.0/developers_guide_php.html#Enabling_user_interaction

It says,

Note: Using the string default instead of a username retrieves the profile of the currently authenticated user.

So you can get all the user data about the currently authenticated user using this.

Hope it helps!

试试这个:

$user = $yt->getUserProfile('default');
$username = $user->getUserName();

Try this:

$user = $yt->getUserProfile('default');
$username = $user->getUserName();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文