Skip to content

Commit

Permalink
Merge pull request #7 from halfak/master
Browse files Browse the repository at this point in the history
Refactoring, + examples and fixed py3 bug
  • Loading branch information
halfak committed Jun 3, 2014
2 parents ef9a472 + 45c2bf4 commit 022eaf3
Show file tree
Hide file tree
Showing 8 changed files with 303 additions and 190 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.0
0.2.1
23 changes: 23 additions & 0 deletions examples/functions.py
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))
3 changes: 2 additions & 1 deletion demo.py → examples/handshaker.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import sys;sys.path.insert(0, ".")
from mwoauth import ConsumerToken, Handshaker
from six.moves import input # For compatibility between python 2 and 3

# Consruct a "consumer" from the key/secret provided by MediaWiki
import config
import config # You'll need to provide this
consumer_token = ConsumerToken(config.consumer_key, config.consumer_secret)

# Construct handshaker with wiki URI and consumer
Expand Down
44 changes: 44 additions & 0 deletions examples/request-oauthlib.py
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))
5 changes: 5 additions & 0 deletions mwoauth/__init__.py
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
Loading

0 comments on commit 022eaf3

Please sign in to comment.