【问题标题】:How to access Cloud Storage for Firebase if I already have access token?如果我已经拥有访问令牌,如何访问 Cloud Storage for Firebase?
【发布时间】:2019-07-23 09:43:15
【问题描述】:

在我的桌面应用程序 (UWP) 中,我正在尝试创建 StorageClient 以使用以下代码访问 Cloud Storage for Firebase:

public StorageClient CreateStorageClient(string accessToken)
{
    var service = new StorageService(new BaseClientService.Initializer
    {
        HttpClientInitializer = new HttpClientInitializer(() => accessToken),
        ApplicationName = StorageClientImpl.ApplicationName,
    });

    StorageClient client = new StorageClientImpl(service);
    return client;
}

public class HttpClientInitializer : IConfigurableHttpClientInitializer
{

    public HttpClientInitializer(Func<string> getFreshTokenMethod)
    {
        _getFreshTokenMethod = getFreshTokenMethod;
    }

    private readonly Func<string> _getFreshTokenMethod;

    public void Initialize(ConfigurableHttpClient httpClient)
    {
        httpClient.DefaultRequestHeaders.Authorization =
            new AuthenticationHeaderValue("Bearer", _getFreshTokenMethod());
    }
}

对于身份验证,我使用 FirebaseAuthentication.net,因此我有可用的 Firebase 身份验证令牌(除了 Google access_token)。

如果我使用 Firebase 身份验证令牌使用上述代码创建 StorageClient,则任何上传操作都会出错:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "authError",
    "message": "Invalid Credentials",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Invalid Credentials"
 }
}

为了进行比较,我成功地使用了与 Firestore library 相同的 Firebase 身份验证令牌(在同一个应用中)。

我也尝试过使用 Google access_token,但它失败了:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "forbidden",
    "message": "my.email@gmail.com does not have storage.objects.create access to my-app.appspot.com/users/[uid]/image.jpg."
   }
  ],
  "code": 403,
  "message": "my.email@gmail.com does not have storage.objects.create access to my-app.appspot.com/users/[uid]/image.jpg."
 }
}

我知道还有其他 Firebase storage 库可用,但如果可能的话,我更喜欢使用 Google 库,因为我已经在使用他们的 Firestore 库(即使他们不正式支持 UWP)。

任何想法如何让它工作?

【问题讨论】:

    标签: c# .net firebase google-cloud-platform google-cloud-storage


    【解决方案1】:

    您遇到的问题是因为您需要向要使用的成员添加角色(您在 UWP 应用中使用的电子邮件)。

    您需要分配给成员的角色是 storage.objectCreator,您可以点击此链接进行操作Grant roles

    如果您想了解更多关于云存储中角色的信息,您可以阅读此链接的信息Roles in Cloud Storage

    【讨论】:

    • 我可以使用其他库 (Firebasestorage.net) 和我的 Android 应用程序使用相同的登录电子邮件访问同一个文件,因此我认为问题与角色无关。无论如何,谢谢你的想法。
    猜你喜欢
    • 2018-07-25
    • 2021-10-02
    • 2012-10-10
    • 1970-01-01
    • 2017-10-24
    • 2017-08-24
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多