Lesson 12: Using the OpenSocial REST API

From VZ Developer Wiki
Revision as of 12:34, 21 June 2010 by Bastian (talk | contribs) (Created page with 'The easiest way to access the OpenSocial REST or RPC API is with an OpenSocial client library. The client libraries handle to complete two- or three-legged OAuth flow (see [[OAut…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The easiest way to access the OpenSocial REST or RPC API is with an OpenSocial client library. The client libraries handle to complete two- or three-legged OAuth flow (see OAuth_Use_Cases) and abstract the whole API communication.

The Client_Libraries article shows where you can download such client libraries and how you can configure them to work with VZ-Netzwerke.

Following are some examples on how to use the PHP library.

Retrieving user data

<?php
// Add the osapi directory to the include path
set_include_path(get_include_path() . PATH_SEPARATOR . '..');

// Require the osapi library
require_once "osapi/osapi.php";

// set logger to just output any debug information to the console or browser
osapiLogger::setLevel(0);
osapiLogger::setAppender(new osapiConsoleAppender());

$userId = 'USER_ID';

//create provider
$provider = new osapiVzProvider(osapiVzProvider::STUDIVZ);
$auth =  new osapiOAuth2Legged('CONSUMER_KEY', 'CONSUMER_SECRET', $userId);
//activate body hash
$auth->setUseBodyHash(true);
$osapi = new osapi($provider, $auth);

// Start a batch so that many requests may be made at once.
$batch = $osapi->newBatch();

$profile_fields = array(
    'aboutMe',
    'displayName',
    'lookingFor',
    'photos'
);

// Fetch the current user.
$self_request_params = array(
    'userId' => '@me',              // Person we are fetching.
    'groupId' => '@self',             // @self for one person.
    'fields' => $profile_fields       // Which profile fields to request.
);
$batch->add($osapi->people->get($self_request_params), 'self');

// Fetch the friends of the user
$friends_request_params = array(
    'userId' => $userId,              // Person whose friends we are fetching.
    'groupId' => '@friends',          // @friends for the Friends group.
    'fields' => $profile_fields,      // Which profile fields to request.
    'count' => 2          // Max friends to fetch.
);
$batch->add($osapi->people->get($friends_request_params), 'friends');

//execute the batch request
echo '<pre>';
print_r($batch->execute());
echo '</pre>';

Sending notifications

<?php
// Add the osapi directory to the include path
set_include_path(get_include_path() . PATH_SEPARATOR . '..');

// Require the osapi library
require_once "osapi/osapi.php";

// set logger to just output any debug information to the console or browser
osapiLogger::setLevel(0);
osapiLogger::setAppender(new osapiConsoleAppender());

$userId = 'USER_ID';

//create provider
$provider = new osapiVzProvider(osapiVzProvider::STUDIVZ);
$auth =  new osapiOAuth2Legged('CONSUMER_KEY', 'CONSUMER_SECRET', $userId);
//activate body hash
$auth->setUseBodyHash(true);
$osapi = new osapi($provider, $auth);

// Start a batch so that many requests may be made at once.
$batch = $osapi->newBatch();

$message = new osapiMessage(array($userId), 'this is the body', 'this is the title', 'NOTIFICATION');

$params = array(
	'userId' => $userId,
	'message' => $message,
);

$batch->add($osapi->messages->create($params), 'requestId');

//execute the batch request
echo '<pre>';
print_r($batch->execute());
echo '</pre>';