Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork1.7k
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
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionsREADME.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletionsscribejava-apis/src/main/java/com/github/scribejava/apis/MediaWikiApi.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff 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"; | ||
} | ||
} |
70 changes: 70 additions & 0 deletionsscribejava-apis/src/test/java/com/github/scribejava/apis/examples/MediaWikiExample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff 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! :)"); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.