javascript-在Google Drive API上简单上传的身份验证问题
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-在Google Drive API上简单上传的身份验证问题,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1420字,纯文字阅读大概需要3分钟。
内容图文
![javascript-在Google Drive API上简单上传的身份验证问题](/upload/InfoBanner/zyjiaocheng/658/8c6acd1f023f4ba49d536ad839c23de4.jpg)
我正在尝试将文件上传到我的Google云端硬盘文件空间,并且正在使用以下代码:
var httpRequest = new XMLHttpRequest();
var url_goto = "https://www.googleapis.com/upload/drive/v2/files?uploadType=media";
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState==4 && httpRequest.status==200) {
Firebug.Console.log(httpRequest.responseText);
} else if (httpRequest.readyState==4 && httpRequest.status==401) {
Firebug.Console.log(httpRequest.responseText);
} else {
Firebug.Console.log('Other status: ' + httpRequest.readyState + ', ' + httpRequest.status);
}
};
var s = 'Test string';
httpRequest.open('POST', url_goto, true);
httpRequest.setRequestHeader('Content-Type', 'text/plain');
httpRequest.setRequestHeader('Content-Length', s.length);
httpRequest.setRequestHeader('Authorization', 'MY_AUTH_KEY');
httpRequest.send(s);
问题是我有以下输出:
"Other status: 1, 0"
"Other status: 1, 0"
"Other status: 2, 401"
"Other status: 3, 401"
…并引发异常:
"{
"error": {
"errors": [
{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Invalid Credentials"
}
}"
有人可以帮助我了解为什么身份验证不起作用吗?
我正在使用从Google API控制台的“简单API访问”部分检索到的API密钥.
谢谢!
解决方法:
如果您使用的是oauth2令牌,则必须在Authorization标头中指定Bear和auth令牌,例如
httpRequest.setRequestHeader(“ Authorization”,“ Bearer MY_AUTH_KEY”);
内容总结
以上是互联网集市为您收集整理的javascript-在Google Drive API上简单上传的身份验证问题全部内容,希望文章能够帮你解决javascript-在Google Drive API上简单上传的身份验证问题所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。