Sign in to follow this  
Followers 0
stereospeaker

Youtube Video Upload via Youtube API - WINHTTP - XMLHTTP

3 posts in this topic

I´m trying to upload a Videofile to my Youtube Account using Autoit and the Youtube API.

I managed to get to the point, that the Videofile gets uploaded "correctly". But when I open my Videos in youtube, youtube displays the error that it could not convert the video.

I tried everything I could think of to resolve this issue. Changed the mime type, used every(!) FileOpen() mode, changed video files, used different types to Upload (WINHTTP and XMLHTTP) but the error remains.

I´m fiddling with this issue since a few weeks now, and I´m all out of ideas. I searched the web for anything that came to my mind what could be related to this issue, like encoding, but again nothing...

So I´m out of ideas, energy and enthusiasm. So i appriciate any help.

Here are my 2 different attemps.

;---- Youtube API:Konstanten
$devKey=yourkey
$ClientID=yourID
$ClientSecret=yourSecret
$Redirect="urn:ietf:wg:oauth:2.0:oob"
Global $acesstoken

Api_Login()<---- Func to get your AccessToken

$Dateihwnd = FileOpen(YourFile.avi");)
$binaryRead = FileRead($Dateihwnd)
FileClose($Dateihwnd)
$VideoTitle="Testtitel"
$VideoDescription="Erste Uploadversuch"
$VideoKeywords="Katze,Lustig,Video"

$xmlPost='--f93dcbA3'&@CRLF& _
'Content-Type: application/atom+xml; charset=UTF-8'&@CRLF &@CRLF & _
''&@CRLF& _
''&@CRLF& _
''&$VideoTitle&''&@CRLF& _
''&@CRLF& _
$VideoDescription&@CRLF& _
''&@CRLF& _
''&@CRLF& _
''&@CRLF& _
'--f93dcbA3'&@CRLF& _
'Content-Type: video/x-msvideo'&@CRLF& _
'Content-Transfer-Encoding: binary'&@CRLF&@CRLF& _
$binaryRead&@CRLF& _
'--f93dcbA3--'&@CRLF

$obj=ObjCreate("Microsoft.XMLHTTP")
$obj.open("POST","http://uploads.gdata.youtube.com/feeds/api/users/default/uploads",False)
$obj.setRequestHeader('Authorization','Bearer '&$acesstoken)
$obj.setRequestHeader('Content-Type','multipart/related; boundary="f93dcbA3"')
$obj.setRequestHeader('Slug','Yourfile.avi')
$obj.setRequestHeader('X-GData-Key','key='&$devKey)
$obj.setRequestHeader('Connection','close')
$obj.send($xmlPost)
MsgBox(0,"",$obj.responsetext)

To change:

$devKey=yourkey

$ClientID=yourID

$ClientSecret=yourSecret

$Dateihwnd = FileOpen(YourFile.avi");)

$obj.setRequestHeader('Slug','Yourfile.avi')

Link to Youtube-API: https://developers.google.com/youtube/2.0/developers_guide_protocol_direct_uploading

Second attemp:

$hopen=_WinHttpOpen("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1" )
$hconn=_WinHttpConnect($hopen, "uploads.gdata.youtube.com" )
$winhttpUpload=_WinHttpSimpleSSLRequest ($hconn,"POST","/feeds/api/users/default/uploads","",$xmlPost,$putHeader,True)
if @error then MsgBox("","",@error)
MsgBox("","UPDATERESPONSE",$winhttpUpload[1])

Please use Data from 1 attempt script. With this script, you get the error from youtube that the upload was canceld.

I have a third version which is based of the Browser-Upload-API (https://developers.google.com/youtube/2.0/developers_guide_protocol_browser_based_uploading) and uses XMLWINHTTP.

The problem here is, that I cannot user the parameter http://www.someurl.com while opening a XMLHTTP request.

Example $obj.open("POST",'YoutubeUploadUrl?nexturl=http://www.example.com',TRUE)

If i use http:// autoit gives me an error when i try to send the request ($obj.send($Post)) and if i leave it out, youtube gives me the response that i did not define the nexturl parameter correctly. It has to strart with http or htpps :/

Hope you guys can help.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

hi stereospeaker, are you sure that your ajax code works fine?

you can try to make a ajax request from a browser..you can use a IEcreateembed or IEcreate object from IE UDF, then you can use a javascript injection like this:

func ajax($page)
$ie.document.url = "javascript: var responsetext; var ajax = new XMLHttpRequest(); ajax.onreadystatechange = state; ajax.open('GET','"&$page&"',true); ajax.send(); function state() {if (ajax.readyState == 4 && ajax.status == 200){responsetext = ajax.responseText;}}"
$text = $ie.document.parentWindow.eval('responsetext')
While $text == ''
$text = $ie.document.parentWindow.eval('responsetext')
WEnd
Return $text
endfunc

$ie is a object returned by IEcreateIEcreateembed

Edited by salvobellino95

Share this post


Link to post
Share on other sites

hi stereospeaker, are you sure that your ajax code works fine?

you can try to make a ajax request from a browser..you can use a IEcreateembed or IEcreate object from IE UDF, then you can use a javascript injection like this:

func ajax($page)
$ie.document.url = "javascript: var responsetext; var ajax = new XMLHttpRequest(); ajax.onreadystatechange = state; ajax.open('GET','"&$page&"',true); ajax.send(); function state() {if (ajax.readyState == 4 && ajax.status == 200){responsetext = ajax.responseText;}}"
$text = $ie.document.parentWindow.eval('responsetext')
While $text == ''
$text = $ie.document.parentWindow.eval('responsetext')
WEnd
Return $text
endfunc

$ie is a object returned by IEcreateIEcreateembed

Hi Salvobellino and thanks for the input.

There is no Javascript involved in the 2 Scripts I posted. You probaply mean my third version where I use the Browserbased Upload Method from the Youtube API. But I don´t use any JS in that version too. All request use either XMLHTTP or WINHTTP.

But your suggestion to use AJAX is interesting and I´ll give it a shot. I already use a mix of AutoIT->Create Webpage(HTML with Upload form elements)->Open with FF->start Upload which kinda works. So your approach to let IE handel the upload sounds promesing.

I´ll try it and let you guys know if it worked or not.

But nevertheless...I really want to know what went wrong with my scripts. So any help to resolve that directupload problem is still highly appriciated :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0