I am trying to get a verification SMS from Firebase Authentication
When I click on (Verify my phone number) it crashes and returns null reference on the line of verifyPhoneNumber
please help me
I tried to change the phone number
and I searched very much and cannot solve it
private void sendVerificationCode(String phone) {
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phone, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
this, // Activity (for callback binding)
mCallbacks); // OnVerificationStateChangedCallbacks
}
Caused by: java.lang.NullPointerException: null reference
at com.google.android.gms.common.internal.Preconditions.checkNotNull(Unknown
Source:2)
at com.google.firebase.auth.PhoneAuthProvider.verifyPhoneNumber(com.google.firebase:firebase-auth##19.1.0:9)
at com.ullb.help.VerifyPhoneActivity.sendVerificationCode(VerifyPhoneActivity.java:67)
at com.ullb.help.VerifyPhoneActivity.onCreate(VerifyPhoneActivity.java:39)
at android.app.Activity.performCreate(Activity.java:7383)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3256)
If everything with firestore is okay and still not working than the problem may you put the callbacks in wrong position . You should implement callback in on create cycle after the send button is clicked. And this doesn't work if you are using emulator so use real android device one and it will work.
Callback
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
#Override
public void onVerificationCompleted(#NonNull PhoneAuthCredential phoneAuthCredential) {
String code = phoneAuthCredential.getSmsCode();
if (code != null) {
verifyCode(code);
phoneOtpEdt.setText(code);
}
}
#Override
public void onVerificationFailed(#NonNull FirebaseException e) {
Toast.makeText(getApplicationContext(), e.getMessage() + "Verification Failed", Toast.LENGTH_SHORT).show();
}
#Override
public void onCodeSent(#NonNull String s, #NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
verificationID = s; //Create your own instance variable
}
};
Sending OTP to the inputed number (Country code is a must)
private void sendOtpToUser(String number) {
PhoneAuthProvider.getInstance().verifyPhoneNumber(
"+91" + number,
60,
TimeUnit.SECONDS,
TaskExecutors.MAIN_THREAD,
mCallbacks);
}
Verifying the received OTP
private void verifyCode(String code) {
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationID, code);
FirebaseAuth auth = FirebaseAuth.getInstance();
auth.signInWithCredential(credential).addOnCompleteListener(LogInActivity.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// do something
} else
// do something
}
});
Don't forget to enable the Phone Sign-in method in Firebase Auth.
I hope it help :)
Related
There's a mistake in this code:
String download_url=task.getResult.getStorage.getDownloadUrl.toString);
When I run the program, I choose a picture from the gallery and I post it, and I get a message:
User is not authenticated, please authenticate using Firebase Authentication and try again
final StorageReference newPhoto=mPhotosStrorage.child(imageUri.getLastPathSegment());
newPhoto.putFile(imageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
#Override
public void onComplete(#NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful())
{
final String myKey=mPhotosDatabase.push().getKey();
//this error String download_url=task.getResult().getDownloadUrl().toString();
String datem=getDateTime();
DatabaseReference newDatabase=mPhotosDatabase.child(myKey);
newDatabase.child("postid").setValue(myKey);
newDatabase.child("postedby").setValue(userId);
newDatabase.child("postedon").setValue(datem);
newDatabase.child("postdetails").setValue(post);
newDatabase.child("postlikes");
newDatabase.child("postviews");
newDatabase.child("postcomments");
newDatabase.child("postimage").setValue(download_url).addOnCompleteListener(new OnCompleteListener<Void>() {
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful())
{
pb.setVisibility(View.GONE);
Pair[] pairs=new Pair[1];
pairs[0]=new Pair<View,String>(homeLayout,"etTransition");
ActivityOptions options=ActivityOptions.makeSceneTransitionAnimation(PostActivity.this,pairs);
startActivity(new Intent(PostActivity.this,HomeActivity.class),options.toBundle());
}
}
});
}else {
Toast.makeText(PostActivity.this, "Error:"+task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
Please help solve it and rewrite the code for me.
According to your last comment, the problem is at the following line of code:
String download_url=task.getResult().getDownloadUrl().toString();
Please note that this is not how you get the download URL nowadays. Converting that Task object to String isn't helpful at all. To solve this, please use the following lines of code:
storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
String url = uri.toString();
//Do what you need to do with the URL
}
});
Besides that, always make sure to have the user authenticated, as that error occurs only when the user is not authenticated and the rules are set to reject the operations.
I'm trying to build an app with biometric authentication (Fingerprint) and I'm having some troubles with the negative button.
The button works, but for some reason is completely invisible.
This is how the app shows
And this is how it sees when you prees the button. As you can see, it exist, but i don't know how to make it visible
I'm using BiometricPrompt and BiometricManager in Java.
Edit: It seems that the button shows normally in any other phone that isn't mine
I'm using a Xiaomi Redmi Note 8.
However this is the code that I'm using:
private void initViews()
{
biometricManager = BiometricManager.from(this);
passwordEditText=findViewById(R.id.passwordText);
loginButton=findViewById(R.id.loginButton);
switch (biometricManager.canAuthenticate()) {
case BiometricManager.BIOMETRIC_SUCCESS:
Log.d("MY_APP_TAG", "App can authenticate using biometrics.");
break;
case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
Log.e("MY_APP_TAG", "No biometric features available on this device.");
break;
case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
Log.e("MY_APP_TAG", "Biometric features are currently unavailable.");
break;
case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
Log.e("MY_APP_TAG", "The user hasn't associated " +
"any biometric credentials with their account.");
break;
}
executor = ContextCompat.getMainExecutor(this);
biometricPrompt = new BiometricPrompt(EnterYourPassActivity.this,
executor, new BiometricPrompt.AuthenticationCallback() {
#Override
public void onAuthenticationError(int errorCode,
#NonNull CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
if(errString.equals("Use account password"))
{
passwordEditText.setVisibility(View.VISIBLE);
}
else
{
Log.d("MY_APP_TAG",""+errString);
Toast.makeText(getApplicationContext(),
"Authentication error: " + errString, Toast.LENGTH_SHORT)
.show();
}
}
#Override
public void onAuthenticationSucceeded(
#NonNull BiometricPrompt.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
Toast.makeText(getApplicationContext(),
"Authentication succeeded!", Toast.LENGTH_SHORT).show();
Intent seeingFiles = new Intent(EnterYourPassActivity.this, SeeingFilesActivity.class);
startActivity(seeingFiles);
}
#Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
Toast.makeText(getApplicationContext(), "Authentication failed",
Toast.LENGTH_SHORT)
.show();
}
});
promptInfo = new BiometricPrompt.PromptInfo.Builder()
.setTitle("Biometric login for my app")
.setSubtitle("Log in using your biometric credential")
.setNegativeButtonText("Use account password")
.build();
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
biometricPrompt.authenticate(promptInfo);
}
});
}
Try to change negativeButton text in this way:
Important - put string into resources:
<string name="negative_button_text"><![CDATA[<font color=\'#48a134\'>Your text at given color</font>]]></string>
As you can see, you can set text color in hex.
Now put negativeText into BiometricPrompt as follows:
val negativeButtonText = getString(R.string.negative_button_text)
val promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle("title")
.setDescription("description")
.setNegativeButtonText(fromHtml(negativeButtonText))
.build()
fun fromHtml(html: String): Spanned {
return HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_LEGACY)
}
In given example negativeText is green and prompt looks like THIS.
I am currently making an android app where I need to verify if the user is entering correct mobile number using the OTP.
The user is already signed in the application using his email and password.
Now I need to verify the mobile number the user enters without using the signInWithCrendntial() method of firebase phone auth.
How do i go about it ?
My mCallbacks is
#Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
Toast.makeText(getApplicationContext(), "Verification Complete", Toast.LENGTH_SHORT).show();
showMessage("Success!!","OTP verified!" + credential);
cred = credential;
//btn_add_guest.setEnabled(true);
}
#Override
public void onVerificationFailed(FirebaseException e) {
Toast.makeText(getApplicationContext(), "Verification Failed", Toast.LENGTH_SHORT).show();
Log.i(TAG,"Error is "+e.getMessage());
}
#Override
public void onCodeSent(String verificationId,
PhoneAuthProvider.ForceResendingToken token) {
Toast.makeText(getApplicationContext(), "Code Sent", Toast.LENGTH_SHORT).show();
mVerificationId = verificationId;
mResendToken = token;
Log.i(TAG,"VERFICATION ID IS"+mVerificationId);
Log.i(TAG,"RESEND TOKEN"+mResendToken);
btn_add_guest.setEnabled(false);
}
};
I m calling this method on button Pressed where put_otp is textView where user enters the OTP.
verifyPhoneNumberWithCode(mVerificationId,put_otp.getText().toString());
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, put_otp.getText().toString());
Log.i(TAG,credential.getProvider());
private void verifyPhoneNumberWithCode(String verificationId, String code) {
Log.i(TAG,"RESEND TOKEN IN METHOD IS"+mResendToken); if(code.equals(mResendToken)&&verificationId.equals(mVerificationId)){
Toast.makeText(AddGuestActivity.this, "Verification Success", Toast.LENGTH_SHORT).show();
btn_add_guest.setEnabled(true);
}
else
Toast.makeText(this,"Please provide correct OTP",Toast.LENGTH_SHORT).show();
}
You can link an email/pass account with a phone account https://firebase.google.com/docs/auth/android/account-linking?authuser=0
I have successfully added Google authentication in android app. I am able to login properly without any error. But when i try to logout GoogleApiClient is giving me null so that i am failing to logout successfully. I tried so many answers here but nothing worked for me. Below is the code I entered in my MainActivity.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if(ApplicationPreferences.get().isFirstTimeUser()) {
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(new Scope(GmailScopes.GMAIL_READONLY))
.requestServerAuthCode(Constants.SERVER_CLIENT_ID, true)
.requestEmail()
.build();
signInButton.setSize(SignInButton.SIZE_STANDARD);
signInButton.setScopes(gso.getScopeArray());
signInButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.sign_in_button:
signIn();
break;
}
}
});
signInButton.setVisibility(View.VISIBLE);
// Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
.enableAutoManage(MainActivity.this, MainActivity.this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.addConnectionCallbacks(MainActivity.this)
.build();
mGoogleApiClient.connect();
} else {
loadMainActivity();
}
}
}, 2000);
Below is my signOut method as specified by Documentation but i failed to understand their statement You must confirm that GoogleApiClient.onConnected has been called before you call signOut. Need some idea what I am doing wrong here.
if (mGoogleApiClient != null)
{
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
#Override
public void onResult(Status status) {
ApplicationPreferences.get().clearAll();
Intent intent = getIntent();
finish();
startActivity(intent);
}
});
}
My OnStart()
#Override
public void onStart() {
super.onStart();
OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if (opr.isDone()) {
// If the user's cached credentials are valid, the OptionalPendingResult will be "done"
// and the GoogleSignInResult will be available instantly.
Log.d(TAG, "Got cached sign-in");
GoogleSignInResult result = opr.get();
handleSignInResult(result);
} else {
// If the user has not previously signed in on this device or the sign-in has expired,
// this asynchronous branch will attempt to sign in the user silently. Cross-device
// single sign-on will occur in this branch.
showProgressDialog();
opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
#Override
public void onResult(GoogleSignInResult googleSignInResult) {
hideProgressDialog();
handleSignInResult(googleSignInResult);
}
});
}
}
The error, if you say callbacks are not being called, might just be the sign that you may not have connected first. I imagine you have implemented the callbacks and they are not being called?
In any case, to "handle" the error situation, you may want to check if the GoogleApiClient is connected.. like this:
if (mGoogleApiClient != null && mGoogleApiClient.isConnected())
{
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
#Override
public void onResult(Status status) {
ApplicationPreferences.get().clearAll();
Intent intent = getIntent();
finish();
startActivity(intent);
}
});
}
else{
Log.w("SomeTag", "It looks like GoogleApiClient is not connected");
}
But I do think you need to check if there are any errors (for instance does onConnectionFailed(ConnectionResult result) get called instead? What error do you see?
Hope this helps.
I am writing an android application to get the Facebook user albums and photos and display in my Android application.
I have created a Facebook App with APP_ID 281846961912565.
While creating the Facebook instance, I am passing this id as follows
facebook = new Facebook(APP_ID);
Using this instance, I am able to login to my FB account post on messages on facebook wall programatically.
After logging in, I get an access_token.
I'm using the access token to get the album ids using facebook.request("https://graph.facebook.com/me/albums?access_token="+facebook.getAccessToken());
Now I get {"error":{"message":"Malformed access token ACCESSTOKENACCESSTOKEN?access_token=ACCESSTOKENACCESSTOKEN","type":"OAuthException","code":190}}
Can any of you please help me resolve this issue and point out what i am doing wrong.
My code is as follows:
private static final String[] PERMISSIONS = new String[] { "publish_stream","user_photos" };
public boolean saveCredentials(Facebook facebook) {
Editor editor = getApplicationContext().getSharedPreferences(KEY,
Context.MODE_PRIVATE).edit();
editor.putString(TOKEN, facebook.getAccessToken());
editor.putLong(EXPIRES, facebook.getAccessExpires());
return editor.commit();
}
public boolean restoreCredentials(Facebook facebook) {
SharedPreferences sharedPreferences = getApplicationContext()
.getSharedPreferences(KEY, Context.MODE_PRIVATE);
facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
return facebook.isSessionValid();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
facebook = new Facebook(APP_ID);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.facebook_dialog);
String facebookMessage = getIntent().getStringExtra("facebookMessage");
if (facebookMessage == null) {
facebookMessage = "Test wall post";
}
messageToPost = facebookMessage;
}
R.layout.facebook_dialog is the dialog which pops up asking if a message should be shared on facebook or not. If yes the following method is called.
public void share(View button) {
if (!facebook.isSessionValid()) {
loginAndPostToWall();
} else {
postToWall(messageToPost);
}
}
public void loginAndPostToWall() {
facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH,
new LoginDialogListener());
}
class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
saveCredentials(facebook);
if (messageToPost != null) {
postToWall(messageToPost);
}
}
public void onFacebookError(FacebookError error) {
showToast("Authentication with Facebook failed!");
finish();
}
public void onError(DialogError error) {
showToast("Authentication with Facebook failed!");
finish();
}
public void onCancel() {
showToast("Authentication with Facebook cancelled!");
finish();
}
}
public void postToWall(String message) {
Bundle parameters = new Bundle();
parameters.putString("message", message);
parameters.putString("description", "topic share");
try {
facebook.request("me");
String response = facebook.request("me/feed", parameters, "POST");
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("")
|| response.equals("false")) {
showToast("Blank response.");
} else {
showToast("Message posted to your facebook wall!");
}
getImagesFromUserAlbum();
finish();
} catch (Exception e) {
showToast("Failed to post to wall!");
e.printStackTrace();
finish();
}
}
Later when I do a `private void getImagesFromUserAlbum() {
facebook.getAccessToken();
JSONArray albumss = null;
String response = null;
try {
response = facebook.request("me/albums");
// `
I get the error
{"error":{"message":"Malformed access token ACCESSTOKEN?access_token=ACCESSTOKEN","type":"OAuthException","code":190}}
Thanks for your help.
The code above is now the working copy. Thanks to Bartek.
If you look at the Errors page in the documentation you will see that when you get error 190 you should authorise/reauthorise the user.
I suspect that this happened to you because you first logged in, then added the permissions to access the albums to your application BUT did not log out and log back in. Hence, you need to obtain a new access token which will grant the new permissions to your application.
Please check is there &expires in your access token if yes then remove it because it is not part of access_token and try after that.