Skip to content

Instantly share code, notes, and snippets.

@dirkk0
Created June 27, 2013 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dirkk0/5875461 to your computer and use it in GitHub Desktop.
Save dirkk0/5875461 to your computer and use it in GitHub Desktop.
Python scripts to register a OAuth1 client with pump.io, to list your status updates and to post an note. Heavily inspired PyPump; Kudos to https://github.com/xray7224/PyPump. To use it run register.py first.
# Thanks to # https://github.com/xray7224/PyPump
#
import json
import sys
import requests
from requests_oauthlib import OAuth1
try:
config = json.load(open('config.json', 'r'))
except:
print "Couldn't find config.json.\nPlease run register.py first to create the tokens.\nAborting."
sys.exit(1)
userurl = config["baseurl"]+'/api/user/%s/feed' % config["username"]
client = OAuth1(
client_key=config["client_id"],
client_secret=config["client_secret"],
resource_owner_key=config["oauth_token"],
resource_owner_secret=config["oauth_token_secret"]
)
req= requests.get(userurl, auth=client)
# req= requests.get(userurl, auth=client, stream=True)
if req.status_code != 200:
print "STATUS CODE IS NOT 200, BUT", req.status_code
# print req.headers
print "... something went wrong"
else:
server_data = req.json()
print server_data['author']['preferredUsername']
# print server_data['displayName']
for item in server_data['items']:
# print item['object']['objectType']
if item['object']['objectType'] == 'note':
print item['object']['content']
# Thanks to # https://github.com/xray7224/PyPump
#
import json
import sys
import requests
from requests_oauthlib import OAuth1
import urlparse
if len(sys.argv) < 2:
message = "test from post.py"
else:
message = sys.argv[1]
try:
config = json.load(open('config.json', 'r'))
except:
print "Couldn't find config.json.\nPlease run register.py first to create the tokens.\nAborting."
sys.exit(1)
userurl = config["baseurl"]+'/api/user/%s/feed' % config["username"]
client = OAuth1(
client_key=config["client_id"],
client_secret=config["client_secret"],
resource_owner_key=config["oauth_token"],
resource_owner_secret=config["oauth_token_secret"]
)
query = {
"verb":"post",
"object":{
"objectType":"note",
"content":message,
},
}
query = json.dumps(query)
req= requests.post(userurl, data=query, auth=client, headers={'Content-Type': 'application/json'})
data = urlparse.parse_qs(req.content)
print "posted successfully: " + message
# Thanks to # https://github.com/xray7224/PyPump
#
import sys
import json
import urlparse
import requests
from requests_oauthlib import OAuth1
import webbrowser
try:
config = json.load(open('config.json', 'r'))
except:
pass
if config:
print """
There is an existing config file.
If you want to create a new one, then please delete or rename the existing one,
Aborting."""
sys.exit(0)
config = {}
config["baseurl"] = raw_input('Please enter the URL of the server running pump.io: ')
config["username"] = raw_input('Please enter the username of the account you want to use: ')
data = {
"client_name":"myClient",
"type":"client_associate",
"application_type":"web",
}
data = json.dumps(data)
endpoint = config["baseurl"] + "/api/client/register"
request = requests.post(endpoint, headers={'Content-Type': 'application/json'}, data=data)
# print request
server_data = request.json()
# print server_data
config["client_id"] = server_data["client_id"]
config["client_secret"] = server_data["client_secret"]
# expires_at = server_data["expires_at"]
print config
client = OAuth1(
client_key=config["client_id"],
client_secret=config["client_secret"],
callback_uri="oob"
)
req = requests.post(config["baseurl"] + "/oauth/request_token", auth=client)
data = urlparse.parse_qs(req.content)
oauth_token_secret = data['oauth_token_secret'][0]
oauth_token = data['oauth_token'][0]
print oauth_token
print oauth_token_secret
print "To allow us to use your pump.io please follow the instructions at:"
webbrowser.open(config["baseurl"]+"/oauth/authorize?oauth_token=" + oauth_token.decode("utf-8"))
verifier_code = raw_input("Verifier Code: ").lstrip(" ").rstrip(" ")
client = OAuth1(
client_key=config["client_id"],
client_secret=config["client_secret"],
resource_owner_key=oauth_token,
resource_owner_secret=oauth_token_secret,
verifier=verifier_code
)
req = requests.post(config["baseurl"] + "/oauth/access_token", auth=client)
data = urlparse.parse_qs(req.content)
config["oauth_token_secret"] = data['oauth_token_secret'][0]
config["oauth_token"] = data['oauth_token'][0]
print config
open('config.json', 'w').write(json.dumps(config, indent=2, separators=(',', ': ')))
print "config.json successfully written."
@jeorgen
Copy link

jeorgen commented Jan 10, 2020

Thanks for these files, it truly works with my pump.io instance! And it is so elegant and few lines of code.

I had to set "config = False" in the try clause of register.py line 16, so that config is not undefined for the if clause on line 18.

Also my feed did not use "note" as key, may have been a quirk of the posting Pumpa client, not sure.

@jeorgen
Copy link

jeorgen commented Jan 10, 2020

I should note that I have tested register and list, not post yet

@jeorgen
Copy link

jeorgen commented Jan 10, 2020

Tested post now, works fine!

@dirkk0
Copy link
Author

dirkk0 commented Jan 10, 2020

This was created 7 years ago - I had no idea pump.io was still a thing, and I wouldn't have expected that this still works.
Thanks for the notes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment