【发布时间】: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