【问题标题】:401 Response using Google Cloud API in Android App在 Android 应用程序中使用 Google Cloud API 的 401 响应
【发布时间】:2014-02-01 02:29:32
【问题描述】:

我正在尝试将 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-->Google-->添加 Google APIs 添加)下载我存储的 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 Cloud Storage 设置为“开启”。特定的存储桶权限设置为“所有用户”=“读者”。我错过了什么?

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

    • 如果我不想公开怎么办?我只希望我的应用能够下载它。
    • 这是一个非常深奥的问题。任何匿名用户都可以下载并安装您的应用程序吗?他们是否需要设置某种帐户才能被允许下载对象?如果您有某种方式对受信任的用户进行身份验证,则将签名 URL 交给这些用户是解决问题的规范方法。
    • 理论上没有匿名用户可以下载它,因为它只在 Google play 中,每个人都会有一个 gumshoe 帐户。我不想使用 oauth2,因为这需要用户从弹出窗口中获得许可(据我了解)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-30
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-15
    相关资源
    最近更新 更多