-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from halfak/master
Refactoring, + examples and fixed py3 bug
- Loading branch information
Showing
8 changed files
with
303 additions
and
190 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
0.2.0 | ||
0.2.1 |
This file contains 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,23 @@ | ||
import sys;sys.path.insert(0, ".") | ||
from mwoauth import ConsumerToken, initiate, complete, identify | ||
from six.moves import input # For compatibility between python 2 and 3 | ||
|
||
# Consruct a "consumer" from the key/secret provided by MediaWiki | ||
import config # You'll need to provide this | ||
consumer_token = ConsumerToken(config.consumer_key, config.consumer_secret) | ||
mw_uri = "https://en.wikipedia.org/w/index.php" | ||
|
||
# Step 1: Initialize -- ask MediaWiki for a temporary key/secret for user | ||
redirect, request_token = initiate(mw_uri, consumer_token) | ||
|
||
# Step 2: Authorize -- send user to MediaWiki to confirm authorization | ||
print("Point your browser to: %s" % redirect) # | ||
response_qs = input("Response query string: ") | ||
|
||
# Step 3: Complete -- obtain authorized key/secret for "resource owner" | ||
access_token = complete(mw_uri, consumer_token, request_token, response_qs) | ||
print(str(access_token)) | ||
|
||
# Step 4: Identify -- (optional) get identifying information about the user | ||
identity = identify(mw_uri, consumer_token, access_token) | ||
print("Identified as {username} (id={sub}).".format(**identity)) |
This file contains 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
This file contains 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,44 @@ | ||
from mwoauth import ConsumerToken, Handshaker | ||
import requests | ||
from requests_oauthlib import OAuth1 | ||
from six.moves import input # For compatibility between python 2 and 3 | ||
|
||
# Consruct a "consumer" from the key/secret provided by MediaWiki | ||
import config # You'll need to provide this | ||
consumer_token = ConsumerToken(config.consumer_key, config.consumer_secret) | ||
|
||
# Construct handshaker with wiki URI and consumer | ||
handshaker = Handshaker("https://en.wikipedia.org/w/index.php", | ||
consumer_token) | ||
|
||
# Step 1: Initialize -- ask MediaWiki for a temporary key/secret for user | ||
redirect, request_token = handshaker.initiate() | ||
|
||
# Step 2: Authorize -- send user to MediaWiki to confirm authorization | ||
print("Point your browser to: %s" % redirect) # | ||
response_qs = input("Response query string: ") | ||
|
||
# Step 3: Complete -- obtain authorized key/secret for "resource owner" | ||
access_token = handshaker.complete(request_token, response_qs) | ||
|
||
# Construct an auth object with the consumer and access tokens | ||
auth1 = OAuth1(consumer_token.key, | ||
client_secret=consumer_token.secret, | ||
resource_owner_key=access_token.key, | ||
resource_owner_secret=access_token.secret) | ||
|
||
# Now, accessing the API on behalf of a user | ||
print("Reading top 10 watchlist items") | ||
response = requests.get( | ||
"https://en.wikipedia.org/w/api.php", | ||
params = { | ||
'action': "query", | ||
'list': "watchlist", | ||
'wllimit': 10, | ||
'wlprop': "title|comment", | ||
'format': "json" | ||
}, | ||
auth=auth1 | ||
) | ||
for item in response.json()['query']['watchlist']: | ||
print("{title}\t{comment}".format(**item)) |
This file contains 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,5 @@ | ||
"""Provides a collection of utilities for easily working with MediaWiki's | ||
OAuth1.0a implementation.""" | ||
from .handshaker import Handshaker | ||
from .tokens import AccessToken, ConsumerToken, RequestToken | ||
from .functions import initiate, complete, identify |
Oops, something went wrong.