google drive and the google drive sdk

Download google drive and the google drive sdk

Post on 15-Jan-2015




1 download

Embed Size (px)


Talk di Claudio Cherubino alla DevFest 2012 @ Firenze


  • 1. Google Drive and theGoogle Drive SDKBuilding Drive appsClaudio CherubinoGoogle Drive Developer Relations

2. What is Google Drive?Yea, some marketing :) 3. Access AnywhereGoogle Drive is everywhere you are -- on theweb, in your home, at the office, and on the go.So wherever you are, your stuff is just...there.Ready to go, ready to share.Install it on:PC, Mac, Android, iOS 4. Store your files in a safe placeThings happen. Your phone goes for a swim.Your laptop takes an infinite snooze.No matter what happens to your devices, yourfiles are safely stored in Google Drive. 5. Powerful searchGoogle Drive can search keywordsin your files -- even the text inpictures -- to help you quickly findwhat youre looking for. 6. View anythingWhen a coworker shares a file withyou, you may not have the supportedsoftware on your computer. WithGoogle Drive you can open and viewover 35 file types directly in thebrowser. 7. Access to a world of apps!Google Drive works with the apps thatmake you more efficient.Choose from a growing set ofproductivity applications, integratedright into your Drive. 8. Demos!Pixlr EditorHelloFaxAutoCAD WS 9. Google Drive SDKWhy integrate with Google Drive? 10. Drive SDK opportunity ++Extensive ReachEffortless IntegrationPut your app in front of millions of Get the best of Google Drives sharingusers with billions of files capabilities, storage capacity and user identity management so you can focus on your app 11. Drive SDK featuresDrive Create, Read, List, Manage files through the API Search, sharing and revisionsDrive Web UI Open files and docs with your app directly from Drive 12. Integrating with Google DriveGetting Started 13. Steps for integrating your app w/ DrivePrerequisites: Create a project in the Google APIs Console Enable the Drive API Create OAuth 2.0 credentialsIntegration steps: Auth with OAuth 2.0 [& OpenID Connect] Write code for opening / saving / managing files 14. OAuth 2.0Introduction 15. A cool tool... OAuth 2.0 Playground 16. Integrating with Google DriveHandling Authorization for Web apps 17. AuthorizationUsing OAuth 2.0 - Redirecting users to the Grant screen Java// Instantiating some dependenciesHttpTransport httpTransport = new NetHttpTransport();JacksonFactory jsonFactory = new JacksonFactory();// Building the flow and the redirect URLGoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET, SCOPES).build();GoogleAuthorizationCodeRequestUrl urlBuilder =flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI);// Redirecting users to the grant screenresp.sendRedirect(; 18. AuthorizationUsing OAuth 2.0 - Getting back & exchanging the code in your callback handlerJava// Reading the auth code value from the URLString authorizationCode = req.getParameter("code");// Exchanging the auth code for tokensGoogleTokenResponse tokenResponse =flow.newTokenRequest(authorizationCode).setRedirectUri(REDIRECT_URI).execute();String accessToken = tokenResponse.getAccessToken();String refreshToken = tokenResponse.getRefreshToken(); 19. Integrating with Google DriveHandling Authorization for Android 20. AuthorizationUsing OAuth 2.0 - Using the AccountManagerProblem!Currently not possible: Anonymous tokens are not allowed App auth using API Key is Disabled for Drive for security reasonsSolutionsPermanent: Add app auth to auth authentication in Android (available w/ Google Play services!)Temporarily: Use a WebView and trigger an OAuth 2.0 flowCheck this Google IO presentation for more. 21. AuthorizationUsing OAuth 2.0 - Using a WebViewSteps to do WebView auth: Redirect Users to the grant screen URL in a WebView Use http://localhost as the redirect URI Register a WebViewClient with an onPageStarted method to intercept page change Detect successful/failed authorization and read the auth code from the URL of the WebViewResources w/ code samples: Sample code for complex login system on mobile apps Improved Twitter OAuth for Android 22. Integrating with Google DriveInteracting w/ Drive files 23. Instantiating the Drive service ObjectJava// Building the credentials ObjectGoogleCredentials credentials = flow.createAndStoreCredential(tokenResponse, null);// Here is the Drive serviceDrive service = new Drive.Builder(httpTransport, jsonFactory, credentials); 24. Creating Files Java// Files metadataFile body = new File();body.setTitle(title);body.setDescription(description);body.setMimeType(mimeType);// Files fileContent = new;FileContent mediaContent = new FileContent(mimeType, fileContent);// Executing the requestfile = service.files().insert(body, mediaContent).execute(); 25. Fetching Files Java// Getting files metadataFile file = service.files().get(fileId).execute();// Downloading the files contentif (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) {HttpResponse resp = service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl())).execute();InputStream content = resp.getContent();} 26. Integrating with Google DriveAdding the Drive Web-UI Integration 27. UI Integration - "Create" 28. UI Integration - "Open with" 29. Distribution - Chrome Web Store 30. Steps for adding Drive UI integrationPrerequisites: Create a Chrome Web Store listing (Painful !) Install the application from the CWS Enable and configure the Drive SDK in the Google APIs ConsoleIntegration steps: Support OAuth 2.0 server-side flow Read action and file ID from URL parameter 31. Passing context on open & createWhat happens when somebody launches your app from Drive? URL JSON{"action" : "create","parentId" : "0ADK06pfg"Create actions}{"action" : "open","ids" : ["0Bz0bd"]Open actions} 32. Passing context on open & createWhat happens when somebody launches your app from Drive?URL{"action" : "open","exportIds" : ["0Bz0bd"]Open native Google Docs actions} 33. Integrating with Google DriveAdding the Drive Android App Integration 34. Receiving intents from the Drive app 35. Receiving intents from the Drive app Export an Activity that supports an intent with the following info: action: drive.intent.action.DRIVE_OPEN path: content:// type: MIME type of the file Declare your API Project ID in the Activitys metadata List supported MIME types in the intent-filter element The intent will not include the body of the document nor the user account Retrieve the file from the Drive API using the resourceId provided by the intent in the pathCheck this Google IO presentation for more. 36. Receiving intents from the Drive app XML 37. Receiving intents from the Drive appJava@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);final Intent intent = getIntent();final String action = intent.getAction();if ("drive.intent.action.DRIVE_OPEN".equals(action)) {String fileId = intent.getStringExtra("resourceId");// Prompt the user to choose the account to use and process the file using the Drive API.} else {// Other action.}} 38. Integrating with Google DriveThe Google Picker 39. UI Integration - Embedded file picker 40. Embedding the pickerJSgoogle.setOnLoadCallback(createPicker);google.load(picker, 1);var view = new google.picker.View(google.picker.ViewId.DOCS);view.setMimeTypes("image/png,image/jpeg,image/jpg");function createPicker() { picker = new google.picker.PickerBuilder().enableFeature(google.picker.Feature.MULTISELECT_ENABLED).setAppId(YOUR_APP_ID).addView(view).setCallback(pickerCallback).build(); picker.setVisible(true);} 41. Handling picker selectionsJS// A simple callback implementation.function pickerCallback(data) {if (data.action == google.picker.Action.PICKED) {var fileId =[0].id;alert(The user selected:+ fileId);}} 42. Security and other features 43. A brief (but important) note on securityThe Drive SDK has 2 security models. The first one gives you full access to the users Drive Read and/or write all files Manage all files List all files ... 44. A brief (but important) note on securityDrives second security model is a per-file security model More restrictive file level access Simple rules - an app can open any file that: The user opened with the app through the Drive UI The user opened with the app through the Picker API The app created itself 45. Other Features / tips & tricks Resumable upload & download Indexable text Search! Conversions to native Google Documents Export of native Google Documents in many formats OCR Revisions List installed apps Copy files, Trash files, Touch files Folders User Permissions Shortcuts Auto-generated MIME type Contentless Indexable & syncable! 46.


View more >