0

次のコードを使用して、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 の OWNER ですが、403 Forbidden "Access not configured" エラーが引き続き発生します。何が間違っている可能性がありますか?

4

1 に答える 1

1

JSON API が一般に利用可能になると、このロジックが機能します。

ただし、現時点では、JSON API は限定プレビューです。不明なユーザーは限定プレビューのメンバーとはみなされないため、現在、REST API を介した完全に匿名のクエリは実行できません。代わりに、接続時に最低限、ホワイトリストに登録された API キーを提供する必要があります。それ以上の身元情報を提供しない場合は、匿名ユーザーとして扱われます。または、さらに進んで、代わりに OAuth2 資格情報を使用して、登録済みユーザーとして扱うこともできます。詳細については、https ://developers.google.com/storage/docs/json_api/ を参照してください。

それは GWT RequestBuilder ですか? 残念ながら、私はその使用法に完全に精通していません。それが役立つ場合は、Google API Java クライアントを使用して API キーとの接続を設定する例を次に示します: https://code.google.com/p/google-api-java-client/wiki/OAuth2#Unauthenticated_access

また、setData() への呼び出しが base64 ではない文字列を渡しているように見えますが、これは失敗します。

于 2013-02-07T18:56:57.237 に答える