Unity3d中的Firebase身份验证+ Firebase存储(Firebase Authentication + Firebase Storage in Unity3d)
我无法理解如何在存储中传递身份验证的值...以下是尝试编写文件时返回的错误,以及代码本身下面的错误。 有人遇到过这样的问题吗?
权限规则使用该标准
service firebase.storage { match /b/project.appspot.com/o { match /{allPaths=**} { allow read, write: if request.auth != null; } } }
System.AggregateException:抛出了类型'System.AggregateException'的异常。 ----------------- Firebase.Storage.StorageException:用户无权访问此对象。 ---> System.IO.IOException:服务器终止了上传会话---内部异常堆栈跟踪结束---
using System.Collections; using System.Collections.Generic; using UnityEngine; using Firebase; using Firebase.Storage; using Firebase.Auth; public class Storage : MonoBehaviour { public Texture2D texture; public Auth autentification; FirebaseApp app; AppOptions options; FirebaseStorage storage; StorageReference storage_ref, ref_file; // Use this for initialization void Start () { InitFirebase (); } public void InitFirebase(){ print("InitFirebase start"); DependencyStatus dependencyStatus = FirebaseApp.CheckDependencies(); if (dependencyStatus != DependencyStatus.Available) { FirebaseApp.FixDependenciesAsync().ContinueWith(task => { dependencyStatus = FirebaseApp.CheckDependencies(); if (dependencyStatus == DependencyStatus.Available) { Signin(); } else { print("Could not resolve all dependencies: " + dependencyStatus); } }); } else { Signin(); } } public void Signin(){ print("Signin start"); string _testEmail = "test@gmail.com"; string _testPassword = "qwerty"; options = new AppOptions (); options.ApiKey = "API_KEY"; options.StorageBucket = "STORAGE_BUCKET"; options.AppId = "APPID"; app = FirebaseApp.Create (options, "name"); FirebaseAuth.GetAuth(app).SignInWithEmailAndPasswordAsync (_testEmail, _testPassword).ContinueWith ((System.Threading.Tasks.Task<FirebaseUser> authTask) => { if (authTask.IsCanceled) { print("signin canceled"); } else if (authTask.IsFaulted) { print("signin faulted " + authTask.Exception.ToString()); } else if (authTask.IsCompleted) { print("signin complete , user id is " + authTask.Result.UserId); storage = FirebaseStorage.GetInstance(app); storage_ref = storage.GetReferenceFromUrl("gs://testproject-3b976.appspot.com/data/"); Upload ("images/" + texture.name + ".png"); } else { print("signin unknown error"); } }); }
I can't understand how to pass the values of authentication in the storage... Below is the error which is returned when attempting to write the file, and below the code itself. Has anyone faced such a problem?
Permission rules use the standard
service firebase.storage { match /b/project.appspot.com/o { match /{allPaths=**} { allow read, write: if request.auth != null; } } }
System.AggregateException: Exception of type 'System.AggregateException' was thrown. ----------------- Firebase.Storage.StorageException: User does not have permission to access this object. ---> System.IO.IOException: The server has terminated the upload session --- End of inner exception stack trace ---
using System.Collections; using System.Collections.Generic; using UnityEngine; using Firebase; using Firebase.Storage; using Firebase.Auth; public class Storage : MonoBehaviour { public Texture2D texture; public Auth autentification; FirebaseApp app; AppOptions options; FirebaseStorage storage; StorageReference storage_ref, ref_file; // Use this for initialization void Start () { InitFirebase (); } public void InitFirebase(){ print("InitFirebase start"); DependencyStatus dependencyStatus = FirebaseApp.CheckDependencies(); if (dependencyStatus != DependencyStatus.Available) { FirebaseApp.FixDependenciesAsync().ContinueWith(task => { dependencyStatus = FirebaseApp.CheckDependencies(); if (dependencyStatus == DependencyStatus.Available) { Signin(); } else { print("Could not resolve all dependencies: " + dependencyStatus); } }); } else { Signin(); } } public void Signin(){ print("Signin start"); string _testEmail = "test@gmail.com"; string _testPassword = "qwerty"; options = new AppOptions (); options.ApiKey = "API_KEY"; options.StorageBucket = "STORAGE_BUCKET"; options.AppId = "APPID"; app = FirebaseApp.Create (options, "name"); FirebaseAuth.GetAuth(app).SignInWithEmailAndPasswordAsync (_testEmail, _testPassword).ContinueWith ((System.Threading.Tasks.Task<FirebaseUser> authTask) => { if (authTask.IsCanceled) { print("signin canceled"); } else if (authTask.IsFaulted) { print("signin faulted " + authTask.Exception.ToString()); } else if (authTask.IsCompleted) { print("signin complete , user id is " + authTask.Result.UserId); storage = FirebaseStorage.GetInstance(app); storage_ref = storage.GetReferenceFromUrl("gs://testproject-3b976.appspot.com/data/"); Upload ("images/" + texture.name + ".png"); } else { print("signin unknown error"); } }); }
原文:https://stackoverflow.com/questions/41795040
最满意答案
我怀疑,您已将菜单声明为实例变量(位于类的顶部),但您从未将其实例化为局部变量。 添加行
menu = (ImageView)findViewById(R.id.menu)
在onCreate()方法的顶部,在设置OnClickListener之前,它将起作用。
声明实例变量(您已经完成)和实际初始化它之间存在差异。 从这个意义上讲,您可以像常规java类中的构造函数一样查看onCreate()方法。 将变量声明在类的顶部是不够的,您必须将其初始化为实际对象的变量。
这可能听起来微不足道,但相信我并非如此。 当我第一次学习java时,在围绕这个概念开始之前,我花了很多时间在屏幕上尖叫着对你现在遇到的同样的错误。
As I suspected, you have declared menu as an instance variable (at the top of your class), but you never instantiate it as a local variable. Add the line
menu = (ImageView)findViewById(R.id.menu)
at the top of your onCreate() method, BEFORE you set the OnClickListener, and it will work.
There is a difference between declaring the instance variable (which you have done) and actually initializing it. In this sense, you can look at the onCreate() method like a constructor in a regular java class. It's not enough to declare the variable at the top of the class, you have to initialize it as a variable of the actual object.
This might sound trivial, but believe me it's not. When I was first learning java, before I wrapped my head around this concept, I spent many hours screaming at the screen over the same sorts of errors you're having now.
相关问答
更多-
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.yourxml); // you have missed this..... PostQuestion postQuestion = new PostQuestion(); postQues.setText(postQuestion.postQuest ...
-
问题发生在您在sendSms方法开始时创建PendingIntent对象的sendSms 。 你正在实施SendSMS作为一项活动,但我认为它不是必须的,并且它没有被正确初始化为一个活动,所以你看到了这个失败。 从你的sendingms类中取出extends Activity ,并将sendSms方法更改为如下启动: public class SendSms { public void sendSMS(Context context, String phoneNumber, String mess ...
-
final EditText text = (EditText)layout.findViewById(R.id.etGivenTo); ^^^^^^ final EditText text = (EditText)layout.findViewById(R.id.etGivenTo); ^^^^^^
-
我怀疑,您已将菜单声明为实例变量(位于类的顶部),但您从未将其实例化为局部变量。 添加行 menu = (ImageView)findViewById(R.id.menu) 在onCreate()方法的顶部,在设置OnClickListener之前,它将起作用。 声明实例变量(您已经完成)和实际初始化它之间存在差异。 从这个意义上讲,您可以像常规java类中的构造函数一样查看onCreate()方法。 将变量声明在类的顶部是不够的,您必须将其初始化为实际对象的变量。 这可能听起来微不足道,但相信我并非如 ...
-
空指针异常 - Android片段中的按钮[重复](Null Pointer Exception - Button in Android Fragment [duplicate])[2022-05-02]
你在RecyclerView上调用findViewById() 。 您需要在根视图上调用它: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) { View layout = inflater.inflate(R.layout.activity_warrior_self_battle_recyclerview, container, false) ... -
您的txtname和txtnumber为空 即 final EditText txtname=(EditText)findViewById(R.id.editText1); final EditText txtnumber=(EditText)findViewById(R.id.editText2); 当您尝试访问空引用时,您将获得NullPointerException 试着让他们这样 final EditText txtname=(EditText)v.findViewById(R.id.editT ...
-
您尚未定义logindatabaseadapter变量。 当你调用logindatabaseadapter.checkSinlgeEntry(...)你会得到一个NullPointerException , logindatabaseadapter = null 。 您需要将logindatabaseadapter = new LoginDataBaseAdapter(this)到onCreate You haven't defined the logindatabaseadapter variable. ...
-
您的LazyAdapter类代码应如下所示: public class LazyAdapter extends BaseAdapter { private Activity activity; private ArrayList
> data; private static LayoutInflater inflater=null; public ImageLoader imageLoader; String item ... -
这一行在这里 btnSongList.setOnClickListener(new OnClickListener(){@Override public void onClick(View v) {visible();}}); 您从未为此特定视图调用findViewById() ,因此指针仍为null。 This line here btnSongList.setOnClickListener(new OnClickListener(){@Override public void on ...
-
在onActivityCreated()中初始化您的视图。 从文档: 在创建片段的活动并且实例化此片段的视图层次结构时调用。 一旦这些部分到位,它可用于进行最终初始化,例如检索视图或恢复状态。 对于使用setRetainInstance(boolean)保留其实例的片段也很有用,因为此回调告诉片段何时与新活动实例完全关联。 这是在onCreateView(LayoutInflater,ViewGroup,Bundle)之后和onStart()之前调用的。 你可以调用getView().findViewByI ...