2013-01-09 21 views
11

Próbuję uzyskać dostęp do prywatnego arkusza kalkulacyjnego Google przy użyciu Javascript. Mam autoryzację z OAuth2.0 i mogę zobaczyć listę wszystkich moich dokumentów na Dysku Google. Czego nie mogę zrobić, to dostać się do konkretnego arkusza kalkulacyjnego. Kod jest następujący przy odpowiednim kodzie arkusza kalkulacyjnego w funkcji "retrieveAllFiles". Wiele z tego jest usuwanych z samouczków Google.Interfejs API Arkuszy Google z OAuth2.0 przy użyciu kodu JavaScript

var clientId = 'working'; 
var apiKey = 'working'; 
var scopes = 'https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/drive https://spreadsheets.google.com/feeds'; 

function handleClientLoad() { 
    console.log('inside handleClientLoad function'); 
    gapi.client.setApiKey(apiKey); 
    window.setTimeout(checkAuth,1); 
} 

function checkAuth() { 
    console.log('inside checkAuth function'); 
    gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult); 
    console.log('finished checkAuth function'); 
} 

function handleAuthResult(authResult) { 
    console.log('inside handleAuthResult function'); 
    var authButton = document.getElementById('authButton'); 
    authButton.style.display = 'none'; 
    if (authResult && !authResult.error) { 
     //Access token has been succesfully retrieved, requests can be sent to the API. 
     apiCalls(); 
    } else { 
     //No access token could be retrieved, show the button to start the authorization flow. 
     authButton.style.display = 'block'; 
     authButton.onclick = function() { 
      gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult); 
     }; 
    } 
} 

function apiCalls() { 
    console.log('inside apiCalls function'); 
    gapi.client.load('drive', 'v2', function() { 
     retrieveAllFiles(callback); 
    }); 
} 

function retrieveAllFiles(callback) { 
    $.get('http://spreadsheets.google.com/feeds/spreadsheets/private/full', function(data) { 
     console.log('get request processed'); 
     console.log(data); 
    }); 
    console.log('inside retrieveAllFiles function'); 
    var retrievePageOfFiles = function(request, result) { 
     request.execute(function(resp) { 
     result = result.concat(resp.items); 
     var nextPageToken = resp.nextPageToken; 
     if (nextPageToken) { 
      request = gapi.client.drive.files.list({ 
       'pageToken': nextPageToken 
      }); 
      retrievePageOfFiles(request, result); 
     } else { 
      callback(result); 
     } 
     }); 
    } 
    var initialRequest = gapi.client.drive.files.list(); 
    retrievePageOfFiles(initialRequest, []); 
} 

function callback(result) { 
    console.log('all should be loaded at this point'); 
    console.log(result); 
    $('#drive-list').append('<ul class="items"></ul>'); 
    $.map(result, function(v,i){ 
     $('.items').append('<li>' + v.title + ':' + v.id + '</li>'); 
    }); 
} 

więc być jasne, końcowy wynik aktualnie znajduje się lista wszystkich moich dokumentów na Dysku Google, ale nie console.log dla „get dane przetwarzane”. Nie dostaję żadnych komunikatów o błędach w konsoli, więc nie mogę powiedzieć, co się dzieje.

Dzięki.

+0

Czy możesz mi powiedzieć, jak uzyskać identyfikator klienta dla arkusza kalkulacyjnego? Dzięki! – VladL

+0

Przejdź do [Google Developers Console] (https://console.developers.google.com/project). Utwórz nowy projekt lub zaloguj się do istniejącego projektu. Na lewym pasku bocznym przejdź do sekcji "Poświadczenia" pod "APIs & auth". Ma sekcję o nazwie 'ClientID' i powinieneś być w stanie dowiedzieć się więcej. – Josh

Odpowiedz

20

Ostateczne rozwiązanie skompilowane z różnych źródeł, mam nadzieję, że to pomoże komuś.

var scopes = 'https://spreadsheets.google.com/feeds'; 
var clientId = 'working'; 
var apiKey = 'working'; 

function handleClientLoad() { 
    console.log('inside handleClientLoad function'); 
    gapi.client.setApiKey(apiKey); 
    window.setTimeout(checkAuth,1); 
} 

function checkAuth() { 
    console.log('inside checkAuth function'); 
    gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult); 
    console.log('finished checkAuth function'); 
} 

function handleAuthResult(authResult) { 
    console.log('inside handleAuthResult function'); 
    console.log(gapi.auth.getToken()); 
    var authButton = document.getElementById('authButton'); 
    authButton.style.display = 'none'; 
    if (authResult && !authResult.error) { 
     //Access token has been successfully retrieved, requests can be sent to the API. 
     loadClient(); 
    } else { 
     //No access token could be retrieved, show the button to start the authorization flow. 
     authButton.style.display = 'block'; 
     authButton.onclick = function() { 
      gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult); 
     }; 
    } 
} 

function loadClient() { 
    console.log('inside loadClient function'); 
    var token = gapi.auth.getToken().access_token; 
    var urlLocation = ''; //Get this from the URL of your Spreadsheet in the normal interface for Google Drive. 

    //This gives a spitout of ALL spreadsheets that the user has access to. 
    var url = 'https://spreadsheets.google.com/feeds/spreadsheets/private/full?access_token=' + token; 

    //This gives a list of all worksheets inside the Spreadsheet, does not give actual data 
    var url = 'https://spreadsheets.google.com/feeds/worksheets/' + urlLocation + '/private/full?access_token=' + token; 

    //This gives the data in a list view - change the word list to cells to work from a cell view and see https://developers.google.com/google-apps/spreadsheets/#working_with_cell-based_feeds for details 
    var url = 'https://spreadsheets.google.com/feeds/list/' + urlLocation + '/od6/private/full?access_token=' + token; 

    console.log(url); 
    $.get(url, function(data) { 
     console.log(data); 
    }); 
} 
+0

Po raz pierwszy otrzymałem błędy "400 Bad Request", gdy pierwszy raz wprowadziłem kod auth_token. Okazało się, że to zły arkusz roboczy. Zadziałało, gdy wstawiłem wartość gid = z adresu docs.google.com dla arkusza kalkulacyjnego. Nie wypróbowałem pełnego adresu URL dla urlLocation. Dokumenty API i większość innych miejsc mówi, że używają wartości key = value z adresu URL dokumentu. Więc jeśli jest to docs.google.com/spreadsheet/ccc?key=blahblah&gid=5, to urlLocation = 'blahblah' i worksheetId = 5 jak w: 'https://spreadsheets.google.com/feeds/list/blahblah/ 5/private/full? Access_token = '+ token – jla

+1

Dostaję się dość daleko z tym przykładem, ale zatrzymują mnie restrykcje bezpieczeństwa w wielu domenach (np. "Nie ma nagłówka" Access-Control-Allow-Origin "itp.) Skąd uruchamiałeś ten skrypt, aby dać JS dostęp do danych odpowiedzi? –

Powiązane problemy