401使用Android App中的Google Cloud API进行响应

时间:2014-02-01 02:29:32

标签: android google-app-engine google-cloud-storage

我正在尝试将Google AppEngine与我的应用程序一起使用(以替换AWS)。我让数据存储区使用以下代码:

themeendpoint.Builder endpointBuilder = new themeendpoint.Builder(
            AndroidHttp.newCompatibleTransport(),
            new JacksonFactory(),
            new HttpRequestInitializer() {
                public void initialize(HttpRequest httpRequest) { }
            });

    themeendpoint endpoint = CloudEndpointUtils.updateBuilder(
            endpointBuilder).build();
    DbAdapter dbAdapter = new DbAdapter(mCtx);
    try {
        CollectionResponseTheme response = endpoint.listTheme().execute();        
        List<Theme> listResponse = response.getItems();
...

但是,当我尝试将以下代码用于Cloud Storage API(使用Eclipse添加 - &gt; Google - &gt;添加Google API)下载我已存储的zip文件时:

File file = new File(Environment.getExternalStorageDirectory() + mLocalPath + mZipName);

    Builder storageBuilder = new Storage.Builder(AndroidHttp.newCompatibleTransport(),
            new JacksonFactory(),
            new HttpRequestInitializer() {
                public void initialize(HttpRequest httpRequest) { }
            });

    Storage storage = storageBuilder.build();


    Storage.Objects.Get get = storage.objects().get(GAE_WEBDATA.PUBLIC_ZIP_BUCKET, mZipName);

    FileOutputStream stream = new FileOutputStream(file);
    try {
        get.executeAndDownloadTo(stream);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        stream.close();
    }
}

我收到以下错误:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
{
  "code" : 401,
"errors" : [ {
"domain" : "global",
"location" : "Authorization",
"locationType" : "header",
"message" : "Login Required",
"reason" : "required"
 } ],
  "message" : "Login Required"
}

在云控制台中,我将Google云端存储设置为“开启”。特定存储桶权限设置为“所有用户”=“读者”。我错过了什么?

1 个答案:

答案 0 :(得分:0)

当您尝试读取对象时,不需要存储桶权限。相反,您需要将OBJECT权限设置为public-read。

您可以通过云控制台或通过gsutil从命令行修复对象权限。命令是:

gsutil acl set public-read gs://bucketName/objectName