Skip to content

Instantly share code, notes, and snippets.

@nicogaspa
Created June 22, 2019 08:52
Show Gist options
  • Save nicogaspa/e802656bc168438b22028c18f3e89d41 to your computer and use it in GitHub Desktop.
Save nicogaspa/e802656bc168438b22028c18f3e89d41 to your computer and use it in GitHub Desktop.
Snippet of python code using ASK SDK initializing a Google API service
from ask_sdk_core.utils import is_intent_name, is_request_type
from ask_sdk_core.dispatch_components import AbstractRequestHandler
from ask_sdk_model.ui import LinkAccountCard
import httplib2
from googleapiclient.discovery import build
from oauth2client.client import AccessTokenCredentials, AccessTokenCredentialsError
class SummaryHandler(AbstractRequestHandler):
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return is_request_type("IntentRequest")(handler_input)
def handle(self, handler_input):
# type: (HandlerInput) -> Response
access_token = handler_input.request_envelope.context.system.user.access_token
if access_token is None:
handler_input.response_builder.speak("Please log in").set_card(LinkAccountCard())
return handler_input.response_builder.response
credentials = AccessTokenCredentials(access_token, 'alexa-skill/1.0')
http = credentials.authorize(httplib2.Http())
service = build('drive', 'v3', http=http)
results = service.files().list(pageSize=500, fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])
handler_input.response_builder.speak("There are {} files in your Google Drive".format(len(items)))
return handler_input.response_builder.response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment