繁体   English   中英

如果我已经具有访问令牌,如何访问Cloud Storage for Firebase?

[英]How to access Cloud Storage for Firebase if I already have access token?

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

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库相同的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存储库可用,但如果可能的话,我希望使用Google库,因为我已经在使用他们的Firestore库(即使它们不正式支持UWP)。

任何想法如何使它工作?

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

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

如果您想了解有关Cloud Storage中角色的更多信息,可以阅读此链接的信息。Cloud Storage中的角色

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM