Javascript Google drive多部分上传文件返回错误403(确定)
我正在尝试使用ClientID将文件上载到驱动器。我得到以下错误,说403(好) 调用POST 403(确定)时出错 我查阅了的文档,但仍然没有找到解决方案 这是我的全部代码Javascript Google drive多部分上传文件返回错误403(确定),javascript,google-drive-api,Javascript,Google Drive Api,我正在尝试使用ClientID将文件上载到驱动器。我得到以下错误,说403(好) 调用POST 403(确定)时出错 我查阅了的文档,但仍然没有找到解决方案 这是我的全部代码 <html> <head> <script type="text/javascript"> var CLIENT_ID = <CLIENT_ID>; var SCOPES = ['https://www.googleapis.com/auth/dr
<html> <head>
<script type="text/javascript">
var CLIENT_ID = <CLIENT_ID>;
var SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly'];
/**
* Check if current user has authorized this application.
*/
function checkAuth() {
gapi.auth.authorize(
{
'client_id': CLIENT_ID,
'scope': SCOPES.join(' '),
'immediate': true
}, handleAuthResult);
}
/**
* Handle response from authorization server.
*
* @param {Object} authResult Authorization result.
*/
function handleAuthResult(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.
*/
function handleAuthClick(event) {
gapi.auth.authorize(
{client_id: CLIENT_ID, scope: SCOPES, immediate: false},
handleAuthResult);
return false;
}
/**
* Load Drive API client library.
*/
function loadDriveApi() {
gapi.client.load('drive', 'v2', insertFile(callback));
}
function callback(){
console.log("It works.");
}
/**
* 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(callback) {
const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function() {
var reader = new FileReader();
reader.onloadend = function () {
var fileData =reader.result;
var contentType = "image/jpg";
var metadata = {
'title': "InstaImage.jpg",
'mimeType': contentType
};
var base64Data = btoa(fileData);
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);
console.log(fileData);
};
reader.readAsDataURL(xhr.response);
};
xhr.open('GET', "https://scontent.cdninstagram.com/hphotos-xpt1/t51.2885-15/e15/11324950_950675781667048_1239101466_n.jpg");
xhr.send();
/**
* 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.
*/
function appendPre(message) {
var pre = document.getElementById('output');
var textContent = document.createTextNode(message + '\n');
pre.appendChild(textContent);
}
</script>
<script src="https://apis.google.com/js/client.js?onload=checkAuth">
</script> </head> <body> <div id="authorize-div" style="display: none">
<span>Authorize access to Drive API</span>
<!--Button for the user to click to initiate auth sequence -->
<button id="authorize-button" onclick="handleAuthClick(event)">
Authorize
</button> </div> <pre id="output"></pre> </body> </html>
var CLIENT_ID=;
变量作用域=['https://www.googleapis.com/auth/drive.metadata.readonly'];
/**
*检查当前用户是否已授权此应用程序。
*/
函数checkAuth(){
gapi.auth.authorize(
{
“客户id”:客户id,
“scope”:SCOPES.join(“”),
“立即”:真
},handleAuthResult);
}
/**
*处理来自授权服务器的响应。
*
*@param{Object}authResult授权结果。
*/
函数handleAuthResult(authResult){
var authorizeDiv=document.getElementById('authorized-div');
if(authResult&!authResult.error){
//隐藏身份验证UI,然后加载客户端库。
authorizeDiv.style.display='none';
loadDriveApi();
}否则{
//显示授权UI,允许用户通过
//单击“授权”按钮。
authorizeDiv.style.display='inline';
}
}
/**
*响应用户单击“授权”按钮,启动身份验证流。
*
*@param{Event}Event按钮单击事件。
*/
函数handleAuthClick(事件){
gapi.auth.authorize(
{client_id:client_id,scope:SCOPES,immediate:false},
手工(结果);
返回false;
}
/**
*加载驱动器API客户端库。
*/
函数loadDriveApi(){
load('drive','v2',insertFile(回调));
}
函数回调(){
log(“它可以工作”);
}
/**
*插入新文件。
*
*要从中读取数据的@param{File}fileData文件对象。
*@param{Function}在请求完成时调用的回调函数。
*/
函数insertFile(回调){
常量边界='----314159265358979323846';
常量分隔符=“\r\n--”+边界+“\r\n”;
const close_delim=“\r\n--”+边界+“--”;
var xhr=new XMLHttpRequest();
xhr.responseType='blob';
xhr.onload=函数(){
var reader=new FileReader();
reader.onloadend=函数(){
var fileData=reader.result;
var contentType=“image/jpg”;
变量元数据={
“title”:“InstaImage.jpg”,
“mimeType”:contentType
};
var base64Data=btoa(fileData);
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(文件)
};
}
执行(回调);
console.log(文件数据);
};
reader.readAsDataURL(xhr.response);
};
xhr.open('GET',”https://scontent.cdninstagram.com/hphotos-xpt1/t51.2885-15/e15/11324950_950675781667048_1239101466_n.jpg");
xhr.send();
/**
*将pre元素附加到包含给定消息的正文中
*作为其文本节点。
*
*@param{string}要放置在pre元素中的消息文本。
*/
函数appendPre(消息){
var pre=document.getElementById('output');
var textContent=document.createTextNode(message+'\n');
pre.appendChild(文本内容);
}
授权访问驱动器API
授权
我的代码有任何错误。
403-拒绝访问
-你确定你使用的是正确的客户端ID
和正确的作用域吗?嗨,谢谢你回答我的问题。我只是检查了我的客户端ID和所有的作用域。我认为每个作用域都是正确的。但我刚发现当我第一次启动应用程序时,我接收另一个错误。加载资源失败:服务器响应状态为403(确定)。