YouTube API Getting Logged in user thumbnail C#
I"m getting a ProfileEntry for a logged in user. I've managed to get the username and such, but I'm having difficulty getting the users profile image. I see it in my browser when I goto http://gdata.youtube.com/feeds/api/users/iamryancoke under the element but I'm not sure how to access that with the C# implementation.
EDIT: I'm using OAuth and here's the code I'm trying to figure out how to get the thumbnail from this:
// Prepare YouTube request settings.
var settings = new YouTubeRequestSettings(youtubeSettings.AppName, youtubeSettings.DeveloperKey,
consumer.ConsumerKey, consumer.TokenManager.ConsumerSecret, accessToken, consumer.TokenManager.GetTokenSecret(accessToken),
youtubeSettings.User, MessagingUtilities.GetRequestUrlFromContext().StripQueryArgumentsWithPrefix("oauth_").ToString());
// Prepare YouTube request.
var youtubeRequest = new YouTubeRequest(settings);
var entry = youtubeRequest.Service.Get("http://gdata.youtube.com/feeds/api/u开发者_JAVA技巧sers/default");;
return (ProfileEntry)entry;
I suppose you are using the Google Data API .NET client library.
http://code.google.com/p/google-gdata/
It took me some time to figure out where the thumbnail was hidden. They burried it pretty well, but you can retrieve the profile picture as follows:
var service = new YouTubeService("My YouTube Application");
service.setUserCredentials("your_email_address@gmail.com", "yourpassword");
var token = service.QueryClientLoginToken();
service.SetAuthenticationToken(token);
string feedUrl = "http://gdata.youtube.com/feeds/api/users/yourusername";
var profile = (ProfileEntry) service.Get(feedUrl);
var thumbnail = (from e in profile.ExtensionElements
where e.XmlName == "thumbnail"
select (XmlExtension) e).SingleOrDefault();
if (thumbnail != null)
{
var thumbnailUrl = thumbnail.Node.Attributes["url"].Value;
}
Once you have the thumbnail URL to the profile picture you can use a simple web request to download and save the image.
A crude example:
string thumbnailUrl =
"http://i4.ytimg.com/i/sIb6065PuhCXorT1WfbJAw/1.jpg?v=bf5482";
WebRequest request = WebRequest.Create(thumbnailUrl);
WebResponse response = request.GetResponse();
Image image = Image.FromStream(response.GetResponseStream());
var extension = Path.GetExtension(thumbnailUrl).Substring(0, 4);
image.Save(@"c:\me" + extension);
精彩评论