public function upload() { $appKey = '<app key>'; $appSecret = '<app secret>'; $userId = '<userId>'; $osapi = new osapi(new osapiMySpaceProvider(), new osapiOAuth2Legged($appKey, $appSecret, $userId)); $batch = $osapi->newBatch(); // Load file binary data $data = file_get_contents('images.jpg'); $user_params = array('userId' => '@me', 'groupId' => '@self', 'albumId' => 'myspace.com.album.1224563', 'mediaType' => 'IMAGE', 'mediaItem' => $data, 'contentType' => 'image/jpg'); // The second option in the $batch->add() assigns a request Id. $batch->add($osapi->mediaItems->uploadContent($user_params), 'upload_mediaItem'); // Send all batched commands $result = $batch->execute(); // Demonstrate iterating over a response set, checking for an error & working with the result data. foreach ($result as $key => $result_item) { if ($result_item instanceof osapiError) { echo "<h2>There was a <em>" . $result_item->getErrorCode() . "</em> error with the <em>{$key}</em> request:</h2>"; echo "<pre>" . htmlentities($result_item->getErrorMessage()) . "<<nowiki>/</nowiki>pre>"; } else { echo "<h2>Response for the <em>{$key}</em> request:</h2>"; echo "<pre>" . htmlentities(print_r($result_item, True)) . "<<nowiki>/</nowiki>pre>"; } } }
/** * creates a message * * @param osapi $osapi * @param $recipients array of recipient ids * @param $title * @param $body * @param $userid */ function createMessage(osapi $osapi, $recipients, $body, $userId = '@me') { $batch = $osapi->newBatch(); // Create a message, title not supported $message = new osapiMessage($recipients, $body, ''); $create_params = array('userId' => $userId, 'groupId' => '@self', 'message' => $message); $batch->add($osapi->messages->create($create_params), 'createMessage'); // Send the batch request. $result = $batch->execute(); return $result; }
<?php // Require the osapi library require_once "../src/osapi.php"; // Enable logger. osapiLogger::setLevel(osapiLogger::INFO); osapiLogger::setAppender(new osapiConsoleAppender()); $consumerKey = 'CONSUMER_KEY'; $consumerSecret = 'CONSUMER_SECRET'; $callbackUrl = 'CALLBACK_URL'; // needed to scope the access token storage $localUserId = ''; $storage = new osapiFileStorage('/tmp/osapi'); $provider = new osapiVzOAuth2Provider(osapiVzOAuth2Provider::STUDIVZ); if (isset($_GET['platform']) && $_GET['platform'] === 'schuelervz') { $provider = new osapiVzOAuth2Provider(osapiVzOAuth2Provider::SCHUELERVZ); } $auth = osapiOAuth2::performOAuthLogin($consumerKey, $consumerSecret, $storage, $provider, $callbackUrl, 'openid', array('gender', 'emails', 'thumbnailUrl'), 'my custom message', 'state', $localUserId); if ($auth->getAccessToken()->getplatform() === 'schuelervz') { $provider = new osapiVzOAuth2Provider(osapiVzOAuth2Provider::SCHUELERVZ); } $osapi = new osapi($provider, $auth); // Start a batch so that many requests may be made at once. $batch = $osapi->newBatch(); // Fetch the current user. $self_request_params = array('userId' => '@me', 'groupId' => '@self', 'fields' => array()); $batch->add($osapi->people->get($self_request_params), 'self'); $result = $batch->execute(); var_dump($result);
// enable osapi logging to file osapiLogger::setLevel(osapiLogger::INFO); osapiLogger::setAppender(new osapiFileAppender(sys_get_temp_dir() . '/opensocial.log')); // create yahoo open social provider $provider = new osapiYahooProvider(); // create file system storage using system temp directory $storage = new osapiFileStorage(sys_get_temp_dir()); // if this is a YAP application, the access token and secret // will be provided. if (isset($_POST['yap_viewer_access_token']) && isset($_POST['yap_viewer_access_token_secret']) && isset($_POST['yap_viewer_guid'])) { $oauth = new osapiOAuth3Legged(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, $storage, $provider, $_POST['yap_viewer_guid'], $_POST['yap_viewer_guid'], $_POST['yap_viewer_access_token'], $_POST['yap_viewer_access_token_secret']); } else { $oauth = osapiOAuth3Legged::performOAuthLogin(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, $storage, $provider, $session_id); } // create open social instance from yahoo provider + oauth credentials $opensocial = new osapi($provider, $oauth); // The number of friends to fetch. $friend_count = 10; // Start a batch so that many requests may be made at once. $batch = $opensocial->newBatch(); // Fetch the user profile $batch->add($opensocial->people->get(array('userId' => '@me', 'groupId' => '@self', 'fields' => array('displayName'))), 'self'); // Fetch the friends of the user $batch->add($opensocial->people->get(array('userId' => '@me', 'groupId' => '@friends', 'fields' => array('id'), 'count' => 100)), 'friends'); // Request the activities of the current user $batch->add($opensocial->activities->get(array('userId' => '@me', 'groupId' => '@self', 'count' => 100)), 'userActivities'); // Send the batch request $result = $batch->execute(); foreach ($result as $key => $result_item) { if ($result_item instanceof osapiError) { $code = $result_item->getErrorCode();
/** * Helper function to do 3-legged-OAuth OpenSocial request * @param $userId UserId to work with * @param $osapi_service.call Service to call * @param $keytoset name of the array key that will contain the results * @return array containing 'keytoset' => results, or osapiError-instance when error occurred */ protected function callOpenSocial($user_params, $osapi_service, $keytoset) { $storage = new osapiFileStorage($this->_filestoragepath); $auth = osapiOAuth3Legged_10a::performOAuthLogin($this->_consumerkey, $this->_consumersecret, $storage, $this->_osapiProvider, $user_params['userId']); $osapi = new osapi($this->_osapiProvider, $auth); if ($this->_strictMode) { $osapi->setStrictMode($strictMode); } // Start a batch so that many requests may be made at once. $batch = $osapi->newBatch(); $call = explode('.', $osapi_service); if (sizeof($call) != 2) { throw new Exception("Invalid OpenSocial service call: {$osapi_service}"); } // Instantiate service $oService = $osapi->{$call}[0]; $user_params['userId'] = '@me'; // real userId does not work,use '@me' instead $batch->add($oService->{$call}[1]($user_params), $keytoset); // Send the batch request. try { $result = $batch->execute(); } catch (Exception $e) { // Rethrow throw new Exception("Invalid OSAPI-call: " . $e->getMessage()); } //print_r($result); //print_r($oService); exit(); if ($result[$keytoset] instanceof osapiError) { $err = $result[$keytoset]; if ($err->getErrorCode() == 401) { // Token did not authorize the request; dispose of it, and // get a new one: if (($token = $storage->get($auth->storageKey)) !== false) { $storage->delete($auth->storageKey); /* protect against infinite local loop */ $this->_token_retry_count = isset($this->_token_retry_count) ? $this->_token_retry_count + 1 : 1; if ($this->_token_retry_count < 3) { $this->prepareClient($user_params['userId']); return $this->callOpenSocial($user_params, $osapi_service, $keytoset); } else { throw new Exception("Could not establish accesstoken"); } } else { throw new Exception("Problem occured when performing OpenSocial call: {$osapi_service}"); } } } return $result; }
/** * Test that changing the request body in a preProcessRequest handler actually * results in a modified body */ private function changeBodyInPreRequestProcess($httpProvider, $mockProvider) { // Change the url in the preRequestProcess event $mockProvider->setPreRequestProcess(array($this, 'changeActivityParameter')); $auth = new osapiOAuth2Legged('test', 'data', '12345'); $osapi = new osapi($mockProvider, $auth); // Return a successful response $httpProvider->addResponse('[{"data":null}]', 200); // Post an activity $batch = $osapi->newBatch(); $activity = new osapiActivity('title', 'body'); $batch->add($osapi->activities->create(array('userId' => '@me', 'groupId' => '@self', 'activity' => $activity))); $result = $batch->execute(); // Ensure that the body was actually changed. $request = $httpProvider->getLastRequest(); $titleIndex = strpos($request['body'], self::TARGET_TITLE_VALUE); $this->assertTrue($titleIndex !== false); $this->assertGreaterThan(0, $titleIndex); }
function _sendActivity($case, $activity) { $spaceName = "Sugar Cases " . $case->case_number; $activity = $GLOBALS["current_user"]->full_name . ": " . $activity; $spaces_config = $this->_getConfig(); $provider = new osapiProvider("", "", "", "", $spaces_config["os_rpc_url"], "eXo Social", true, null); $auth = new osapiOAuth2Legged($spaces_config["os_oauth_key_name"], $spaces_config["os_oauth_key_secret"], $spaces_config["os_user"]); $osapi = new osapi($provider, $auth); $osactivity = new osapiActivity(); $osactivity->setTitle($activity); $osactivity->setBody($activity); $params = array('userId' => '@me', 'groupId' => "space:" . $spaceName, 'activity' => $osactivity); // Start a batch $batch = $osapi->newBatch(); $batch->add($osapi->activities->create($params)); $result = $batch->execute(); }