Wednesday, September 28, 2011

AppRequest functionality added to fb_graph gem

The developer of the fb_graph facebook graph api gem helped me out by adding the ability to post app-generated requests! Thanks, Nov Matake!

(https://github.com/nov/fb_graph/wiki/App-Request)

Everything I need to do in Ruby, but in PHP

I've had 9/10ths of this working for about a week, I've just been stuck getting it into ruby. I'm just going to post some code for sending notifications, getting a user's notifications, and deleting them. It also handles login.

<?php

require 'facebook-php-sdk/src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => '168953989848835',
  'secret' => '60359e93359d47a9d349c3dd263a479a',
));

// Get User ID
$user = $facebook->getUser();

// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl();
}


?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <head>
    <title>php-sdk</title>
    <style>
      body {
        font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
      }
      h1 a {
        text-decoration: none;
        color: #3b5998;
      }
      h1 a:hover {
        text-decoration: underline;
      }
    </style>
  </head>
  <body>
    <h1>php-sdk</h1>

    <?php if ($user): ?>
      <a href="<?php echo $logoutUrl; ?>">Logout</a>
    <?php else: ?>
      <div>
        Login using OAuth 2.0 handled by the PHP SDK:
        <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
      </div>
    <?php endif ?>

    <h3>PHP Session</h3>
    <pre><?php print_r($_SESSION); ?></pre>

<?php

  $app_id = '168953989848835';
  $app_secret = '60359e93359d47a9d349c3dd263a479a';

  $token_url = "https://graph.facebook.com/oauth/access_token?" .
    "client_id=" . $app_id .
    "&client_secret=" . $app_secret .
    "&grant_type=client_credentials";

  $app_access_token = file_get_contents($token_url);

  $user_id = substr($user.id, 0, -2);

  $apprequest_url ="https://graph.facebook.com/" .
    $user_id .
    "/apprequests?message='INSERT_UT8_STRING_MSG'" .
    "&data='INSERT_STRING_DATA'&"  .  
    $app_access_token . "&method=post";

  $result = file_get_contents($apprequest_url);
  #echo $user_id;
  echo "App Request sent?";
  echo "request id = " . $result;
 
  #$pieces = explode("=", $app_access_token);
  #echo $pieces[1];
    //Get all app requests for user
   
  $request_url ="https://graph.facebook.com/" .
    $user_id . "/apprequests?" .
     $app_access_token;
  $requests = file_get_contents($request_url);

  //Print all outstanding app requests
  echo '<pre>';
  print_r($requests);
  echo '</pre>';
 
  //Process and delete app requests
  $data = json_decode($requests);
  foreach($data->data as $item) {
    $id = $item->id;
    $delete_url = "https://graph.facebook.com/" .
      $id . "?" . $app_access_token . "&method=delete";

    $result = file_get_contents($delete_url);
    echo("Requests deleted is " . $result. ", message = " . $item->message);
  } 
 
?>

Tuesday, September 27, 2011

Welp

I was talking with the author of the fbgraph gem about how to do something, at least I ended up getting him to add it to his TODO list:

https://github.com/nov/fb_graph/issues/126

Go go gadget unsupported language!

Project Outline

In the past few years there has been an industry-wide trend towards Facebook integration. It can even be seen on WPI’s JobFinder website. Essentially, the goal is to let users of your website engage with it through facebook. Apps can deliver alerts and news stories to users. Facebook also provides access to many other core features via API. Some examples are sending requests, awarding achievements, and creating events. It also gives apps their own page on Facebook’s site, where they can fill a ‘canvas’ which covers most of the page with anything they want.
We plan to build a facebook application that will accomplish these functions:
    1. handle authentication:
      1. Our application will allow users to associate their Facebook account with their Assistments account using the OAUTH 2.0 protocol.
      2. It will also request any necessary permissions from the user.
    2. manage notifications
      1. When an event, such as a problem set being assigned, occurs, the application will generate a notification of some type.
      2. When a user arrives at our app via a notification, we can redirect the user to the relevant page.
    3. It will also provide the infrastructure necessary to integrate Assistments further with facebook. For example, awarding achievements to users will be as simple as a single post request.

This will be beneficial to students using Assistments because many younger children use facebook. With our application, assistments will be immediately visible to students in a place they already go. We hope that if we increase the convenience of using assistments, students will be more likely to do their assignments.

Monday, September 26, 2011

Issues with ROR and FB

I've been having a lot of trouble getting rails to work with facebook. They only support PHP and javascript SDKs, so I'm going to try using a ruby gem I found that integrates javascript and rails.

https://github.com/cowboyd/therubyracer

Sunday, September 18, 2011

Progress with Facebook API

I've been tinkering with the facebook API. So far, I have working versions of:
  1. Users can login and logout through my page.
  2. Send requests from my app to registered users. Once issued they can be:
    1. updated with new info ("DUE IN 2 HOURS!" or "3 out of 5 questions completed")
    2. read ("A good common practice for Apps on Facebook.com is to detect if a user has any requests pending when your application loads and then prompt the user to complete the actions associated with the requests before continuing.")
    3. deleted (clear request when associated problem set is completed)
  3. Register achievements that can then be awarded to users. They can then be:
    1. read
    2. deleted
  4. Award users scores for the app. They can then be:
    1. read
    2. deleted
This is all PHP implementations of html POST requests. It will be easy to transfer to ROR once we get our version of ASSISTments running
 
Also, Screenshots!
Achievements and scores. It also autogenerates notifications about how you have x of y achievements for an app, or the highest score.
 
 
 
 
 
 
 
 
 
 
 
 
Requests from an app to a user: These show up in a few different places, including the main page.
(I left the default text in)

Tuesday, September 13, 2011

Assisments Facebook App Users

The potential users for our Facebook app fall into three groups:. Here's some examples of how each group could use our app.
  1. students:
    1. The student logs onto facebook and sees a private notification through one or more of Facebook's notification systems. Notification examples:
      1. New assignment
      2. Assignment grade available
    2. The student logs onto Facebook and completes a problem set through the Assistments app
  2. teachers
    1. unknown
  3. parents
    1. get notifications about their students grades and assignments
We recently got the results from a survey given to the parents of children using Assistments. We haven't been able to go through all of them, but there is some interest in getting notifications through Facebook.



    Assistments ISP First Post

    Hi! My name is Paul Kinsky. I'm a Computer Science major at Worcester Polytechnic Institute, WPI.

    I'm working on a project for several professors at integrate a to extend a piece of educational software called Assistments with social media. Right now we're working on a facebook app that would allow students to complete problem sets within facebook.

    We've been granted access to the source code today, by the end of the week we hope to have a working prototype. I'll be posting details as we go.