【发布时间】:2018-05-11 01:06:11
【问题描述】:
我正在尝试通过在网络浏览器中运行的 Javascript 访问谷歌云存储桶中的内容。到目前为止,我已经使用服务帐户凭据在服务器上创建了签名的 url,并在 REST 调用期间将它们传递给客户端。我这样做的唯一原因是因为我之前试图解决这个问题,然后放弃并选择了签名的 url。现在我需要让它工作。
到目前为止,我已经尝试使用服务帐户凭据在服务器上创建访问令牌,如下所示:
credential = GoogleCredential.fromStream(this.getClass().getResourceAsStream("/serviceaccount.json"));
LinkedList<String> list = new LinkedList<String>();
list.add("https://www.googleapis.com/auth/devstorage.read_only");
credential = credential.createScoped(list);
credential.refreshToken();
然后我将“access_token”返回表单 credential.getAccessToken() 传递给客户端,并在 XmlHttpRequest 中使用它,如下所示:
var xhr = new XMLHttpRequest();
xhr.open('GET', "https://storage.googleapis.com/....." true);
xhr.responseType = 'arraybuffer';
xhr.setRequestHeader('Authorization', 'Bearer ' + access_token);
这会导致 chrome 产生以下错误。
“对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。”
桶上的Cors是
[{"maxAgeSeconds": 3600, "method": ["GET", "HEAD", "DELETE"], "origin": ["http://www.voxxlr.com"], "responseHeader": ["O rigin", "Content-Type", "Content-Length"]},{"maxAgeSeconds": 3600, "method": ["GET", "HEAD", "DELETE"], "origin": ["http ://voxxlr.com"], "responseHeader": ["Origin", "Content-Type", "Content-Length"]}]
接下来我尝试使用如下 API 密钥:
var xhr = new XMLHttpRequest();
xhr.open('GET', "https://storage.googleapis.com/....?KEY=...." true);
xhr.responseType = 'arraybuffer';
这产生了以下错误:
拒绝访问 匿名用户没有 storage.objects.get 对 voxxlr/1511465797269/n.bin 的访问权限。
API 密钥不应该像那样提供访问权限吗?我并不是真的在寻找包含 google/javascript 客户端的解决方案,因为唯一需要的操作就是读取存储桶内容。不需要管理或删除功能。我基本上只是在寻找一个解决方案,让我域中的所有 html/javascript 都可以读取存储桶。
任何帮助将不胜感激...这已经占用了很多时间,但似乎应该有一个简单的解决方案。
【问题讨论】: