gpt4 book ai didi

java - 使用 JSON API 写入存储桶时出现错误 403 "Access not configured"

转载 作者:行者123 更新时间:2023-12-01 14:52:07 24 4
gpt4 key购买 nike

我正在尝试使用以下代码通过 REST API 写入 Cloud Storage:

  public static void insertData() {
try {
StorageObject st = new StorageObject();
//create the media object
Media m = new Media();
String content = "hi! this is a test";
m.setData(Base64.encodeBase64String(content.getBytes()));
m.setContentType("text/html");
st.setMedia(m);
//this gets me the credential, works for other APIs but not cloud storage
Storage storage = RequestBuilder.buildStorage();
//Create the insert and execute
Insert insert = storage.objects().insert("mybucket", st);
insert.execute();
} catch (IOException e) {
log.severe(e.getMessage());
}
}

这是我根据 REST API 的 ACL 条目:

 "kind": "storage#bucketAccessControls",
"items": [
{
"kind": "storage#bucketAccessControl",
"id": "gammeprediction/allUsers",
"selfLink": "https://www.googleapis.com/storage/v1beta1/b/gammeprediction/acl/allUsers",
"bucket": "mybucket",
"entity": "allUsers",
"role": "OWNER"
}]

这就是我获取凭证的方式:

private static Credential authorize() {
GoogleCredential credential = null;
//load properties
Properties appProperties = new Properties();
appProperties.load(RequestBuilder.class
.getResourceAsStream("/app.properties"));

// creates an authorization with the key and service account given
InputStream is = RequestBuilder.class.getResourceAsStream("/"
+ appProperties.getProperty("app.keyFileName"));
PrivateKey pk;
try {
pk = PrivateKeys.loadFromKeyStore(KeyStore.getInstance("PKCS12"),
is, "notasecret", "privatekey", "notasecret");
credential = new GoogleCredential.Builder()
.setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(
appProperties
.getProperty("app.serviceAccount"))
.setServiceAccountPrivateKey(pk)
.setServiceAccountScopes(PredictionScopes.PREDICTION,
DriveScopes.DRIVE, StorageScopes.DEVSTORAGE_FULL_CONTROL).build();


return credential;
}

存储桶的权限是 allUsers 的所有者,但我仍然收到 403 Forbidden“访问未配置”错误。可能出了什么问题?

最佳答案

一旦 JSON API 普遍可用,这个逻辑就会起作用。

但是,目前 JSON API 处于有限预览状态。由于未知用户不被视为有限预览的成员,因此目前无法通过 REST API 进行完全匿名查询。相反,您在连接时必须至少提供一个列入白名单的 API key 。如果您不提供进一步的身份信息,您将被视为匿名用户。或者,更进一步,您可以使用 OAuth2 凭据来将其视为注册用户。欲了解更多信息,请参阅:https://developers.google.com/storage/docs/json_api/

这是一个 GWT RequestBuilder 吗?不幸的是,我并不完全熟悉它的用途。如果有帮助,这里是使用 Google API Java 客户端设置与 API key 的连接的示例:https://code.google.com/p/google-api-java-client/wiki/OAuth2#Unauthenticated_access

此外,您对 setData() 的调用似乎传递了一个非 Base64 字符串,这将会失败。

关于java - 使用 JSON API 写入存储桶时出现错误 403 "Access not configured",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14738831/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com