Skip to content

Instantly share code, notes, and snippets.

@bryanhelmig
Created August 2, 2012 22:26
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 bryanhelmig/3241340 to your computer and use it in GitHub Desktop.
Save bryanhelmig/3241340 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
"""
first, install requests and oauth hook:
> sudo pip install requests requests-oauth -U
then run:
> python qbrequests.py
expected failure rate is about 50%, and is seemingly random.
"""
import requests
import urllib
from oauth_hook import OAuthHook
QUICKBOOKS_CONSUMER_KEY = 'xxx'
QUICKBOOKS_CONSUMER_SECRET = 'xxx'
ACCESS_TOKEN = 'xxx'
ACCESS_TOKEN_SECRET = 'xxx'
BASE_URL = 'https://qbo.intuit.com/qboNN/'
REALM_ID = 'xxx'
# print out the raw, concated header
def new_authorization_header(oauth_params):
"""Return Authorization header"""
authorization_headers = 'OAuth '
# order in a specific manner...
keys = ['oauth_consumer_key', 'oauth_token', 'oauth_nonce', 'oauth_timestamp',
'oauth_signature_method', 'oauth_signature', 'oauth_version']
items = ((k, oauth_params[k]) for k in keys)
authorization_headers += ','.join(['{0}="{1}"'.format(k, urllib.quote(str(v))) for k, v in items])
print authorization_headers
return authorization_headers
OAuthHook.authorization_header = staticmethod(new_authorization_header)
qbsession = requests.session(headers={'Host': 'qbo.intuit.com',
'Content-Type': 'application/x-www-form-urlencoded'},
hooks={'pre_request': OAuthHook(access_token=ACCESS_TOKEN,
access_token_secret=ACCESS_TOKEN_SECRET,
consumer_key=QUICKBOOKS_CONSUMER_KEY,
consumer_secret=QUICKBOOKS_CONSUMER_SECRET,
header_auth=True)})
for x in range(10):
URL = BASE_URL + 'resource/customers/v2/{0}'.format(REALM_ID)
response = qbsession.post(url=URL,
params={'Sort': 'CreateTime NewestToOldest'},
data={})
try:
response.raise_for_status()
print 'Successful request!'
except:
print 'Failed request:', response
print 'request headers:', response.request.headers
print 'response content:', response.content
print # spacer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment