Save Text From Textarea To Google Drive Using Javascript
I'm trying to upload text typed into a Textarea to a Google Drive file (mimetype: text/x-java for java source code.) I've tried to incorporate the update and insert methods menti
Solution 1:
try checking out and try the quickstart for javascript.
<html><head><scripttype="text/javascript">// Your Client ID can be retrieved from your project in the Google// Developer Console, https://console.developers.google.comvarCLIENT_ID = '<YOUR_CLIENT_ID>';
varSCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly'];
/**
* Check if current user has authorized this application.
*/functioncheckAuth() {
gapi.auth.authorize(
{
'client_id': CLIENT_ID,
'scope': SCOPES.join(' '),
'immediate': true
}, handleAuthResult);
}
/**
* Handle response from authorization server.
*
* @param {Object} authResult Authorization result.
*/functionhandleAuthResult(authResult) {
var authorizeDiv = document.getElementById('authorize-div');
if (authResult && !authResult.error) {
// Hide auth UI, then load client library.
authorizeDiv.style.display = 'none';
loadDriveApi();
} else {
// Show auth UI, allowing the user to initiate authorization by// clicking authorize button.
authorizeDiv.style.display = 'inline';
}
}
/**
* Initiate auth flow in response to user clicking authorize button.
*
* @param {Event} event Button click event.
*/functionhandleAuthClick(event) {
gapi.auth.authorize(
{client_id: CLIENT_ID, scope: SCOPES, immediate: false},
handleAuthResult);
returnfalse;
}
/**
* Load Drive API client library.
*/functionloadDriveApi() {
gapi.client.load('drive', 'v3', listFiles);
}
/**
* Print files.
*/functionlistFiles() {
var request = gapi.client.drive.files.list({
'pageSize': 10,
'fields': "nextPageToken, files(id, name)"
});
request.execute(function(resp) {
appendPre('Files:');
var files = resp.files;
if (files && files.length > 0) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
appendPre(file.name + ' (' + file.id + ')');
}
} else {
appendPre('No files found.');
}
});
}
/**
* Append a pre element to the body containing the given message
* as its text node.
*
* @param {string} message Text to be placed in pre element.
*/functionappendPre(message) {
var pre = document.getElementById('output');
var textContent = document.createTextNode(message + '\n');
pre.appendChild(textContent);
}
</script><scriptsrc="https://apis.google.com/js/client.js?onload=checkAuth"></script></head><body><divid="authorize-div"style="display: none"><span>Authorize access to Drive API</span><!--Button for the user to click to initiate auth sequence --><buttonid="authorize-button"onclick="handleAuthClick(event)">
Authorize
</button></div><preid="output"></pre></body></html>
This would be useful way to familiarize yourself to the code and some required steps when using Drive API. Hope it helps.
Solution 2:
I took a look at the network tab for trying to save the doc to google, and it looks like it's returning an authentication error, have you looked through their client auth docs? https://developers.google.com/drive/v2/web/auth/web-client#handling_authorization_requests
Here's the error,
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}
Post a Comment for "Save Text From Textarea To Google Drive Using Javascript"