【发布时间】:2017-03-03 23:00:43
【问题描述】:
我已将这 2 个包添加到我的项目中: https://github.com/step-up-labs/firebase-database-dotnet 和 https://github.com/step-up-labs/firebase-authentication-dotnet
我正在尝试从 VB.NET 访问 Firebase 上的 JSON 数据,使用 API 密钥和电子邮件/密码进行身份验证。我的代码:
Async Sub AccessTheWebAsync()
Dim authProvider = New FirebaseAuthProvider(New FirebaseConfig("myAPIkey"))
Dim auth = authProvider.SignInWithEmailAndPasswordAsync("myemail", "myPW").Result
Dim firebase = New FirebaseClient("myFireBaseURL")
Dim venues = Await firebase.Child("venues").OrderByKey().WithAuth(auth.FirebaseToken).OnceAsync(Of Venue)()
'more code
end sub
在“auth.FirebaseToken”上收到此错误: "'String' 类型的值不能转换为 'System.Func(Of String)'"
也试过这个方法(谷歌认证):
Async Sub AccessTheWebAsync()
Dim result = Await GoogleWebAuthorizationBroker.AuthorizeAsync(New ClientSecrets() With {.ClientId = myclientID, .ClientSecret = myclientsecret}, {"https://www.googleapis.com/auth/firebase"}, "user", CancellationToken.None)
FetchFirebaseData(result.Token.AccessToken, FirebaseAuthType.Google)
End Sub
Private Async Sub FetchFirebaseData(accessToken As String, authType As FirebaseAuthType)
Dim auth = New FirebaseAuthProvider(New FirebaseConfig(myFirebaseAppKey))
'error on this line:
Dim data = Await auth.SignInWithOAuthAsync(authType, accessToken)
'more code to do something with the data
End Sub
错误消息是:“消息”:“无法将 Google 用户配置文件响应解析为 JSON:{\“错误\”:{\“代码\”:403,\“消息\”:\“权限不足\” ,\"errors\":[{\"reason\":\"insufficientPermissions\",\"domain\":\"global\",\"message\":\"Insufficient Permission\"}]}}"
最终,我希望能够访问我的 Firebase 项目的“数据”(JSON) 以及“规则”(JSON)。这样我就可以创建一个可以在本地机器上运行的非常基本的前端。
【问题讨论】:
-
它需要一个
Func(Of String),即一个指向返回字符串的函数的委托。我不知道 Firebase 是如何工作的,但您可以尝试以下方法:.WithAuth(DirectCast(Function() auth.FirebaseToken, Func(Of String))).OnceAsync(Of Venue)()。 -
好吧,这消除了错误,但它仍然不能 100% 工作。不过,我已经转向了不同的方向。我没有以编程方式登录 API,而是使用 Firebase 上“数据”的“导入 JSON”功能,并在上传之前通过其他方式构建我的 JSON。
-
这并不能告诉我太多,因为我对 Firebase 一无所知 :),所以我恐怕只能这样做了。我什至不知道这是否正确,我的意思是,
Func(Of String)的使用是正确的,但我不知道该函数是否还需要做一些其他的事情,而不仅仅是返回一个字符串。我能说的最好的就是:祝你好运! -
干杯伙伴,感谢您的意见。
-
好的,我可以确认这是正确的做法,所以我将把它作为答案发布。我在 Firebase 的源代码中发现一个名为
WithAuth()的内部(在 VB 中也称为Friend)扩展(它与原始函数具有相同的名称)使用与我相同的技术:github.com/step-up-labs/firebase-database-dotnet/blob/master/…
标签: json vb.net authentication firebase