2013-01-09 24 views
11

का उपयोग कर मैं जावास्क्रिप्ट का उपयोग कर एक निजी Google स्प्रेडशीट तक पहुंचने का प्रयास कर रहा हूं। मैंने OAuth2.0 के साथ सफलतापूर्वक अधिकृत किया है और मेरे सभी Google ड्राइव दस्तावेज़ों की एक सूची देख सकता हूं। जो मैं नहीं कर सकता वह एक विशिष्ट स्प्रेडशीट में मिलता है। कोड "retrieveAllFiles" फ़ंक्शन में प्रासंगिक स्प्रेडशीट कोड के साथ निम्नानुसार है। इनमें से बहुत सारे Google ट्यूटोरियल से खींचा गया है।जावा स्प्रेडशीट्स एपीआई OAuth2.0 के साथ जावास्क्रिप्ट

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>'); 
    }); 
} 

तो स्पष्ट होना, अंतिम परिणाम वर्तमान में मेरे सभी Google डिस्क डॉक्स की एक सूची है, लेकिन के लिए कोई console.log "डेटा संसाधित"। मुझे कंसोल में कोई त्रुटि संदेश नहीं मिल रहा है, इसलिए मैं यह नहीं बता सकता कि क्या हो रहा है।

धन्यवाद।

+0

क्या आप कृपया मुझे बता सकते हैं कि क्लाइंट आईडी को स्प्रेडशीट्स के लिए कैसे प्राप्त करें? धन्यवाद! – VladL

+0

[Google के डेवलपर कंसोल] पर जाएं (https://console.developers.google.com/project)। या तो एक नई परियोजना बनाएं या किसी मौजूदा प्रोजेक्ट में लॉगिन करें। बाएं साइडबार पर, 'एपीआई और एथ' के तहत 'प्रमाण पत्र' नामक अनुभाग पर जाएं। इसमें 'क्लाइंट आईडी' नामक एक अनुभाग है और आपको वहां और जानने में सक्षम होना चाहिए। – Josh

उत्तर

20

अंतिम समाधान विभिन्न स्रोतों के समूह से संकलित अंतिम उम्मीद है, उम्मीद है कि यह किसी की मदद करेगा।

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

जब मैंने पहली बार auth_token में रखा था तो मुझे फिर से "400 खराब अनुरोध" त्रुटियां मिलनी शुरू हुईं। यह एक खराब वर्कशीट आईडी बन गया। जब मैंने स्प्रेड शीट के लिए docs.google.com url से gid = के मान में डाल दिया तो यह काम करता था। मैंने urlLocation के लिए पूरा यूआरएल नहीं आज़माया। एपीआई दस्तावेज़ और अधिकांश अन्य स्थान दस्तावेज़ URL से कुंजी = मान का उपयोग करने के लिए कहते हैं। तो यदि यह docs.google.com/spreadsheet/ccc?key=blahblah&gid=5 है, तो urlLocation = 'blahblah' और worksheetId = 5 जैसा है: 'https://spreadsheets.google.com/feeds/list/blahblah/ 5/निजी/पूर्ण? Access_token = '+ टोकन – jla

+1

तो, मुझे इस उदाहरण के साथ बहुत दूर मिल गया है, लेकिन क्रॉस-डोमेन सुरक्षा प्रतिबंध मुझे रोकते हैं (यानी "नहीं' एक्सेस-कंट्रोल-अनुमति-उत्पत्ति 'हेडर मौजूद है" आदि) आपके जेएस को प्रतिक्रिया डेटा तक पहुंचने के लिए आपने इस स्क्रिप्ट को कहाँ से चलाया? –