<s> import json <EOL> import urllib <EOL> import urllib2 <EOL> import gdata <EOL> import gdata . gauth <EOL> import gdata . docs <EOL> import gdata . data <EOL> import gdata . docs . client <EOL> import gdata . docs . data <EOL> from django . conf import settings <EOL> oauth2_token = gdata . gauth . OAuth2Token ( <EOL> client_id = settings . GOOGLE_CLIENT_ID , <EOL> client_secret = settings . GOOGLE_CLIENT_SECRET , <EOL> scope = '<STR_LIT:U+0020>' . join ( <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) , <EOL> user_agent = '<STR_LIT>' ) <EOL> redirect_uri = oauth2_token . generate_authorize_url ( <EOL> redirect_uri = settings . GOOGLE_STEP2_URI , <EOL> access_type = '<STR_LIT>' , approval_prompt = '<STR_LIT>' ) <EOL> def get_refreshed_token ( token ) : <EOL> data = urllib . urlencode ( { <EOL> '<STR_LIT>' : settings . GOOGLE_CLIENT_ID , <EOL> '<STR_LIT>' : settings . GOOGLE_CLIENT_SECRET , <EOL> '<STR_LIT>' : token . refresh_token , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> request = urllib2 . Request ( <EOL> url = '<STR_LIT>' , <EOL> data = data ) <EOL> request_open = urllib2 . urlopen ( request ) <EOL> response = request_open . read ( ) <EOL> request_open . close ( ) <EOL> tokens = json . loads ( response ) <EOL> token . access_token = tokens [ '<STR_LIT>' ] <EOL> return token <EOL> def google_export_xls ( filename , title , token , blob = True ) : <EOL> if blob : <EOL> token = gdata . gauth . token_from_blob ( token ) <EOL> if token . refresh_token is not None and token . access_token is not None : <EOL> oauth2_token . refresh_token = token . refresh_token <EOL> working_token = get_refreshed_token ( oauth2_token ) <EOL> docs_client = gdata . docs . client . DocsClient ( <EOL> source = oauth2_token . user_agent ) <EOL> docs_client = working_token . authorize ( docs_client ) <EOL> xls_doc = gdata . docs . data . Resource ( <EOL> type = '<STR_LIT>' , title = title ) <EOL> media = gdata . data . MediaSource ( ) <EOL> media . SetFileHandle ( filename , '<STR_LIT>' ) <EOL> xls_doc = docs_client . CreateResource ( xls_doc , media = media ) <EOL> return xls_doc . find_html_link ( ) </s>
