Permalink
Browse files

Added a server side (PHP) proxy/gateway to tunnel requests for

YouTube's API due to issues with Security when running in web sandbox.
Thanks to YouTube for making their API so e-a--s----y :)
  • Loading branch information...
1 parent 8a8a5e0 commit 81518efc0c8381b0b0bd911f7ac8176ef148af25 @zoharbabin committed Sep 10, 2011
@@ -23,6 +23,7 @@ package com.zoharbabin.youtube
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.net.URLLoader;
+ import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.net.URLRequestHeader;
import flash.net.URLRequestMethod;
@@ -161,9 +162,21 @@ package com.zoharbabin.youtube
**/
private var clientLoginUrl:String = 'https://www.google.com/accounts/ClientLogin';
/**
- * The url to YouTube's direct upload token service
+ * The url to YouTube's direct upload token service.
**/
private var uploadMethodUrl:String = 'http://gdata.youtube.com/action/GetUploadToken';
+
+ /**
+ * A gateway Url to avoid Google's poor crossdomain.xml file.
+ **/
+ [Bindable]
+ public var gatewayUrl:String = 'ytapi/ytgateway.php';
+
+ /**
+ * A gateway Url for the upload service to avoid Google's poor crossdomain.xml file.
+ **/
+ [Bindable]
+ public var gatewayUrlUpload:String = 'ytapi/savefileanduploadtoyt.php';
/**
* Will be used to retrive the local file to upload and to upload
**/
@@ -190,6 +203,26 @@ package com.zoharbabin.youtube
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, authFailedHandler);
urlLoader.load(urlRequest);
}
+
+ /**
+ * YouTube login call through a gateway (to avoid security issues), listen to loginSuccess and loginFailed to respond.
+ **/
+ public function youTubeLoginGateway (yt_useremail:String, yt_password:String):void
+ {
+ Security.loadPolicyFile('http://gdata.youtube.com/crossdomain.xml');
+ Security.loadPolicyFile('https://accounts.googleapis.com/crossdomain.xml');
+ youTubeUserEmail = yt_useremail;
+ youTubePassword = yt_password;
+ var body:String = 'yt_email='+youTubeUserEmail+'&yt_pass='+youTubePassword+'&yt_service=youtube&yt_appname='+appName;
+ var loginurl:String = gatewayUrl + '?' + body;
+ var urlRequest:URLRequest = new URLRequest(loginurl);
+ urlRequest.method = URLRequestMethod.GET;
+ var urlLoader:URLLoader = new URLLoader();
+ urlLoader.addEventListener(Event.COMPLETE, authCompleteHandler);
+ urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, authFailedHandler);
+ urlLoader.addEventListener(IOErrorEvent.IO_ERROR, authFailedHandler);
+ urlLoader.load(urlRequest);
+ }
/**
* login to YouTube was successful.
@@ -199,8 +232,13 @@ package com.zoharbabin.youtube
var result:String = urlLoader.data;
authKey = result.substr(result.indexOf('Auth=')+5);
authKey = StringUtil.trim(authKey); //'end of line' symbol at the end of the token messes up the API and returns 411
- trace ('Got auth: '+authKey);
- dispatchEvent(new Event("loginSuccess"));
+ authKey = authKey.substring("auth string is:".length,authKey.length);
+ if (authKey.length > 6) {
+ trace ('Got auth: '+authKey);
+ dispatchEvent(new Event("loginSuccess"));
+ } else {
+ dispatchEvent(new Event("loginFailed"));
+ }
}
/**
@@ -257,7 +295,52 @@ package com.zoharbabin.youtube
_isUploadCompleted = false;
isFileLoaded = true;
videoBytes = video_bytes;
- createAtomFeed();
+ //createAtomFeed();
+ gatewayUpload ();
+ }
+
+ /**
+ * It might be required to use a gateway to overcome Google's crossdomain issues,
+ * use this function if you experience issues with security and setup a gateway on your server.
+ * @see savefileanduploadtoyt.php
+ * @see ytupload.php
+ **/
+ public function gatewayUpload ():void {
+ //savefileanduploadtoyt.php
+ var req:URLRequest;
+ var loader:URLLoader = new URLLoader();
+ loader.dataFormat = URLLoaderDataFormat.BINARY;
+ req = new URLRequest(gatewayUrlUpload + "?email="+youTubeUserEmail +
+ "&pass="+youTubePassword +
+ "&title="+ytVideoName +
+ "&desc="+ytVideoDescription +
+ "&tags="+ytKeywords +
+ "&cat="+ytVideoCategory +
+ "&devtags="+developerTag );
+ req.method = URLRequestMethod.POST;
+ req.contentType = 'application/octet-stream';
+ req.data = videoBytes;
+ loader.addEventListener(Event.COMPLETE, gatewayComplete);
+ loader.addEventListener(IOErrorEvent.IO_ERROR, gatewayIOError);
+ loader.load(req);
+ }
+
+ /**
+ * Success handler for the gateway upload.
+ **/
+ private function gatewayComplete(event:Event):void {
+ var msg:String = event.target.data;
+ trace ("gatewayComplete: "+msg);
+ _isUploadCompleted = true;
+ dispatchEvent(new Event("uploadComplete"));
+ }
+
+ /**
+ * Error handler for the gateway upload.
+ **/
+ private function gatewayIOError(event:IOErrorEvent):void {
+ trace('An error occurred (gatewayIOError): ' + event.text);
+ dispatchEvent(new Event("uploadFailed"));
}
/**
@@ -0,0 +1,121 @@
+<?php
+
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category Zend
+ * @package Zend_Gdata
+ * @subpackage App
+ * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @version $Id: HttpException.php 23775 2011-03-01 17:25:24Z ralph $
+ */
+
+/**
+ * Zend_Gdata_App_Exception
+ */
+require_once 'Zend/Gdata/App/Exception.php';
+
+/**
+ * Zend_Http_Client_Exception
+ */
+require_once 'Zend/Http/Client/Exception.php';
+
+/**
+ * Gdata exceptions
+ *
+ * Class to represent exceptions that occur during Gdata operations.
+ *
+ * @category Zend
+ * @package Zend_Gdata
+ * @subpackage App
+ * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+class Zend_Gdata_App_HttpException extends Zend_Gdata_App_Exception
+{
+
+ protected $_httpClientException = null;
+ protected $_response = null;
+
+ /**
+ * Create a new Zend_Gdata_App_HttpException
+ *
+ * @param string $message Optionally set a message
+ * @param Zend_Http_Client_Exception Optionally pass in a Zend_Http_Client_Exception
+ * @param Zend_Http_Response Optionally pass in a Zend_Http_Response
+ */
+ public function __construct($message = null, $e = null, $response = null)
+ {
+ $this->_httpClientException = $e;
+ $this->_response = $response;
+ parent::__construct($message);
+ }
+
+ /**
+ * Get the Zend_Http_Client_Exception.
+ *
+ * @return Zend_Http_Client_Exception
+ */
+ public function getHttpClientException()
+ {
+ return $this->_httpClientException;
+ }
+
+ /**
+ * Set the Zend_Http_Client_Exception.
+ *
+ * @param Zend_Http_Client_Exception $value
+ */
+ public function setHttpClientException($value)
+ {
+ $this->_httpClientException = $value;
+ return $this;
+ }
+
+ /**
+ * Set the Zend_Http_Response.
+ *
+ * @param Zend_Http_Response $response
+ */
+ public function setResponse($response)
+ {
+ $this->_response = $response;
+ return $this;
+ }
+
+ /**
+ * Get the Zend_Http_Response.
+ *
+ * @return Zend_Http_Response
+ */
+ public function getResponse()
+ {
+ return $this->_response;
+ }
+
+ /**
+ * Get the body of the Zend_Http_Response
+ *
+ * @return string
+ */
+ public function getRawResponseBody()
+ {
+ if ($this->getResponse()) {
+ $response = $this->getResponse();
+ return $response->getRawBody();
+ }
+ return null;
+ }
+
+}
@@ -0,0 +1,146 @@
+<?php
+
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category Zend
+ * @package Zend_Gdata
+ * @subpackage App
+ * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @version $Id: MediaFileSource.php 23775 2011-03-01 17:25:24Z ralph $
+ */
+
+/**
+ * @see Zend_Gdata_App_MediaData
+ */
+require_once 'Zend/Gdata/App/BaseMediaSource.php';
+
+/**
+ * Concrete class to use a file handle as an attachment within a MediaEntry.
+ *
+ * @category Zend
+ * @package Zend_Gdata
+ * @subpackage App
+ * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+class Zend_Gdata_App_MediaFileSource extends Zend_Gdata_App_BaseMediaSource
+{
+ /**
+ * The filename which is represented
+ *
+ * @var string
+ */
+ protected $_filename = null;
+
+ /**
+ * The content type for the file attached (example image/png)
+ *
+ * @var string
+ */
+ protected $_contentType = null;
+
+ /**
+ * Create a new Zend_Gdata_App_MediaFileSource object.
+ *
+ * @param string $filename The name of the file to read from.
+ */
+ public function __construct($filename)
+ {
+ $this->setFilename($filename);
+ }
+
+ /**
+ * Return the MIME multipart representation of this MediaEntry.
+ *
+ * @return string
+ * @throws Zend_Gdata_App_IOException
+ */
+ public function encode()
+ {
+ if ($this->getFilename() !== null &&
+ is_readable($this->getFilename())) {
+
+ // Retrieves the file, using the include path
+ $fileHandle = fopen($this->getFilename(), 'r', true);
+ $result = fread($fileHandle, filesize($this->getFilename()));
+ if ($result === false) {
+ require_once 'Zend/Gdata/App/IOException.php';
+ throw new Zend_Gdata_App_IOException("Error reading file - " .
+ $this->getFilename() . '. Read failed.');
+ }
+ fclose($fileHandle);
+ return $result;
+ } else {
+ require_once 'Zend/Gdata/App/IOException.php';
+ throw new Zend_Gdata_App_IOException("Error reading file - " .
+ $this->getFilename() . '. File is not readable.');
+ }
+ }
+
+ /**
+ * Get the filename associated with this reader.
+ *
+ * @return string
+ */
+ public function getFilename()
+ {
+ return $this->_filename;
+ }
+
+ /**
+ * Set the filename which is to be read.
+ *
+ * @param string $value The desired file handle.
+ * @return Zend_Gdata_App_MediaFileSource Provides a fluent interface.
+ */
+ public function setFilename($value)
+ {
+ $this->_filename = $value;
+ return $this;
+ }
+
+ /**
+ * The content type for the file attached (example image/png)
+ *
+ * @return string The content type
+ */
+ public function getContentType()
+ {
+ return $this->_contentType;
+ }
+
+ /**
+ * Set the content type for the file attached (example image/png)
+ *
+ * @param string $value The content type
+ * @return Zend_Gdata_App_MediaFileSource Provides a fluent interface
+ */
+ public function setContentType($value)
+ {
+ $this->_contentType = $value;
+ return $this;
+ }
+
+ /**
+ * Alias for getFilename().
+ *
+ * @return string
+ */
+ public function __toString()
+ {
+ return $this->getFilename();
+ }
+
+}
Oops, something went wrong.

0 comments on commit 81518ef

Please sign in to comment.