<?php phpinfo(); /** * Example API call * GET profile information */ $consumer_key = '123456'; $consumer_secret = '56xyAzi1'; $url = 'http://api.figshare.com/v1/my_data/articles'; $method = 'POST'; $oauth = new OAuth($consumer_key, $consumer_secret); $oauth->setToken('n4oTQ22l4FxsuQlYZlhCFwYrrSgrlPn1lhIx32uzwzAwn4oTQ22l4FxsuQlYZlhC1F', '0MNOqkQncNHuKTi6fQ8MuA'); $OA_header = $oauth->getRequestHeader($method, $url); $headers = array("Content-Type: application/json", "Authorization: {$OA_header}"); $data = json_encode(array('title' => 'Test dataset', 'description' => 'Test description', 'defined_type' => 'dataset')); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); echo $response;
/** * Tests OAuth authentication in requests. */ function testRequestAuthentication() { $entity_type = 'entity_test'; $resource = 'entity:' . $entity_type; $method = 'GET'; $format = 'json'; // Allow GET requests through OAuth on entity_test. $config = \Drupal::configFactory()->getEditable('rest.settings'); $settings = array(); $settings[$resource][$method]['supported_formats'][] = $format; $settings[$resource][$method]['supported_auth'][] = 'oauth'; $config->set('resources', $settings); $config->save(); $this->container->get('router.builder')->rebuild(); // Create an entity programmatically. $entity_values = array('name' => 'Some name', 'user_id' => 1, 'field_test_text' => array(0 => array('value' => 'Some value', 'format' => 'plain_text'))); $entity = entity_create($entity_type, $entity_values); $entity->save(); // Create a user account that has the required permissions to read // resources via the REST API. $permissions = array('view test entity', 'restful get entity:' . $entity_type, 'access own consumers'); $account = $this->drupalCreateUser($permissions); $this->drupalLogin($account); // Generate a set of consumer keys. $this->drupalPostForm('oauth/consumer/add/' . $account->id(), array(), 'Add'); // Get the consumer we just generated for the new user. $user_data = \Drupal::service('user.data')->get('oauth', $account->id()); // Now send an authenticated request to read the entity through REST. $url = $entity->urlInfo()->setRouteParameter('_format', $format); $endpoint = $url->setAbsolute()->toString(); $oauth = new \OAuth(key($user_data), $user_data[key($user_data)]['consumer_secret']); $oauth_header = $oauth->getRequestHeader('GET', $endpoint); $out = $this->curlExec(array(CURLOPT_HTTPGET => TRUE, CURLOPT_NOBODY => FALSE, CURLOPT_URL => $endpoint, CURLOPT_HTTPHEADER => array('Authorization: ' . $oauth_header))); $this->verbose('GET request to: ' . $endpoint . '<hr />' . $out); $this->assertResponse('200', 'HTTP response code is 200 for successfully authenticated request.'); $this->curlClose(); }
function UploadTweetMedia($mediaUrl) { global $twitterCredentials; if ($twitterCredentials === false) { return false; } if (!$mediaUrl) { return false; } $data = \Newsstand\HTTP::Get($mediaUrl); if (!$data) { return false; } $boundary = ''; $mimedata['media'] = "content-disposition: form-data; name=\"media\"\r\nContent-Type: image/png\r\nContent-Transfer-Encoding: binary\r\n\r\n" . $data; while ($boundary == '') { for ($x = 0; $x < 16; $x++) { $boundary .= chr(rand(ord('a'), ord('z'))); } foreach ($mimedata as $d) { if (strpos($d, $boundary) !== false) { $boundary = ''; } } } $mime = ''; foreach ($mimedata as $d) { $mime .= "--{$boundary}\r\n{$d}\r\n"; } $mime .= "--{$boundary}--\r\n"; $oauth = new OAuth($twitterCredentials['consumerKey'], $twitterCredentials['consumerSecret']); $oauth->setToken($twitterCredentials['WoWTokens']['accessToken'], $twitterCredentials['WoWTokens']['accessTokenSecret']); $url = 'https://upload.twitter.com/1.1/media/upload.json'; $requestHeader = $oauth->getRequestHeader('POST', $url); $inHeaders = ["Authorization: {$requestHeader}", 'Content-Type: multipart/form-data; boundary=' . $boundary]; $outHeaders = []; $ret = \Newsstand\HTTP::Post($url, $mime, $inHeaders, $outHeaders); if ($ret) { $json = json_decode($ret, true); if (json_last_error() == JSON_ERROR_NONE) { if (isset($json['media_id_string'])) { return $json['media_id_string']; } else { DebugMessage('Parsed JSON response from post to twitter, no media id', E_USER_WARNING); DebugMessage(print_r($json, true), E_USER_WARNING); return false; } } else { DebugMessage('Non-JSON response from post to twitter', E_USER_WARNING); DebugMessage($ret, E_USER_WARNING); return false; } } else { DebugMessage('No/bad response from post to twitter', E_USER_WARNING); DebugMessage(print_r($outHeaders, true), E_USER_WARNING); return false; } }