User:CSteipp (WMF)/OAuth demo client

<?php

if ( PHP_SAPI !== 'cli' ) { die( "CLI-only test script\n" ); }

/** * A basic client for overall testing */

function wfDebugLog( $method, $msg) { echo "[$method] $msg\n"; }

require '~/code/extensions/OAuth/lib/OAuth.php';

$consumerKey = ''; $consumerSecret = '';

$baseurl = 'https:// /index.php?title=Special:MWOAuth';

$initiateUrl = $baseurl. '/initiate&format=json&oauth_callback=oob'; $tokenUrl = $baseurl. '/token&format=json';

// Get a temporary token $c = new OAuthConsumer( $consumerKey, $consumerSecret ); $parsed = parse_url( $initiateUrl ); $params = array; parse_str($parsed['query'], $params); $req_req = OAuthRequest::from_consumer_and_token($c, NULL, "GET", $initiateUrl, $params); $hmac_method = new OAuthSignatureMethod_HMAC_SHA1; $sig_method = $hmac_method; $req_req->sign_request($sig_method, $c, NULL);

$headers = array( $req_req->to_header );

echo "Calling: $initiateUrl\n(With OAuth headers): {$headers[0]}\n\n";

$ch = curl_init; curl_setopt( $ch, CURLOPT_URL, $initiateUrl ); curl_setopt( $ch, CURLOPT_PORT, 443 ); curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 ); curl_setopt( $ch, CURLOPT_HEADER, 0 ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $data = curl_exec( $ch ); if( !$data ) { 'Curl error: '. curl_error( $ch ); } echo "Returned: $data\n\n"; $token = json_decode( $data );

// Send the user off to Authorize our App $authorizeUrl = $baseurl. "/authorize&oauth_token={$token->key}&oauth_consumer_key=$consumerKey"; print "Send your user to $authorizeUrl\n";

// Get the Verifi print "Enter the verification code:\n"; $fh = fopen( "php://stdin", "r" ); $line = fgets( $fh );

$rc = new OAuthConsumer( $token->key, $token->secret ); $parsed = parse_url( $tokenUrl ); parse_str($parsed['query'], $params); $params['oauth_verifier'] = trim($line);

$acc_req = OAuthRequest::from_consumer_and_token($c, $rc, "GET", $tokenUrl, $params); $acc_req->sign_request($sig_method, $c, $rc);

echo "Calling: $acc_req\n";

unset( $ch ); $ch = curl_init; curl_setopt( $ch, CURLOPT_URL, (string) $acc_req ); curl_setopt( $ch, CURLOPT_PORT, 443 ); curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 ); curl_setopt( $ch, CURLOPT_HEADER, 0 ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); $data = curl_exec( $ch ); if( !$data ) { 'Curl error: '. curl_error( $ch ); }

echo "Returned: $data\n\n";