Google cloud storage 谷歌云存储上传未经授权
我想使用javascript将文件上传到google云存储,我使用的是google api javascript 我的bucket不是公共的,我需要配置写入权限,但云存储需要身份验证 我尝试了很多类型的配置,但对GAE身份验证知之甚少 因此,当我尝试发送文件时,会显示以下消息: “消息”:“匿名用户没有storage.objects.create访问bucket boti-lab-dev的权限。” 按照我的代码:Google cloud storage 谷歌云存储上传未经授权,google-cloud-storage,google-cloud-endpoints,Google Cloud Storage,Google Cloud Endpoints,我想使用javascript将文件上传到google云存储,我使用的是google api javascript 我的bucket不是公共的,我需要配置写入权限,但云存储需要身份验证 我尝试了很多类型的配置,但对GAE身份验证知之甚少 因此,当我尝试发送文件时,会显示以下消息: “消息”:“匿名用户没有storage.objects.create访问bucket boti-lab-dev的权限。” 按照我的代码: function start() { // 2. Initialize the
function start() {
// 2. Initialize the JavaScript client library.
gapi.client.init({
'apiKey': 'myApiKey',
// clientId and scope are optional if auth is not required.
'clientId': 'xxxbug.apps.googleusercontent.com',
'scope': 'https://www.googleapis.com/auth/devstorage.read_write',
}).then(function() {
// 3. Initialize and make the API request.
return gapi.client.request({
'method': 'post',
'path': 'https://www.googleapis.com/upload/storage/v1/b/myBucket/o?uploadType=media&name=nameOfFile',
'header': {
'Authorization': 'Bearer ya29.mykey'
}
})
}).then(function(response) {
console.log(response.result);
}, function(reason) {
console.log('Error: ' + reason.result.error.message);
});
};
// 1. Load the JavaScript client library.
gapi.load('client', start);
我需要创建或配置什么?
谢谢以下内容对我来说很有魅力: 1/添加以下javascript:
function init() {
gapi.client.setApiKey(yourApiKey);
window.setTimeout(checkAuth, 1);
}
function checkAuth() {
gapi.auth.authorize({
client_id: yourClientId,
scope: yourApiScopes,
immediate: true
}, handleAuthResult);
}
function handleAuthResult(authResult) {
if (authResult && !authResult.error) {
//do something
} else {
$("#loginButton").click(function () {
handleAuthClick();
});
}
}
function handleAuthClick() {
gapi.auth.authorize({
client_id: yourClientId,
scope: yourApiScopes,
immediate: false
}, handleAuthResult);
return false;
}
当你的示波器等于
"email https://www.googleapis.com/auth/devstorage.read_write"
2/在HTML页面的末尾添加此行
<script src="https://apis.google.com/js/client.js?onload=init"></script>
我明白了。
我使用了下面的代码:
这对我很有效,我知道这不好,因为我派了一个信物持有者。
我测试了你的解决方案,也成功了,谢谢。
您的解决方案更好,因为您使用的是api google
function sentStorage(token, bucket, x-goog-project-id) {
var file = document.getElementById("myFile").files[0];
var url = 'https://www.googleapis.com/upload/storage/v1/b/'
url += bucket + o?uploadType=media&name=' + file;
xhr = new XMLHttpRequest();
xhr.open('POST', url);
xhr.setRequestHeader('Content-Type', file.type);
xhr.setRequestHeader('x-goog-project-id', x-goog-project-id);
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
xhr.send(file);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
var response = JSON.parse(xhr.responseText);
if (xhr.status === 200) {
alert('codigo 200');
} else {
var message = 'Error: ' + response.error.message;
console.log(message);
alert(message);
}
}
};
}
function sentStorage(token, bucket, x-goog-project-id) {
var file = document.getElementById("myFile").files[0];
var url = 'https://www.googleapis.com/upload/storage/v1/b/'
url += bucket + o?uploadType=media&name=' + file;
xhr = new XMLHttpRequest();
xhr.open('POST', url);
xhr.setRequestHeader('Content-Type', file.type);
xhr.setRequestHeader('x-goog-project-id', x-goog-project-id);
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
xhr.send(file);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
var response = JSON.parse(xhr.responseText);
if (xhr.status === 200) {
alert('codigo 200');
} else {
var message = 'Error: ' + response.error.message;
console.log(message);
alert(message);
}
}
};
}