import argparse
import httplib2
import os
import sys
import json
from apiclient import discovery
from oauth2client import file
from oauth2client import client
from oauth2client import tools
# Define sample variables.
_BUCKET_NAME = 'mybucket'
_API_VERSION = 'v1'
# Parser for command-line arguments.
parser = argparse.ArgumentParser(
    description=__doc__,
    formatter_class=argparse.RawDescriptionHelpFormatter,
    parents=[tools.argparser])
# CLIENT_SECRETS is name of a file containing the OAuth 2.0 information for this
# application, including client_id and client_secret. You can see the Client ID
# and Client secret on the APIs page in the Cloud Console:
CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), 'client_secrets.json')
# Set up a Flow object to be used for authentication.
# Add one or more of the following scopes. PLEASE ONLY ADD THE SCOPES YOU
# NEED. For more information on using scopes please see
FLOW = client.flow_from_clientsecrets(CLIENT_SECRETS,
  scope=[
    ],
    message=tools.message_if_missing(CLIENT_SECRETS))
def index():
  cliente=CLIENT_SECRETS
  flow= FLOW
#    va= main(sys.argv)
   # Parse the command-line flags.
#  flags = parser.parse_args(argv[1:])
    # If the credentials don't exist or are invalid run through the native client
  # flow. The Storage object will ensure that if successful the good
  # credentials will get written back to the file.
  storage = file.Storage('sample.dat')
  credentials = storage.get()
  if credentials is None or credentials.invalid:
    credentials = tools.run_flow(FLOW, storage, flags)
  # Create an httplib2.Http object to handle our HTTP requests and authorize it
  # with our good Credentials.
  http = httplib2.Http()
  http = credentials.authorize(http)
  # Construct the service object for the interacting with the Cloud Storage API.
  service = discovery.build('storage', _API_VERSION, http=http)
  try:
    req = service.buckets().get(bucket=_BUCKET_NAME)
    resp = req.execute()
    print1= json.dumps(resp, indent=2)
   
    fields_to_return = 'nextPageToken,items(name,size,contentType,metadata(my-key))'
    req = service.objects().list(bucket=_BUCKET_NAME, fields=fields_to_return)
    # If you have too many items to list in one request, list_next() will
    # automatically handle paging with the pageToken.
    while req is not None:
      resp = req.execute()
      print2= json.dumps(resp, indent=2)
      req = service.objects().list_next(req, resp)
  except client.AccessTokenRefreshError:
    aviso= "The credentials have been revoked or expired, please re-run the application to re-authorize"
    
  form=SQLFORM(db.gfile)
  return dict(print1=print1,print2=print2, form=form)