Created
June 12, 2015 15:48
-
-
Save matthewbednarski/82c1cb9600f3f8743eaa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Enter a client ID for a web application from the Google Developer Console. | |
// The provided clientId will only work if the sample is run directly from | |
// https://google-api-javascript-client.googlecode.com/hg/samples/authSample.html | |
// In your Developer Console project, add a JavaScript origin that corresponds to the domain | |
// where you will be running the script. | |
var clientId = | |
'clientId'; | |
// Enter the API key from the Google Develoepr Console - to handle any unauthenticated | |
// requests in the code. | |
// The provided key works for this sample only when run from | |
// https://google-api-javascript-client.googlecode.com/hg/samples/authSample.html | |
// To use in your own application, replace this API key with your own. | |
var apiKey = 'apikey'; | |
// To enter one or more authentication scopes, refer to the documentation for the API. | |
var scopes = "https://www.googleapis.com/auth/drive"; | |
// Use a button to handle authentication the first time. | |
function checkAuth() { | |
gapi.auth.authorize({ | |
client_id: clientId, | |
scope: scopes, | |
immediate: true | |
}, handleAuthResult); | |
} | |
// Load the API and make an API call. Display the results on the screen. | |
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('authorize-button'); | |
authButton.style.display = 'none'; | |
if (authResult && !authResult.error) { | |
//Access token has been successfully retrieved, requests can be sent to the API. | |
console.log("loading Client"); | |
loadClient(); | |
} else { | |
console.error(authResult.error); | |
//No access token could be retrieved, show the button to start the authorization flow. | |
authButton.style = ''; | |
authButton.onclick = function() { | |
gapi.auth.authorize({ | |
client_id: clientId, | |
scope: scopes, | |
immediate: false | |
}, handleAuthResult); | |
}; | |
} | |
} | |
function gapifyUrl(url){ | |
var token = gapi.auth.getToken().access_token; | |
if(url.indexOf('?') > 0){ | |
url += '&access_token=' + token; | |
}else{ | |
url += '?access_token=' + token; | |
} | |
return url; | |
} | |
function loadClient() { | |
console.log('inside loadClient function'); | |
var urlLocation = '17jhiW-w2gX277_XNkmGndWqYaqwzHuFJkWKeSQ1mWgw'; //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/values?alt=json-in-script&callback=googleDocCallback'; | |
url = gapifyUrl(url); | |
console.log(url); | |
$.ajax({ | |
url: url, | |
type: 'GET', | |
crossDomain: true | |
}) | |
.done(function(data) {}) | |
.fail(function(a, b, c, d) { | |
console.error(a); | |
console.error(b); | |
console.error(c); | |
console.error(d); | |
}); | |
} | |
function parseRowData(entry) { | |
var ret = {}; | |
_.forOwn(entry, function(value, key) { | |
if (key.indexOf('gsx$') === 0) { | |
var skey = key.split('gsx$')[1]; | |
if (value.hasOwnProperty('$t')) { | |
this[skey] = value['$t']; | |
} else { | |
this[skey] = value; | |
} | |
} | |
}, ret); | |
return ret; | |
} | |
function newRowTemplate(row) { | |
var ret = {}; | |
for (var key in row) { | |
ret[key] = undefined; | |
} | |
return ret; | |
} | |
function parseSheetData(sheet) { | |
var feed = sheet.feed; | |
var entry = feed.entry; | |
var link = feed.link; | |
var ret = {}; | |
var links = _.chain(link) | |
.filter(function(item) { | |
return item.href !== undefined; | |
}) | |
.filter(function(item) { | |
return item.rel !== undefined; | |
}) | |
.filter(function(item) { | |
return item.rel.indexOf('#') > 0; | |
}) | |
.map(function(item) { | |
var r = {}; | |
if (item.hasOwnProperty('rel')) { | |
if (item.rel.indexOf('http:') === 0) { | |
var parts = item.rel.split('#'); | |
var key = parts[1]; | |
r[key] = item.href; | |
} | |
} | |
return r; | |
}) | |
.value(); | |
ret.links = links; | |
_.forEach(ret.links, function(link){ | |
for(var key in link){ | |
this[key] = link[key]; | |
} | |
}, ret); | |
var rows = _.chain(entry) | |
.map(parseRowData) | |
.value(); | |
ret.data = rows; | |
ret.row_template = newRowTemplate(rows[0]); | |
console.log(ret); | |
tryAddRow(ret); | |
return ret; | |
} | |
function tryAddRow( ret ) { | |
var rowC = _.clone(ret.row_template); | |
for(var k in rowC){ | |
rowC[k] = _.uniqueId(k + '_'); | |
} | |
var url = ret.post; | |
url = gapifyUrl(url); | |
console.log(url); | |
$.ajax({ | |
url: url, | |
type: 'POST', | |
crossDomain: true | |
}) | |
.done(function(data) { | |
console.log(data); | |
}) | |
.fail(function(a, b, c, d) { | |
console.error(a); | |
console.error(b); | |
console.error(c); | |
console.error(d); | |
}); | |
} | |
function googleDocCallback(data, a, b, c) { | |
console.log(data); | |
var ret = parseSheetData(data); | |
console.log(ret); | |
return ret; | |
}; | |
function makeApiCall() { | |
gapi.client.load('drive', 'v2', function() { | |
var request = gapi.client.request({ | |
'path': '/drive/v2/files/17jhiW-w2gX277_XNkmGndWqYaqwzHuFJkWKeSQ1mWgw', | |
'method': 'GET', | |
'params': { | |
'maxResults': '100' | |
} | |
}); | |
// var request = gapi.client.drive.files.list(); | |
request.execute(function(resp) { | |
console.log(resp); | |
var url = 'http://spreadsheets.google.com/feeds/list/' + resp.id + '/Sheet1/public/basic?alt=json-in-script&callback=myFunc'; | |
$.ajax( | |
url | |
) | |
.done(function(data) { | |
if (console && | |
console.log) { | |
console.log( | |
"Sample of data:", | |
data.slice( | |
0, | |
100)); | |
} | |
}); | |
}); | |
}); | |
} | |
var gapi = function() { | |
this.listFiles = function() { | |
var request = gapi.client.request({ | |
'path': '/drive/v2/files', | |
'method': 'GET', | |
'params': { | |
'maxResults': '100' | |
} | |
}); | |
request.execute(function(resp) { | |
console.log(resp); | |
var heading = document.createElement('h4'); | |
heading.appendChild(document.createTextNode( | |
resp.selfLink)); | |
for (var i = 0; i < resp.items.length; i++) { | |
var item = resp.items[i]; | |
var p = document.createElement( | |
'p'); | |
var anchor = document.createElement( | |
'a'); | |
var href = document.createAttribute( | |
'href'); | |
heading.appendChild(p); | |
p.appendChild(anchor); | |
anchor.setAttributeNode(href); | |
href.value = item.alternateLink; | |
anchor.text = item.title; | |
} | |
document.getElementById('content').appendChild( | |
heading); | |
}); | |
}; | |
return this; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset='utf-8' /> | |
<script src="http://code.jquery.com/jquery-git2.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.9.3/lodash.js"></script> | |
<script src="gapi.local.js"></script> | |
</head> | |
<body> | |
<!--Add a button for the user to click to initiate auth sequence --> | |
<button id="authorize-button" style="visibility: hidden">Authorize</button> | |
<script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script> | |
<div id="content"></div> | |
<p>Retrieves your profile name using the Google Plus API.</p> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment