Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add MediaWiki API#852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
kullfar merged 1 commit intoscribejava:masterfromlucaswerkmeister:mediawiki
May 23, 2018
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletionsREADME.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,6 +63,7 @@ ScribeJava support out-of-box several HTTP clients:
* Microsoft Azure Active Directory (Azure AD) (http://azure.microsoft.com/)
* Microsoft Live (https://login.live.com/)
* Mail.Ru (https://mail.ru/)
* MediaWiki (https://www.mediawiki.org/)
* Meetup (http://www.meetup.com/)
* NAVER (http://www.naver.com/)
* NetEase (http://www.163.com/)
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
package com.github.scribejava.apis;

import com.github.scribejava.core.builder.api.DefaultApi10a;

public class MediaWikiApi extends DefaultApi10a {

private static final MediaWikiApi WIKIMEDIA_INSTANCE = new MediaWikiApi(
"https://meta.wikimedia.org/w/index.php",
"https://meta.wikimedia.org/wiki/"
);

private static final MediaWikiApi WIKIMEDIA_BETA_INSTANCE = new MediaWikiApi(
"https://meta.wikimedia.beta.wmflabs.org/w/index.php",
"https://meta.wikimedia.beta.wmflabs.org/wiki/"
);

private final String indexUrl;
private final String niceUrlBase;

/**
* @param indexUrl The URL to the index.php of the wiki.
* Due to <a href="https://phabricator.wikimedia.org/T59500">a MediaWiki bug</a>,
* some requests must currently use the non-nice URL.
* @param niceUrlBase The base of nice URLs for the wiki, including the trailing slash.
* Due to <a href="https://phabricator.wikimedia.org/T74186">another MediaWiki bug</a>,
* some requests must currently use the nice URL.
*/
public MediaWikiApi(final String indexUrl, final String niceUrlBase) {
this.indexUrl = indexUrl;
this.niceUrlBase = niceUrlBase;
}

/**
* The instance for wikis hosted by the Wikimedia Foundation.
* Consumers are requested on
* <a href="https://meta.wikimedia.org/wiki/Special:OAuthConsumerRegistration/propose">
* Special:OAuthConsumerRegistration/propose
* </a>.
*/
public static MediaWikiApi wikimediaInstance() {
return WIKIMEDIA_INSTANCE;
}

/**
* The instance for wikis in the Wikimedia Foundation’s Beta Cluster.
* Consumers are requested on
* <a href="https://meta.wikimedia.beta.wmflabs.org/wiki/Special:OAuthConsumerRegistration/propose">
* Special:OAuthConsumerRegistration/propose
* </a>.
*/
public static MediaWikiApi wikimediaBetaInstance() {
return WIKIMEDIA_BETA_INSTANCE;
}

@Override
public String getRequestTokenEndpoint() {
return indexUrl + "?title=Special:OAuth/initiate";
}

@Override
public String getAuthorizationBaseUrl() {
return niceUrlBase + "Special:OAuth/authorize";
}

@Override
public String getAccessTokenEndpoint() {
return indexUrl + "?title=Special:OAuth/token";
}

}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
package com.github.scribejava.apis.examples;

import java.util.Scanner;
import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.apis.MediaWikiApi;
import com.github.scribejava.core.model.OAuth1AccessToken;
import com.github.scribejava.core.model.OAuth1RequestToken;
import com.github.scribejava.core.model.OAuthRequest;
import com.github.scribejava.core.model.Response;
import com.github.scribejava.core.model.Verb;
import com.github.scribejava.core.oauth.OAuth10aService;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

public final class MediaWikiExample {

// To get your consumer key/secret, see https://meta.wikimedia.org/wiki/Special:OAuthConsumerRegistration/propose
private static final String CONSUMER_KEY = "";
private static final String CONSUMER_SECRET = "";

private static final String API_USERINFO_URL =
"https://meta.wikimedia.org/w/api.php?action=query&format=json&meta=userinfo";

private MediaWikiExample() {
}

public static void main(String... args) throws IOException, InterruptedException, ExecutionException {
final OAuth10aService service = new ServiceBuilder(CONSUMER_KEY)
.apiSecret(CONSUMER_SECRET)
.build(MediaWikiApi.wikimediaInstance());

final Scanner in = new Scanner(System.in);

System.out.println("=== MediaWiki's OAuth Workflow ===");
System.out.println();

// Obtain the Request Token
System.out.println("Fetching the Request Token...");
final OAuth1RequestToken requestToken = service.getRequestToken();
System.out.println("Got the Request Token!");
System.out.println();

System.out.println("Now go and authorize ScribeJava here:");
System.out.println(service.getAuthorizationUrl(requestToken));
System.out.println("And paste the verifier here");
System.out.print(">>");
final String oauthVerifier = in.nextLine();
System.out.println();

// Trade the Request Token and Verfier for the Access Token
System.out.println("Trading the Request Token for an Access Token...");
final OAuth1AccessToken accessToken = service.getAccessToken(requestToken, oauthVerifier);
System.out.println("Got the Access Token!");
System.out.println("(The raw response looks like this: " + accessToken.getRawResponse() + "')");
System.out.println();

// Now let's go and ask for a protected resource!
System.out.println("Now we're going to access a protected resource...");
final OAuthRequest request = new OAuthRequest(Verb.GET, API_USERINFO_URL);
service.signRequest(accessToken, request);
final Response response = service.execute(request);
System.out.println("Got it! Lets see what we found...");
System.out.println();
System.out.println(response.getBody());

System.out.println();
System.out.println("Thats it man! Go and build something awesome with MediaWiki and ScribeJava! :)");
}

}

[8]ページ先頭

©2009-2025 Movatter.jp