def drive(): # Initialize client object to use Google api. # You need client_secrets.json downloaded and stored # in the current working directory flow = ece568helper.get_client_object(_addr, _port, SCOPES) auth_uri = flow.step1_get_authorize_url() if (not(request.query.code)): redirect(auth_uri) auth_code = request.query.code credentials = flow.step2_exchange(auth_code) ece568helper.output_helper('credentials', credentials) http_auth = credentials.authorize(httplib2.Http()) profile_service = build('plus', 'v1', http=http_auth) people_resource = profile_service.people() people_document = people_resource.get(userId='me').execute() ece568helper.output_helper('profile', people_document) drive_service = build('drive', 'v2', http=http_auth) filename = 'profile.out' media_body = MediaFileUpload(filename, mimetype='text/plain', resumable=True) body = { 'title': 'profile_out', 'description': 'Profile information excluding email address', 'mimeType': 'text/plain' } try: file = drive_service.files().insert(body=body, media_body=media_body).execute() return template('drive', result='success') except errors.HttpError, error: return template('drive', result='fail')
def drive(): # Initialize client object to use Google api. # You need client_secrets.json downloaded and stored # in the current working directory flow = ece568helper.get_client_object(_addr, _port, SCOPES) if 'access_denied' in request.query_string: # user denied access return template('drive', result='fail') if 'code' not in request.query_string: # redirect user to Google's OAuth 2.0 server inside this if # to avoid endlessly repeating auth_uri = flow.step1_get_authorize_url() return redirect(auth_uri) # handle OAuth 2.0 server response (error or auth code) # FormsDict has virtual attributes to access any keyword auth_code = request.query.code # exchange authorization code for access token credentials = flow.step2_exchange(auth_code) ece568helper.output_helper('credentials', credentials) # apply access token to an Http object http_auth = credentials.authorize(httplib2.Http()) # build service object plus_service = build('plus', 'v1', http=http_auth) # obtain basic profile info of authenticated user people_resource = plus_service.people() people_document = people_resource.get(userId='me').execute() ece568helper.output_helper('profile', people_document) # upload profile file to Google Drive media_body = MediaFileUpload(filename='profile.out', mimetype='text/plain', resumable=True) body = { 'title': 'profile.out', 'description': 'Profile information excluding email address', 'mimeType': 'text/plain' } # might want to first handle the request then redirect to another URL that # doesn't include response params (like auth code) as per Google documentation try: drive_service = build('drive', 'v2', http=http_auth) drive_service.files().insert( body=body, media_body=media_body).execute() return template('drive', result='success') except errors.HttpError, error: return template('drive', result='fail: '+str(error))
def drive(): # Initialize client object to use Google api. # You need client_secrets.json downloaded and stored # in the current working directory flow = ece568helper.get_client_object(_addr, _port, SCOPES) auth_code = request.query.code error = request.query.error # handle authorization fail if error: return template('drive', result=error) # request authorization if needed if not auth_code: auth_uri = flow.step1_get_authorize_url() redirect(auth_uri) else: credentials = flow.step2_exchange(auth_code) #store credentials ece568helper.output_helper('credentials', credentials) http_auth = credentials.authorize(httplib2.Http()) # build services profile_service = build('plus', 'v1', http=http_auth) drive_service = build('drive', 'v2', http=http_auth) # retrieve and save user profile profile = profile_service.people().get(userId='me').execute() ece568helper.output_helper('profile', profile) # upload profile to google drive media_body = MediaFileUpload('profile.out', mimetype='text/plain', resumable=True) body = { 'title': 'profile.out', 'description': 'Profile information excluding email address', 'mimeType': 'text/plain' } try: file = drive_service.files().insert(body=body,media_body=media_body).execute() return template('drive', result='success') except errors.HttpError, error: return template('drive', result='fail')
def drive(): FILENAME = 'profile.out' MIMETYPE = 'text/plain' TITLE = 'profile.out' DESCRIPTION = 'Profile information excluding email address' code = request.GET.get("code") flow = ece568helper.get_client_object(_addr, _port, SCOPES) if not code: auth_uri = flow.step1_get_authorize_url() redirect(auth_uri) else: credentials = flow.step2_exchange(code) ece568helper.output_helper('credentials', credentials) try: http = httplib2.Http() credentials.authorize(http) drive_service = apiclient.discovery.build('drive', 'v2', http=http) plus_service = apiclient.discovery.build('plus', 'v1', http=http) people_service = plus_service.people() people_document = people_service.get(userId='me').execute() ece568helper.output_helper('profile', people_document) media_body = apiclient.http.MediaFileUpload( FILENAME, mimetype=MIMETYPE, resumable=True ) body = { 'title': TITLE, 'description': DESCRIPTION, } new_file = drive_service.files().insert(body=body, media_body=media_body).execute() return template('drive', result='success') except errors.HttpError as error: return template('drive', result='fail')
def drive(): # Initialize client object to use Google api. # You need client_secrets.json downloaded and stored # in the current working directory flow = ece568helper.get_client_object(_addr, _port, SCOPES) auth_uri = flow.step1_get_authorize_url() params = dict(request.params) if not params or not params['code']: redirect(auth_uri) return template('drive', result='fail') auth_code = params['code'] credentials = flow.step2_exchange(auth_code) # http_auth = credentials.authorize(httplib2.Http()) ece568helper.output_helper('credentials', credentials) # TODO - upload return template('drive', result='fail')
def drive(): # Initialize client object to use Google api. # You need client_secrets.json downloaded and stored # in the current working directory flow = ece568helper.get_client_object(_addr, _port, SCOPES) auth_uri = flow.step1_get_authorize_url() if 'error' in request.query: return template('drive', result=request.query.error) if 'code' not in request.query: auth_uri = flow.step1_get_authorize_url() redirect(auth_uri) else: auth_code = request.query.code credentials = flow.step2_exchange(auth_code) ece568helper.output_helper("credentials", credentials) http_auth = credentials.authorize(httplib2.Http()) plusService = build("plus", "v1", http=http_auth) driveService = build("drive", "v2", http=http_auth) people_document = plusService.people().get(userId = "me").execute() ece568helper.output_helper('profile', people_document) mediaBody = MediaFileUpload('profile.out', mimetype='text/plain', resumable=True) requestBody = { 'title': 'profile.out', 'description': 'myProfile', 'mimeType': 'text/plain' } try: uploadedFile = driveService.files().insert( body = requestBody, mediaBody = media_body ).execute() return template('drive', result = 'Upload done with result: \n %s' % uploadedFile) except errors.HttpError, error: return template('drive', result = 'An error occured: %s' % error)