Skip to content

Commit

Permalink
Updated oauth with better error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
guyc committed Jun 4, 2013
1 parent 144b41b commit ef388b6
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions gaugette/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import sys
import urllib
import httplib
try:
import httplib # python2
except ImportError:
import http.client # python3
import os.path
import json
import time
import gdata.spreadsheet.service
import gdata.docs.service

class OAuth:

Expand All @@ -22,9 +26,15 @@ def __init__(self, client_id, client_secret):
'https://docs.google.com/feeds', # if an application needs to create spreadsheets, or otherwise manipulate their metadata,
]
self.host = 'accounts.google.com'
self.reset_connection()
self.load_token()

# this setup is isolated because it eventually generates a BadStatusLine
# exception, after which we always get httplib.CannotSendRequest errors.
# When this happens, we try re-creating the exception.
def reset_connection(self):
# httplib.HTTPConnection.debuglevel = 1
self.conn = httplib.HTTPSConnection(self.host)
self.load_token()

def load_token(self):
token = None
Expand Down Expand Up @@ -60,8 +70,8 @@ def get_user_code(self):
self.verification_url = data['verification_url']
self.retry_interval = data['interval']
else:
print response.status
print response.read()
print(response.status)
print(response.read())
sys.exit()
return self.user_code

Expand Down Expand Up @@ -128,3 +138,11 @@ def spreadsheet_service(self):
}
client = gdata.spreadsheet.service.SpreadsheetsService(additional_headers=headers)
return client

def docs_service(self):
headers = {
"Authorization": "%s %s" % (self.token['token_type'], self.token['access_token'])
}
client = gdata.docs.service.DocsService(additional_headers=headers)
return client

0 comments on commit ef388b6

Please sign in to comment.