【发布时间】:2018-02-06 04:35:04
【问题描述】:
我正在尝试启动并运行 Unity 的电话号码身份验证,但我没有收到任何 SMS 响应。在线文档对如何执行此操作非常不清楚。我更愿意为 Firebase 使用 Google Auth,但显然它尚不受支持。谁能帮我弄清楚如何启动并运行它?
这是我目前所拥有的......
public class SignIn : MonoBehaviour {
private string phoneNumber;
private string secureCode;
private uint phoneAuthTimeoutMs;
private Firebase.Auth.FirebaseAuth firebaseAuth;
private Credential credential;
private string verificationId;
private string verificationCode;
public Text PhoneNumberInputFieldText;
public Text SecureCodeInputFieldText;
public Button SendSecureCodeButton;
public Button SubmitSecureCodeButton;
// Use this for initialization
void Start()
{
SendSecureCodeButton.GetComponent<Button>().onClick.AddListener(() => StartSignIn());
}
// Update is called once per frame
void Update () {
}
void StartSignIn()
{
firebaseAuth = Firebase.Auth.FirebaseAuth.DefaultInstance;
phoneAuthTimeoutMs = 60000;
phoneNumber = PhoneNumberInputFieldText.text;
Debug.Log(phoneNumber);
if (PlayerPrefs.GetString("secureCode") != null)
{
verificationId = PlayerPrefs.GetString("secureCode");
}
PhoneAuthProvider provider = PhoneAuthProvider.GetInstance(firebaseAuth);
provider.VerifyPhoneNumber(phoneNumber, phoneAuthTimeoutMs, null,
verificationCompleted: (credential) =>
{
// Auto-sms-retrieval or instant validation has succeeded (Android only).
// There is no need to input the verification code.
// `credential` can be used instead of calling GetCredential().
},
verificationFailed: (error) =>
{
// The verification code was not sent.
// `error` contains a human readable explanation of the problem.
},
codeSent: (id, token) =>
{
//Prompt here to type in SecureCode
PlayerPrefs.SetString("secureCode", id);
credential = provider.GetCredential(verificationId, verificationCode);
firebaseAuth.SignInWithCredentialAsync(credential).ContinueWith(task => {
if (task.IsFaulted)
{
Debug.LogError("SignInWithCredentialAsync encountered an error: " +
task.Exception);
return;
}
FirebaseUser newUser = task.Result;
Debug.Log("User signed in successfully");
// This should display the phone number.
Debug.Log("Phone number: " + newUser.PhoneNumber);
// The phone number providerID is 'phone'.
Debug.Log("Phone provider ID: " + newUser.ProviderId);
});
// Verification code was successfully sent via SMS.
// `id` contains the verification id that will need to passed in with
// the code from the user when calling GetCredential().
// `token` can be used if the user requests the code be sent again, to
// tie the two requests together.
},
codeAutoRetrievalTimeOut: (id) =>
{
// Called when the auto-sms-retrieval has timed out, based on the given
// timeout parameter.
// `id` contains the verification id of the request that timed out.
});
}
}
提前致谢!
这是我的 Firebase 控制台:
【问题讨论】:
-
你的 logcat 说什么?原因可能是因为电话号码尚未在 Firebase 上注册,所以它不起作用。
-
它输出“电话号码:1-888-XXX-XXXX”和“电话提供商 ID:”不是实际的 X 只是一些其他号码。我想这是一个谷歌号码。我相信它来自task.Result。
标签: c# firebase unity3d firebase-authentication