Skip to content

Instantly share code, notes, and snippets.

@bdone
Forked from voxpelli/vptest.info
Created April 30, 2012 02:22
Show Gist options
  • Save bdone/2554965 to your computer and use it in GitHub Desktop.
Save bdone/2554965 to your computer and use it in GitHub Desktop.
OAuth.module 3.x example - a test I made to verify the D7 port of the module - should work well on D6 as well though
name = VoxPelli OAuth Test
core = 7.x
dependencies[] = oauth_common
dependencies[] = http_client
<?php
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('bVAU7jXKSL7vgnyayuiLZiGXZfoevZ8R', FALSE);
if (!$consumer) {
$consumer = new DrupalOAuthConsumer('bVAU7jXKSL7vgnyayuiLZiGXZfoevZ8R', 'N7LiwonhxUFs7RrwxEnizc2Doc7afPRx');
$consumer->write();
}
$sig_method = DrupalOAuthClient::signatureMethod();
$client = new DrupalOAuthClient($consumer, NULL, $sig_method);
$request_token = $client->getRequestToken('http://sandbox.local/oauth/request_token', array(
'callback' => 'http://d7.local/vptest/access',
));
$request_token->write();
$_SESSION['vptest_request_key'] = $request_token->key;
$auth_url = $client->getAuthorizationUrl('http://sandbox.local/oauth/authorize', array(
'callback' => 'http://d7.local/vptest/access',
));
drupal_goto($auth_url);
}
function _vptest_test_access() {
$consumer = DrupalOAuthConsumer::load('bVAU7jXKSL7vgnyayuiLZiGXZfoevZ8R', 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('http://sandbox.local/oauth/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);
$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