Google drive api 谷歌网站中的javascript示例存在问题
在将CLIENT_ID更改为我的googleapi访问中的ID之后,我尝试在我的google站点中使用HTML框在页面中测试javascript示例;当我试图保存HTML框时,get无法加载外部url client.js?onload=handleClientLoad消息。如何使用google站点使javascript示例正常工作Google drive api 谷歌网站中的javascript示例存在问题,google-drive-api,Google Drive Api,在将CLIENT_ID更改为我的googleapi访问中的ID之后,我尝试在我的google站点中使用HTML框在页面中测试javascript示例;当我试图保存HTML框时,get无法加载外部url client.js?onload=handleClientLoad消息。如何使用google站点使javascript示例正常工作 <script type="text/javascript"> var CLIENT_ID = 'MY_CLIENT_ID'; var SCO
<script type="text/javascript">
var CLIENT_ID = 'MY_CLIENT_ID';
var SCOPES = 'https://www.googleapis.com/auth/drive';
/**
* Called when the client library is loaded to start the auth flow.
*/
function handleClientLoad() {
window.setTimeout(checkAuth, 1);
}
/**
* Check if the current user has authorized the application.
*/
function checkAuth() {
alert('checkAuth()');
gapi.auth.authorize(
{'client_id': CLIENT_ID, 'scope': SCOPES, 'immediate': true},
handleAuthResult);
}
/**
* Called when authorization server replies.
*
* @param {Object} authResult Authorization result.
*/
function handleAuthResult(authResult) {
alert('handleAuthResult()');
if (authResult && !authResult.error) {
// Access token has been successfully retrieved, requests can be sent to the API.
var filePicker = document.getElementById('filePicker');
filePicker.style.visibility = '';
filePicker.onchange = uploadFile;
} else {
// No access token could be retrieved, force the authorization flow.
gapi.auth.authorize(
{'client_id': CLIENT_ID, 'scope': SCOPES, 'immediate': false},
handleAuthResult);
}
}
/**
* Start the file upload.
*
* @param {Object} evt Arguments from the file selector.
*/
function uploadFile(evt) {
gapi.client.load('drive', 'v2', function() {
var file = evt.target.files[0];
insertFile(file);
});
}
/**
* Insert new file.
*
* @param {File} fileData File object to read data from.
* @param {Function} callback Function to call when the request is complete.
*/
function insertFile(fileData, callback) {
const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
var reader = new FileReader();
reader.readAsBinaryString(fileData);
reader.onload = function(e) {
var contentType = fileData.type || 'application/octet-stream';
var metadata = {
'title': fileData.name,
'mimeType': contentType
};
var base64Data = btoa(reader.result);
var multipartRequestBody =
delimiter +
'Content-Type: application/json\r\n\r\n' +
JSON.stringify(metadata) +
delimiter +
'Content-Type: ' + contentType + '\r\n' +
'Content-Transfer-Encoding: base64\r\n' +
'\r\n' +
base64Data +
close_delim;
var request = gapi.client.request({
'path': '/upload/drive/v2/files',
'method': 'POST',
'params': {'uploadType': 'multipart'},
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
},
'body': multipartRequestBody});
if (!callback) {
callback = function(file) {
console.log(file)
};
}
request.execute(callback);
}
}
</script>
<script type="text/javascript" src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>
<div>
<!--Add a file picker for the user to start the upload process -->
<input type="file" id="filePicker" style="visibility: hidden" />
</div>
var CLIENT_ID='MY_CLIENT_ID';
var作用域https://www.googleapis.com/auth/drive';
/**
*在加载客户端库以启动身份验证流时调用。
*/
函数handleClientLoad(){
setTimeout(checkAuth,1);
}
/**
*检查当前用户是否已授权应用程序。
*/
函数checkAuth(){
警报('checkAuth()');
gapi.auth.authorize(
{'client\u id':client\u id,'scope':SCOPES,'immediate':true},
手工(结果);
}
/**
*当授权服务器答复时调用。
*
*@param{Object}authResult授权结果。
*/
函数handleAuthResult(authResult){
警报('handleAuthResult()');
if(authResult&!authResult.error){
//已成功检索访问令牌,可以向API发送请求。
var filePicker=document.getElementById('filePicker');
filePicker.style.visibility='';
filePicker.onchange=上传文件;
}否则{
//无法检索访问令牌,请强制执行授权流。
gapi.auth.authorize(
{'client\u id':client\u id,'scope':SCOPES,'immediate':false},
手工(结果);
}
}
/**
*开始文件上传。
*
*@param{Object}来自文件选择器的evt参数。
*/
函数上传文件(evt){
load('drive','v2',function(){
var file=evt.target.files[0];
插入文件(文件);
});
}
/**
*插入新文件。
*
*要从中读取数据的@param{File}fileData文件对象。
*@param{Function}在请求完成时调用的回调函数。
*/
函数insertFile(fileData,回调){
常量边界='----314159265358979323846';
常量分隔符=“\r\n--”+边界+“\r\n”;
const close_delim=“\r\n--”+边界+“--”;
var reader=new FileReader();
reader.readAsBinaryString(fileData);
reader.onload=函数(e){
var contentType=fileData.type | |“应用程序/八位字节流”;
变量元数据={
“title”:fileData.name,
“mimeType”:contentType
};
var base64Data=btoa(reader.result);
var multipartRequestBody=
分隔符+
'内容类型:application/json\r\n\r\n'+
stringify(元数据)+
分隔符+
'内容类型:'+contentType+'\r\n'+
'内容传输编码:base64\r\n'+
“\r\n”+
Base64数据+
闭上眼睛;
var request=gapi.client.request({
“路径”:“/upload/drive/v2/files”,
'method':'POST',
'params':{'uploadType':'multipart'},
“标题”:{
'内容类型':'多部分/混合;边界='+边界+''
},
“body”:multipartRequestBody});
如果(!回调){
回调=函数(文件){
console.log(文件)
};
}
执行(回调);
}
}