2015-08-25 11 views
5

Voglio visualizzare tutti i file in Google selettore per che sto seguendo questo link:La chiave API sviluppatore non è valido durante la visualizzazione di file in Google selettore

https://developers.google.com/picker/docs/

ma restituisce l'API la chiave dello sviluppatore non è valida.

Il mio codice è il seguente, ma la chiave sta funzionando correttamente in altri progetti:

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/> 
    <title>Google Picker Example</title> 

    <script type="text/javascript"> 

    // The Browser API key obtained from the Google Developers Console. 
    // Replace with your own Browser API key, or your own key. 
    var developerKey = 'AXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXM'; 

    // The Client ID obtained from the Google Developers Console. Replace with your own Client ID. 
    var clientId = "XXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com" 

    // Replace with your own App ID. (Its the first number in your Client ID) 
    var appId = "XXXXXXXXXXXX"; 

    // Scope to use to access user's Drive items. 
    var scope = ['https://www.googleapis.com/auth/drive']; 

    var pickerApiLoaded = false; 
    var oauthToken; 

    // Use the Google API Loader script to load the google.picker script. 
    function loadPicker() { 
     gapi.load('auth', {'callback': onAuthApiLoad}); 
     gapi.load('picker', {'callback': onPickerApiLoad}); 
    } 

    function onAuthApiLoad() { 
     window.gapi.auth.authorize(
      { 
      'client_id': clientId, 
      'scope': scope, 
      'immediate': false 
      }, 
      handleAuthResult); 
    } 

    function onPickerApiLoad() { 
     pickerApiLoaded = true; 
     createPicker(); 
    } 

    function handleAuthResult(authResult) { 
     if (authResult && !authResult.error) { 
     oauthToken = authResult.access_token; 
     createPicker(); 
     } 
    } 

    // Create and render a Picker object for searching images. 
    function createPicker() { 
     if (pickerApiLoaded && oauthToken) { 
     var view = new google.picker.View(google.picker.ViewId.DOCS); 
     view.setMimeTypes("image/png,image/jpeg,image/jpg"); 
     var picker = new google.picker.PickerBuilder() 
      .enableFeature(google.picker.Feature.NAV_HIDDEN) 
      .enableFeature(google.picker.Feature.MULTISELECT_ENABLED) 
      .setAppId(appId) 
      .setOAuthToken(oauthToken) 
      .addView(view) 
      .addView(new google.picker.DocsUploadView()) 
      .setDeveloperKey(developerKey) 
      .setCallback(pickerCallback) 
      .build(); 
     picker.setVisible(true); 
     } 
    } 

    // A simple callback implementation. 
    function pickerCallback(data) { 
     if (data.action == google.picker.Action.PICKED) { 
     var fileId = data.docs[0].id; 
     alert('The user selected: ' + fileId); 
     } 
    } 
    </script> 
    </head> 
    <body> 
    <div id="result"></div> 

    <!-- The Google API Loader script. --> 
    <script type="text/javascript" src="https://apis.google.com/js/api.js?onload=loadPicker"></script> 
    </body> 
</html> 
+1

Che tipo di credenziali di fatto si aggiunge alla console degli sviluppatori di google? – DaImTo

+0

Mi sembra di avere lo stesso problema. Le credenziali che ho sono una chiave API dove Type = "Browser". Questo è quello che sto passando in setDeveloperKey. Ho anche un ID client OAuth 2.0 dove Type = "Web application". Sto passando clientId.split ('-') [0] per setAppId. setOAuthToken viene passato un token di autenticazione valido che sono in grado di utilizzare per recuperare i file dall'API di Drive. picker.setVisible (true) genera un popup che dice semplicemente "La chiave sviluppatore API non è valida." Se rimuovo la chiamata a setDeveloperKey, il picker funziona. – Kwateco

risposta

3

Sembra non è stato attivato "Google picker API" nella console google