Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Embed Youtube Video using iFrame JS Player API

Here's the code to embed Youtube video using their new iFrame Javascript Player API. This is the piece of code I also used to embed YouTube video in my iOS application. Also, it is possible to play it inline. Not the typical fullscreen playback.


Read more about the iFrame Player API Reference.

It's also worth mentioning that on iOS6 devices, autoplay just doesn't work anymore. Apple added this restriction to save user's bandwidth. Read more about User Control of Downloads Over Cellular Networks.

UPDATE 2014-10-29
- Use this instead: https://developers.google.com/youtube/v3/guides/ios_youtube_helper

Facebook IFrame App in Safari

You don't really need cookies to make your Facebook IFrame app work in Safari browser. I remember two years ago, to make Page Tab applications work in Safari, you only need to make use of this trick. I won't be explaining it in details here.

Just recently, I was asked to do this new Fb project that's intended for Page Tab, which now is an iFrame-only platform. I've been developing it using Chrome browser. And when I finally get to test it on Safari, unsurprisingly, it's the same old scenario. What's worse is that the old trick won't work anymore (you better read this).

This app relies so much on Javascript. And instead of deploying several php pages, I only need to come up with a startup page which when loaded on the iFrame, loads the rest of the required scripts. Other pages are dynamically constructed using html fragments that are parsed by a templating script. So most of the Client-Server transactions are done asynchronously. And with Safari continously blocking third-party cookies, it just doesn't work.

After hours of googling for answers, I turned to Facebook's PHP SDK. I found out that it doesn't really require you to use cookies after all (now at 3.1.1). If you take a look at `BaseFacebook` class, particularly the `getSignedRequest` method, you'll see that it first checks the request parameter for Signed Request data (they used to have `session` before this).

...
public function getSignedRequest() {
  if (!$this->signedRequest) {
    if (isset($_REQUEST['signed_request'])) {
      $this->signedRequest = $this->parseSignedRequest(
        $_REQUEST['signed_request']);
    } else if (isset($_COOKIE[$this->getSignedRequestCookieName()])) {
      $this->signedRequest = $this->parseSignedRequest(
        $_COOKIE[$this->getSignedRequestCookieName()]);
    }
  }
  return $this->signedRequest;
}
...

With this in mind, it's possible to send `signed_request` data along with an Ajax request which the PHP SDK can use to determine the user and for your app to verify as well. I use FB.getLoginStatus to acquire a copy of this data. This of course assumes that the user has already authorized your app. Otherwise you'll have to let him do so.

The following example uses jQuery's `$.ajax`.

...
$.ajax({
  url: BACKEND_LINK_HERE,
  type: 'POST',
  data: {
    signed_request: 'sOm3ReA||yLo0ooooooo...oooooooooNgT3xtHeR3',
    another_data: 12345
  }
})
...

So that's it. You need to make sure you have a valid `signed_request` data each time a user interacts with your application. Don't even think about storing it because the access token contained will soon expire which then deems it useless (about 2.5 hours from the moment the user interacted with your app). Yes that's right. And just to let you know, `offline_access` is no longer supported.

Here's a Facebook article about handling invalid and expired access tokens in case you're interested.


Related posts:

Facebook FB.getLoginStatus() Not Working

A lot of them complaining about `FB.getLoginStatus` method not responding. Lucky you, I'm one of them. This is just one of those things common to us when developing an application on Facebook platform. Perhaps the reason why sometimes, changes just seem so hard to adapt. At least only at first.

FB.getLoginStatus(function(response) {
  if (response.status === 'connected') {
    // the user is logged in and has authenticated your
    // app, and response.authResponse supplies
    // the user's ID, a valid access token, a signed
    // request, and the time the access token 
    // and signed request each expire

    var uid = response.authResponse.userID;
    var accessToken = response.authResponse.accessToken;

  } else if (response.status === 'not_authorized') {
    // the user is logged in to Facebook, 
    // but has not authenticated your app

  } else {
    // the user isn't logged in to Facebook.
  }
 });

My application is intended for Page Tab. I'm one of the administrators of it and it's being run on Sandbox Mode.

So here's what I did to solve this issue.



  • go to app's settings page, then Basic tab.
  • fill up `Site URL` field for `Website` under `Select how your app integrates with Facebook` section.
  • hit Save button then reload your app (canvas, page tab,...)

You should see FB.getLoginStatus() in action by now.


Related posts: