Labels

Showing posts with label Ruby on Rails. Show all posts
Showing posts with label Ruby on Rails. Show all posts

Sunday, October 30, 2011

API progress

As of now, I've made a test api call that gets a database object, saves data to it, and then reads other data from it. I've also managed to create 'watcher' database objects via API and retrieve data from them (a callback url) elsewhere.

The next step is to send a POST request to that callback_url when the relevant event happens. This can be expanded to cover assignments being added, achievements, etc.

EDIT: and then I uploaded the code - my api call is test_call in lib/gwt/ajax.rb

EDIT2: now featuring working post/response of JSON data to and from an external php script! Proof of concept finished, time to start on the actual product.

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!

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