Google YouTube API Uploading Videos From Your Web Application To YouTube Using Java

Google YouTube API lets users who want their web applications to be able to upload videos to the YouTube site as well as displaying the user’s uploaded videos. First things first. As the developer, you need to get a developer’s key. You need this to be able to perform write or upload video operations. Go over to this link. Once you have finished submitting your developer information, you need to register a product in order to get a developer key. Once you’ve finished registering, you actually also get a ClientID aside from the developer key. You need both of these two in your code.

To start with, we need to initialize a YouTubeService object which we use for account validation.

public YouTubeService service;
 
public void init() {
  if (service == null) {
    service = new YouTubeService(YOUTUBE_CLIENTID, YOUTUBE_DEVELOPERKEY);
    String username = YOUTUBE_USERNAME;
    String password = YOUTUBE_PASSWORD;
 
    try {
      service.setUserCredentials(username, password);
    } catch (AuthenticationException ae) {
      ae.printStackTrace();
    }
  }
}

Then we call this method

String token, formUrl;
public static void setFormDetails() throws IOException {
  VideoEntry newEntry = new VideoEntry();
  YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();
 
  String videoTitle = "this is a title";
 
  mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME, "Autos"));
  mg.setTitle(new MediaTitle());
  mg.setPrivate(false);
  mg.setKeywords(new MediaKeywords());
  mg.getKeywords().addKeyword("");
  mg.getTitle().setPlainTextContent(videoTitle);
  mg.setDescription(new MediaDescription());
  mg.getDescription().setPlainTextContent(videoTitle);
 
  URL uploadUrl = new URL("http://gdata.youtube.com/action/GetUploadToken");
 
  try {
    FormUploadToken fut = service.getFormUploadToken(uploadUrl, newEntry);
    token = fut.getToken();
    formUrl = fut.getUrl();
  } catch (ServiceException se) {
    se.printStackTrace();
  }
}

The setFormDetails() method sets two variables token and formUrl which are required values in your HTML form fields. The formUrl would serve as your action URL of the form tag while the token variable will be a hidden field. The action URL must also append a nexturl parameter in order for your browser to redirect it to that URL after upload is finished. Your HTML form field would look like this:

<form action="FORM_URL?nexturl=http://mydomain.com" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="hidden" name="token" value="TOKEN_VALUE"/>
<input type="submit" value="go" />
</form>

Within the setFormDetails() method, the YouTubeNamespace.CATEGORY_SCHEME lets you specify which Category the video will be set as. This method is purely manual. You can create your own form page with title, description and category that lets the user sets it according to his preference. Just make sure those values are not empty because as they are required by the YouTubeAPI.

Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.


(1 votes, average: 5.00 out of 5)
 Loading ...

PHP Mask Credit Card Number

This method masks credit card numbers and shows only the last 4 digits. I also forgot where I got this method from. If you wish to change the number of last digits shown, just modify the last parameter of the method. The part where it says $l = 4.

function maskCC($n, $b = 4, $p = 16, $m = ‘X’, $l = 4) {
  // $n = ‘card number: 1234567890123456′ string
  // $b = break into how many in each chunk?
  // $p = pad to what length?
  // $m = mask character?
  // $l = leave how many numbers showing?
  preg_match(”/card number:\s*(\d+)[^\d]*/i”, trim($n), $matches);
  $pattern = “/card number:\s*(\d+)/ie”;
  $replacement = ” implode(’-', str_split( substr_replace( str_pad(’$1′, ‘$p’, ‘0′, STR_PAD_LEFT), str_repeat(’$m’, $p-$l), 0, $l * -1 ), $b) ); “;
  return preg_replace($pattern, $replacement, trim($n));
}

// use the function
var_dump(maskCC(”\ncard number: 1222233334444\n”));

Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.


(No Ratings Yet)
 Loading ...

PHP Encrypt Decrypt using Base64

Here are two methods to encrypt and decrypt using Base64. I forgot where I got this from but these 2 methods are pretty handy. Make sure you remember your key as your string will be encrypted and decrypted according to what you specify as key. Key is a string here.

Usage is as follows:
$encrypted = encrypt(”to encrypt string”, “chitgoks”);
$decrypted = decrypt($encrypted, “chitgoks”);

$decrypted will return to encrypt string.

function encrypt($string, $key) {
  $result = ”;
  for($i=0; $i<strlen($string); $i++) {
    $char = substr($string, $i, 1);
    $keychar = substr($key, ($i % strlen($key))-1, 1);
    $char = chr(ord($char)+ord($keychar));
    $result.=$char;
  }

  return base64_encode($result);
}

function decrypt($string, $key) {
  $result = ”;
  $string = base64_decode($string);

  for($i=0; $i<strlen($string); $i++) {
    $char = substr($string, $i, 1);
    $keychar = substr($key, ($i % strlen($key))-1, 1);
    $char = chr(ord($char)-ord($keychar));
    $result.=$char;
  }

  return $result;
}

Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.


(No Ratings Yet)
 Loading ...

PHP Delete directory

Deletes a directory in the server’s file system.

function deleteDirectory($dir, $DeleteMe) {
  if(!$dh = @opendir($dir)) return;
  while (false !== ($obj = readdir($dh))) {
    if($obj==’.’ || $obj==’..’) continue;
    if (!@unlink($dir.’/’.$obj)) SureRemoveDir($dir.’/’.$obj, true);
  }
  closedir($dh);
  if ($DeleteMe){
  @rmdir($dir);
}

Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.


(No Ratings Yet)
 Loading ...