Skip to content

Instantly share code, notes, and snippets.

@MrMaksimize
Forked from voxpelli/vptest.info
Last active December 24, 2015 12:49
Show Gist options
  • Save MrMaksimize/6800201 to your computer and use it in GitHub Desktop.
Save MrMaksimize/6800201 to your computer and use it in GitHub Desktop.
name = VoxPelli OAuth Test
core = 7.x
dependencies[] = oauth_common
dependencies[] = http_client
<?php
// Provider
define("T_PROVIDER_OAUTH_URL", 'http://cm22master.dev/oauth/');
// Consumer
define("T_CONSUMER_KEY", 'FZGaB5xujYoyqHZCkA5ATdQvapn68gip');
define("T_CONSUMER_SECRET", 'W2iwYiMXrFdiRjQfRGrtDRn2C9zhsmqd');
define("T_CONSUMER_CALLBACK_URL", 'http://cm22slave.dev/oauth/authorized/1');
function vptest_menu() {
$items['vptest/request'] = array(
'page callback' => '_vptest_test_request',
'access arguments' => array('access content'),
);
$items['vptest/access'] = array(
'page callback' => '_vptest_test_access',
'access arguments' => array('access content'),
);
return $items;
}
function _vptest_test_request() {
$consumer = DrupalOAuthConsumer::load(T_CONSUMER_KEY, FALSE);
if (!$consumer) {
$consumer = new DrupalOAuthConsumer(T_CONSUMER_KEY, T_CONSUMER_SECRET);
$consumer->write();
}
$sig_method = DrupalOAuthClient::signatureMethod();
$client = new DrupalOAuthClient($consumer, NULL, $sig_method);
$request_token = $client->getRequestToken(T_PROVIDER_OAUTH_URL . 'request_token', array(
'callback' => 'http://d7.local/vptest/access',
));
$request_token->write();
$_SESSION['vptest_request_key'] = $request_token->key;
$auth_url = $client->getAuthorizationUrl(T_PROVIDER_OAUTH_URL . 'authorize', array(
'callback' => T_CONSUMER_CALLBACK_URL,
));
drupal_goto($auth_url);
}
function _vptest_test_access() {
$consumer = DrupalOAuthConsumer::load(T_CONSUMER_KEY, FALSE);
$request_token = DrupalOAuthToken::loadByKey($_GET['oauth_token'], $consumer, OAUTH_COMMON_TOKEN_TYPE_REQUEST);
$client = new DrupalOAuthClient($consumer, $request_token);
$verifier = isset($_GET['oauth_verifier']) ? $_GET['oauth_verifier'] : NULL;
$access_token = $client->getAccessToken(T_PROVIDER_OAUTH_URL . 'access_token', array('verifier' => $verifier));
$request_token->delete();
$sig_method = DrupalOAuthClient::signatureMethod();
$auth = new HttpClientOAuth($consumer, $access_token, $sig_method, TRUE, TRUE);
$formatter = new HttpClientBaseFormatter(HttpClientBaseFormatter::FORMAT_JSON);
$client = new HttpClient($auth, $formatter);
// @TODO abstract this
$result = $client->post('http://sandbox.local/oauthlogin/api/user/info');
print '<pre>';
var_dump($result);
print '</pre>'; die;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment