-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdemo.php
More file actions
75 lines (59 loc) · 2.48 KB
/
demo.php
File metadata and controls
75 lines (59 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
include 'MWOAuthClient.php';
$consumerKey = 'b02833d71fe5700c891c531fe041c36c';
$consumerSecret = 'f2b6f6ccc47e9f6b298eaa74789897b98dc6a594';
// Configure the connection to the wiki you want to use. Passing title=Special:OAuth as a
// GET parameter makes the signature easier. Otherwise you need to call
// $client->setExtraParam('title','Special:OAuth/whatever') for each step.
// If your wiki uses wgSecureLogin, the canonicalServerUrl will point to http://
$config = new MWOAuthClientConfig(
'https://localhost/w/index.php?title=Special:OAuth', // url to use
true, // do we use SSL? (we should probably detect that from the url)
false // do we validate the SSL certificate? Always use 'true' in production.
);
$config->canonicalServerUrl = 'http://localhost';
$config->redirURL = 'https://localhost/view/Special:OAuth?';
$cmrToken = new OAuthToken( $consumerKey, $consumerSecret );
$client = new MWOAuthClient( $config, $cmrToken );
// Step 1 - Get a request token
list( $redir, $requestToken ) = $client->initiate();
// Step 2 - Have the user authorize your app. Get a verifier code from them.
// (if this was a webapp, you would redirect your user to $redir, then use the 'oauth_verifier'
// GET parameter when the user is redirected back to the callback url you registered.
echo "Point your browser to: $redir\n\n";
print "Enter the verification code:\n";
$fh = fopen( "php://stdin", "r" );
$verifyCode = trim( fgets( $fh ) );
// Step 3 - Exchange the request token and verification code for an access token
$accessToken = $client->complete( $requestToken, $verifyCode );
// You're done! You can now identify the user, and/or call the API (examples below) with $accessToken
// If we want to authenticate the user
$identity = $client->identify( $accessToken );
echo "Authenticated user {$identity->username}\n";
// Do a simple API call
echo "Getting user info: ";
echo $client->makeOAuthCall(
$accessToken,
'https://localhost/wiki/api.php?action=query&meta=userinfo&uiprop=rights&format=json'
);
// Make an Edit
$editToken = json_decode( $client->makeOAuthCall(
$accessToken,
'https://localhost/wiki/api.php?action=tokens&format=json'
) )->tokens->edittoken;
$apiParams = array(
'action' => 'edit',
'title' => 'Talk:Main_Page',
'section' => 'new',
'summary' => 'Hello World',
'text' => 'Hi',
'token' => $editToken,
'format' => 'json',
);
$client->setExtraParams( $apiParams ); // sign these too
echo $client->makeOAuthCall(
$accessToken,
'https://localhost/wiki/api.php',
true,
$apiParams
);