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)
Labels
- Assistments (10)
- Assistments API (2)
- Facebook (12)
- fb_graph (1)
- javascript (1)
- LcAlg (2)
- php (5)
- Ruby on Rails (5)
Wednesday, September 28, 2011
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);
}
?>
<?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!
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:
- handle authentication:
- Our application will allow users to associate their Facebook account with their Assistments account using the OAUTH 2.0 protocol.
- It will also request any necessary permissions from the user.
- manage notifications
- When an event, such as a problem set being assigned, occurs, the application will generate a notification of some type.
- When a user arrives at our app via a notification, we can redirect the user to the relevant page.
- 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
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:
- Users can login and logout through my page.
- Send requests from my app to registered users. Once issued they can be:
- updated with new info ("DUE IN 2 HOURS!" or "3 out of 5 questions completed")
- 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.")
- deleted (clear request when associated problem set is completed)
- Register achievements that can then be awarded to users. They can then be:
- read
- deleted
- Award users scores for the app. They can then be:
- read
- 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.
- students:
- The student logs onto facebook and sees a private notification through one or more of Facebook's notification systems. Notification examples:
- New assignment
- Assignment grade available
- The student logs onto Facebook and completes a problem set through the Assistments app
- teachers
- unknown
- parents
- get notifications about their students grades and assignments
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.
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.
Subscribe to:
Posts (Atom)