views:

138

answers:

0

I have been trying to ad a video that already uploaded to youtube to a playlist that already existing on youtube, both created with the same account, that I have the username and password to.

Heres the Google documentation http://code.google.com/apis/youtube/2.0/developers_guide_protocol_playlists.html#Updating_a_playlist

and heres my code trying to do that..... (requesturl = http://gdata.youtube.com/feeds/api/playlists/[playlistid])

_requestUrl = String.Format("{0}/{1}", Constants.General.YOUTUBE_PLAYLISTUPDATE, playlistId)

    Dim _sb As New System.Text.StringBuilder
    With _sb
        .Append("<?xml version=""1.0""?")
        .Append("<entry xmlns=""http://www.w3.org/2005/Atom"" xmlns:yt=""http://gdata.youtube.com/schemas/2007""&gt;")

        .AppendFormat("<id>{0}</id>", videoId)

        .Append("</entry>")
    End With

    _postBytes = System.Text.Encoding.UTF8.GetBytes(_sb.ToString)

    _request = CType(WebRequest.Create(_requestUrl), HttpWebRequest)

        With _request
            .ContentType = "application/atom+xml; charset=UTF-8"
            .ContentLength = _postBytes.Length
            .Method = "POST"
            .Headers.Add("Authorization", String.Format("GoogleLogin auth={0}", MasterAccessToken.ClientLoginToken))
            .Headers.Add("GData-Version", "2")
            .Headers.Add("X-GData-Key", String.Format("key={0}", YouTube.Constants.Security.DEVELOPERKEY))
        End With

        _writeStream = _request.GetRequestStream
        With _writeStream
            .Write(_postBytes, 0, _postBytes.Length)
        End With

        Using _response = CType(_request.GetResponse, HttpWebResponse)
            With _response
                If .StatusCode = HttpStatusCode.OK OrElse .StatusCode = HttpStatusCode.Created Then
                    _result = True
                Else
                    _result = False
                End If
            End With
        End Using

I get back error 400 (bad request) each time. I think the general principle is ok, as I use this method for several other calls, including adding the playlist and uploading the video, both of which work.

I note in the examples that AuthSub is used. I am doing this on a Windows client, and so I am using ClientLogin. As it works on all the other calls, I am kind of assuming its ok for this one...

The ClientLogin Token and the DeveloperKey token are provided, and, as the other calls do work, I think they are ok..

Ideas?


Update:

I found the source code repository on Google for the .NET api for YouTube and downloaded that. I tried to add a video to a playlist using this.... (the fact that the function is there at least attempts to reassure me that it supposed to work ;-) (user name passwords and keys changed...

Dim _rs As New Google.YouTube.YouTubeRequestSettings("m...nle", "AI39s.......q1A", "[username]", "[password]")

Dim _r As New Google.YouTube.YouTubeRequest(_rs) Dim _apentry As Google.GData.Client.Feed(Of Google.YouTube.Playlist)

_apentry = _r.GetPlaylistsFeed("[username]")

Dim _aaa As Google.YouTube.Playlist = _apentry.Entries.First

Dim _plm As New Google.YouTube.PlayListMember

_plm.Id = "jincEPk09EE"

_r.AddToPlaylist(_aaa, _plm)

This generasted a slightly different message, as monitored by fiddler...

POST http://gdata.youtube.com/feeds/api/playlists/E75A209BEEF1521D HTTP/1.1
Content-Type: application/atom+xml; charset=UTF-8
User-Agent: G-managementconsole/GDataGAuthRequestFactory-CS-Version=1.5.0.0--IEnumerable
X-GData-Key: key=AI39si6rzECTrA0wHF....JPNzdTFJw5q1A
Authorization: GoogleLogin auth=DQAA5k9Bb12we8KoqkJT....7JX6hW3r2adME
GData-Version: 2.0
Host: gdata.youtube.com
Content-Length: 427
Expect: 100-continue

<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005"&gt;
  <media:group xmlns:media="http://search.yahoo.com/mrss/"&gt;
    <yt:videoid xmlns:yt="http://gdata.youtube.com/schemas/2007"&gt;jincEPk09EE&lt;/yt:videoid&gt;
  </media:group>
  <category term="http://gdata.youtube.com/schemas/2007#playlist" scheme="http://schemas.google.com/g/2005#kind" />
</entry>

Unfortunatley, this still generates the same 400 Bad Request message that I got before... Back to the drawing board...