Skip to content

Commit 60d37fe

Browse files
Add MediaWiki API
This API can be used for any MediaWiki installation that has the OAuth extension [1] installed. Two default instances are provided for convenience: one for wikis hosted by the Wikimedia Foundation (including Wikipedia), which is the one most users will be interested in, and one for the Wikimedia Foundation’s Beta Cluster, which is more appropriate for testing purposes. The example is copied and slightly adapted from AWeberExample.java. [1]: https://www.mediawiki.org/wiki/Extension:OAuth
1 parent 47c0732 commit 60d37fe

3 files changed

Lines changed: 141 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ ScribeJava support out-of-box several HTTP clients:
6363
* Microsoft Azure Active Directory (Azure AD) (http://azure.microsoft.com/)
6464
* Microsoft Live (https://login.live.com/)
6565
* Mail.Ru (https://mail.ru/)
66+
* MediaWiki (https://www.mediawiki.org/)
6667
* Meetup (http://www.meetup.com/)
6768
* NAVER (http://www.naver.com/)
6869
* NetEase (http://www.163.com/)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.github.scribejava.apis;
2+
3+
import com.github.scribejava.core.builder.api.DefaultApi10a;
4+
5+
public class MediaWikiApi extends DefaultApi10a {
6+
7+
private static final MediaWikiApi WIKIMEDIA_INSTANCE = new MediaWikiApi(
8+
"https://meta.wikimedia.org/w/index.php",
9+
"https://meta.wikimedia.org/wiki/"
10+
);
11+
12+
private static final MediaWikiApi WIKIMEDIA_BETA_INSTANCE = new MediaWikiApi(
13+
"https://meta.wikimedia.beta.wmflabs.org/w/index.php",
14+
"https://meta.wikimedia.beta.wmflabs.org/wiki/"
15+
);
16+
17+
private final String indexUrl;
18+
private final String niceUrlBase;
19+
20+
/**
21+
* @param indexUrl The URL to the index.php of the wiki.
22+
* Due to <a href="https://phabricator.wikimedia.org/T59500">a MediaWiki bug</a>,
23+
* some requests must currently use the non-nice URL.
24+
* @param niceUrlBase The base of nice URLs for the wiki, including the trailing slash.
25+
* Due to <a href="https://phabricator.wikimedia.org/T74186">another MediaWiki bug</a>,
26+
* some requests must currently use the nice URL.
27+
*/
28+
public MediaWikiApi(final String indexUrl, final String niceUrlBase) {
29+
this.indexUrl = indexUrl;
30+
this.niceUrlBase = niceUrlBase;
31+
}
32+
33+
/**
34+
* The instance for wikis hosted by the Wikimedia Foundation.
35+
* Consumers are requested on
36+
* <a href="https://meta.wikimedia.org/wiki/Special:OAuthConsumerRegistration/propose">
37+
* Special:OAuthConsumerRegistration/propose
38+
* </a>.
39+
*/
40+
public static MediaWikiApi wikimediaInstance() {
41+
return WIKIMEDIA_INSTANCE;
42+
}
43+
44+
/**
45+
* The instance for wikis in the Wikimedia Foundation’s Beta Cluster.
46+
* Consumers are requested on
47+
* <a href="https://meta.wikimedia.beta.wmflabs.org/wiki/Special:OAuthConsumerRegistration/propose">
48+
* Special:OAuthConsumerRegistration/propose
49+
* </a>.
50+
*/
51+
public static MediaWikiApi wikimediaBetaInstance() {
52+
return WIKIMEDIA_BETA_INSTANCE;
53+
}
54+
55+
@Override
56+
public String getRequestTokenEndpoint() {
57+
return indexUrl + "?title=Special:OAuth/initiate";
58+
}
59+
60+
@Override
61+
public String getAuthorizationBaseUrl() {
62+
return niceUrlBase + "Special:OAuth/authorize";
63+
}
64+
65+
@Override
66+
public String getAccessTokenEndpoint() {
67+
return indexUrl + "?title=Special:OAuth/token";
68+
}
69+
70+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.github.scribejava.apis.examples;
2+
3+
import java.util.Scanner;
4+
import com.github.scribejava.core.builder.ServiceBuilder;
5+
import com.github.scribejava.apis.MediaWikiApi;
6+
import com.github.scribejava.core.model.OAuth1AccessToken;
7+
import com.github.scribejava.core.model.OAuth1RequestToken;
8+
import com.github.scribejava.core.model.OAuthRequest;
9+
import com.github.scribejava.core.model.Response;
10+
import com.github.scribejava.core.model.Verb;
11+
import com.github.scribejava.core.oauth.OAuth10aService;
12+
import java.io.IOException;
13+
import java.util.concurrent.ExecutionException;
14+
15+
public final class MediaWikiExample {
16+
17+
// To get your consumer key/secret, see https://meta.wikimedia.org/wiki/Special:OAuthConsumerRegistration/propose
18+
private static final String CONSUMER_KEY = "";
19+
private static final String CONSUMER_SECRET = "";
20+
21+
private static final String API_USERINFO_URL =
22+
"https://meta.wikimedia.org/w/api.php?action=query&format=json&meta=userinfo";
23+
24+
private MediaWikiExample() {
25+
}
26+
27+
public static void main(String... args) throws IOException, InterruptedException, ExecutionException {
28+
final OAuth10aService service = new ServiceBuilder(CONSUMER_KEY)
29+
.apiSecret(CONSUMER_SECRET)
30+
.build(MediaWikiApi.wikimediaInstance());
31+
32+
final Scanner in = new Scanner(System.in);
33+
34+
System.out.println("=== MediaWiki's OAuth Workflow ===");
35+
System.out.println();
36+
37+
// Obtain the Request Token
38+
System.out.println("Fetching the Request Token...");
39+
final OAuth1RequestToken requestToken = service.getRequestToken();
40+
System.out.println("Got the Request Token!");
41+
System.out.println();
42+
43+
System.out.println("Now go and authorize ScribeJava here:");
44+
System.out.println(service.getAuthorizationUrl(requestToken));
45+
System.out.println("And paste the verifier here");
46+
System.out.print(">>");
47+
final String oauthVerifier = in.nextLine();
48+
System.out.println();
49+
50+
// Trade the Request Token and Verfier for the Access Token
51+
System.out.println("Trading the Request Token for an Access Token...");
52+
final OAuth1AccessToken accessToken = service.getAccessToken(requestToken, oauthVerifier);
53+
System.out.println("Got the Access Token!");
54+
System.out.println("(The raw response looks like this: " + accessToken.getRawResponse() + "')");
55+
System.out.println();
56+
57+
// Now let's go and ask for a protected resource!
58+
System.out.println("Now we're going to access a protected resource...");
59+
final OAuthRequest request = new OAuthRequest(Verb.GET, API_USERINFO_URL);
60+
service.signRequest(accessToken, request);
61+
final Response response = service.execute(request);
62+
System.out.println("Got it! Lets see what we found...");
63+
System.out.println();
64+
System.out.println(response.getBody());
65+
66+
System.out.println();
67+
System.out.println("Thats it man! Go and build something awesome with MediaWiki and ScribeJava! :)");
68+
}
69+
70+
}

0 commit comments

Comments
 (0)