· 7 years ago · Feb 28, 2018, 12:20 PM
1<!DOCTYPE html>
2<html xmlns="http://www.w3.org/1999/xhtml">
3<head>
4 <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
5 <title>Google Picker Example</title>
6
7 <script type="text/javascript">
8
9 // The Browser API key obtained from the Google API Console.
10 // Replace with your own Browser API key, or your own key.
11 var developerKey = 'AIzaSyCHrCoqRS0dFnjJvBalGRyZ9N9tGoA_7gk';
12
13 // The Client ID obtained from the Google API Console. Replace with your own Client ID.
14 var clientId = "980522751508-arhr74bvn5shgqlaq5rjhbnundvh6g6p.apps.googleusercontent.com"
15
16 // Replace with your own project number from console.developers.google.com.
17 // See "Project number" under "IAM & Admin" > "Settings"
18 var appId = "980522751508";
19
20 // Scope to use to access user's Drive items.
21 var scope = ['https://www.googleapis.com/auth/drive'];
22
23 var pickerApiLoaded = false;
24 var oauthToken;
25
26 // Use the Google API Loader script to load the google.picker script.
27 function loadPicker() {
28 gapi.load('auth', {'callback': onAuthApiLoad});
29 gapi.load('picker', {'callback': onPickerApiLoad});
30 }
31
32 function onAuthApiLoad() {
33 window.gapi.auth.authorize(
34 {
35 'client_id': clientId,
36 'scope': scope,
37 'immediate': false
38 },
39 handleAuthResult);
40 }
41
42 function onPickerApiLoad() {
43 pickerApiLoaded = true;
44 createPicker();
45 }
46
47 function handleAuthResult(authResult) {
48 if (authResult && !authResult.error) {
49 oauthToken = authResult.access_token;
50 createPicker();
51 }
52 }
53
54 // Create and render a Picker object for searching images.
55 function createPicker() {
56 if (pickerApiLoaded && oauthToken) {
57 var view = new google.picker.View(google.picker.ViewId.DOCS);
58 // view.setMimeTypes("image/png,image/jpeg,image/jpg");
59 var picker = new google.picker.PickerBuilder()
60 .enableFeature(google.picker.Feature.NAV_HIDDEN)
61 .enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
62 .setAppId(appId)
63 .setOAuthToken(oauthToken)
64 .addView(view)
65 .addView(new google.picker.DocsUploadView())
66 .setDeveloperKey(developerKey)
67 .setCallback(pickerCallback)
68 .build();
69 picker.setVisible(true);
70 }
71 }
72
73 // A simple callback implementation.
74 function pickerCallback(data) {
75 if (data.action == google.picker.Action.PICKED) {
76 console.log(data);
77 var fileId = data.docs[0].id;
78 // alert('The user selected: ' + fileId);
79 }
80 }
81 </script>
82</head>
83<body>
84<div id="result"></div>
85
86<!-- The Google API Loader script. -->
87<script type="text/javascript" src="https://apis.google.com/js/api.js?onload=loadPicker"></script>
88</body>
89</html>