Firebase authentication with Yahoo on android - android

Using firebase authentication with Yahoo and followed the instructions mentioned here. So I created an App in Yahoo Developer with persmissions to read Contacts and Profile (Not sure which api to get the email and name so I requested for both) and copied the Client Id and Secret in Firebase Authentication Sign In Method for Yahoo. Copied the Callback back to Yahoo Developer.
I followed the codes from the Firebase Authentication for Yahoo but when I try to run it, I get the following error:
Here's my code:
public void onClick(View view) {
signInWithOtherProvider(
OAuthProvider.newBuilder("yahoo.com")
.addCustomParameter("prompt", "login")
.setScopes(new ArrayList<String>() {
{
// Request access to Yahoo Mail API.
add("mail-r");
// This must be preconfigured in the app's API permissions.
add("sdct-w");
// Profile
add("sdps-r");
}
})
.build()
);
}
private void signInWithOtherProvider(OAuthProvider provider) {
Task<AuthResult> pendingTaskResult = auth.getPendingAuthResult();
if (pendingTaskResult != null) {
pendingTaskResult
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
#Override
public void onSuccess(AuthResult authResult) {
signInSuccess(authResult);
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
signInFailed(e);
}
});
} else {
auth
.startActivityForSignInWithProvider(getActivity(), provider)
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
#Override
public void onSuccess(AuthResult authResult) {
signInSuccess(authResult);
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
signInFailed(e);
}
});
}
}
Has anyone successfully use Yahoo with Firebase on Android?
Appreaciate any help.
UPDATE 2019/09/30
I was able to replicate the error using Postman.
It seems that when I entered the incorrect callback URL in Yahoo Developer page, I got the same error. But when I entered it correctly (https://www.getpostman.com/oauth2/callback), it went through. So my best guess is that it must be an issue with the callback url in Firebase.
Now looking at the Firebase console specifically in the Sign In Method for Yahoo, the callback url is (project-id.firebaseapp.com). Yahoo does not accept this as it seems to need a valid url so I added https://. So the redirect url I used is https://project-id.firebasapp.com. However, this still does not work. Then I tried the format similar to the other providers callback which is https://project-id.firebaseapp.com/__/auth/handler but still does not work.
Is there anything I can do in the Firebase Console to validate the callback url?

Related

Firebase Github Authentication for Jetpack Compose App

Firstly, I read the documentation. I enabled GitHub auth from the console and typed OAuth app info, such as client_id, etc. But I got confused about how should I do it. In the documentation, there are some Java codes like that. But I want to do that in Kotlin and also not just "copy-pasting" it:
Task<AuthResult> pendingResultTask = firebaseAuth.getPendingAuthResult();
if (pendingResultTask != null) {
// There's something already here! Finish the sign-in for your user.
pendingResultTask
.addOnSuccessListener(
new OnSuccessListener<AuthResult>() {
#Override
public void onSuccess(AuthResult authResult) {
// User is signed in.
// IdP data available in
// authResult.getAdditionalUserInfo().getProfile().
// The OAuth access token can also be retrieved:
// ((OAuthCredential)authResult.getCredential()).getAccessToken().
}
})
.addOnFailureListener(
new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
// Handle failure.
}
});
} else {
// There's no pending result so you need to start the sign-in flow.
// See below.
}
// The user is already signed-in.
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
firebaseUser
.startActivityForLinkWithProvider(/* activity= */ this, provider.build())
.addOnSuccessListener(
new OnSuccessListener<AuthResult>() {
#Override
public void onSuccess(AuthResult authResult) {
// GitHub credential is linked to the current user.
// IdP data available in
// authResult.getAdditionalUserInfo().getProfile().
// The OAuth access token can also be retrieved:
// authResult.getCredential().getAccessToken().
}
})
.addOnFailureListener(
new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
// Handle failure.
}
});
Simply what I want to do is get this:
After the user clicked GitHub icon and then redirects it to the GitHub page. What should I do?

Unable to find FCM token

I am trying to implement notifications and can send a notification using the firebase console to all devices of the app, however I am having problem trying to retrieve the token of a device so i can send the notification to one particular device. I have a service that extends FirebaseMessagingService and includes the method onNewtoken seen below. I have added this service to my manifest and tried running the app but still unable to find the token. Is there something im doing wrong?
#Override
public void onNewToken(#NonNull String s) {
super.onNewToken(s);
Log.d("NEW_TOKEN",s);
}
OnNewtoken will trigger only by following below scenarios
The app deletes Instance ID
The app is restored on a new device
The user uninstalls/reinstalls the app The user clears app data.
you can get user token by below code as well
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {
#Override
public void onSuccess(InstanceIdResult instanceIdResult) {
String token = instanceIdResult.getToken();
// send it to server
}
});
You can get token forcefully like this u can call this code from onCreate() as sometimes when app has already generated to token it does not call onNewToken
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
#Override
public void onComplete(#NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
KLog.w("getInstanceId failed", task.getException());
return;
}
// Get new Instance ID token
if (task.getResult() != null) {
String token = task.getResult().getToken();
}
}
});

firebase signInWithCustomToken not working

I'm trying to use firebase authenticate my users with a custom token i generate
FirebaseAuth.getInstance().signInWithCustomToken(token).addOnCanceledListener(new OnCanceledListener() {
#Override
public void onCanceled() {
System.out.println("canceled");
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
System.out.println("failure");
}
}).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
System.out.println("complete");
}});
token is a valid authentication token i get from my server
This method is called on a WorkerThread
but no message is printed on console
what might be happening?
===========UPDATE==========
I tested and it work fine on API lvl 27
but it doens't work on API 16
according to firebase page, fibase requires minimum api 9
A couple of ideas:
Make sure you have a valid token. You can paste it into https://jwt.io/ and compare it to a valid token or check the elements that are required as described here. The token has to be signed with a private key of the same Firebase project that you are trying to connect to. You can get the private key from the Firebase Console / Project Settings / Service Accounts
In the OnFailureListener, print out the exception message, e.g. e.getMessage().
addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.d(TAG, e.getMessage());
}

Android Firebase's createUser failing: handler always calling onError

I'm trying to create a new user in Firebase but onError is constantly being called by my ValueResultHandler:
public void onSignUpComplete(String email, String password) {
Firebase ref = new Firebase(Constants.FIREBASE_URL); // this is "https://subdomain.firebaseio.com/"
ref.createUser(email, password, new SignUpResultHandler());
}
class SignUpResultHandler implements Firebase.ValueResultHandler<Map<String, Object>> {
#Override
public void onSuccess(Map<String, Object> StringObjectMap) {
Toast.makeText(getApplicationContext(), "Created User!", Toast.LENGTH_SHORT().show();
}
#Override
public void onError(FirebaseError firebaseError) {
Toast.makeText(getApplicationContext(), "Failed to create user! " +
firebaseError.getDetails(), Toast.LENGTH_SHORT).show();
}
}
I always get the toast "Failed to create user!" and an empty return from getDetails().
Some extra information: Email and password authentication is enabled in Firebase and my manifest has a uses-permission for "android.permission.INTERNET". I have firebase-auth:9.4.0, firebase-core:9.4.0, firebase-database:9.4.0 and firebase-client-android:2.5.2 as dependencies (and play services). I have my google-services.json file in the app folder and the plugin applied in gradle. I'm running the app in a connected device but this also fails on an emulator.
Looks like you're following old Firebase documentation.
new Firebase() method is deprecated as of May 18, 2016 and the url will be handled by the json file, so we do not need to define it. Here is the link for steps with new documentation: Create a password-based account.

Firebase authWithOauthToken is not working

I am developing an app in Android using firebase.I have created the login activity where i have a method that logs user in when they pass the credentials(user creation is already done).Then i will save the token recieved in onAuthenticated callback so that i can log user in automatically next time when he/she opens the app without asking to enter the credentials.
Here is the code
private void loginWithPassword(final String email, String password) {
progressDialog.show();
FirebaseConnections.getConnection().authWithPassword(email, password,
new Firebase.AuthResultHandler() {
#Override
public void onAuthenticated(AuthData authData) {
// Authentication just completed successfully :)
IGStorePreference.getInstance().saveString(Constants.TOKEN, authData.getToken());
IGStorePreference.getInstance().saveString(Constants.UID, authData.getUid());
IGStorePreference.getInstance().saveString(Constants.PROVIDER, authData.getProvider());
dismissProgressDialog();
}
#Override
public void onAuthenticationError(FirebaseError error) {
// Something went wrong :(
dismissProgressDialog();
Snackbar.make(parentView, error.getMessage(), Snackbar.LENGTH_LONG).show();
}
});
}
And then i check onCreate whether we have token token to log user in
private void checkIfTokenExistAndLogin() {
if (IGStorePreference.getInstance().isPrefExists(Constants.TOKEN)) {
progressDialog.show();
String provider = IGStorePreference.getInstance().getString(Constants.PROVIDER);
String token = IGStorePreference.getInstance().getString(Constants.TOKEN);
FirebaseConnections.getConnection().authWithOAuthToken(provider, token, new Firebase.AuthResultHandler() {
#Override
public void onAuthenticated(AuthData authData) {
IGStorePreference.getInstance().saveString(Constants.TOKEN, authData.getToken());
IGStorePreference.getInstance().saveString(Constants.UID, authData.getUid());
IGStorePreference.getInstance().saveString(Constants.PROVIDER, authData.getProvider());
dismissProgressDialog();
}
#Override
public void onAuthenticationError(FirebaseError firebaseError) {
dismissProgressDialog();
Snackbar.make(parentView, firebaseError.getMessage(), Snackbar.LENGTH_LONG).show();
}
});
}
}
But the problem is that i am getting an error while login user with authWithOAuthToken.Please help what i am missing.
This is the error i recieve everytime.
FirebaseError: Invalid authentication credentials provided.
authWithOAuthToken is used to login with a social provider. For example, user signs in with Google and gets an OAuth token returned from Google. Then app sends this OAuth token to Firebase auth server via authWithOAuthToken. User can log in after server verifies the OAuth token.
In your case, user logged in with email/password. The token you received was a Firebase auth token issued by Firebase auth server not an OAuth token issued by social provider.
Please refer to the doc for details: https://firebase.google.com/support/guides/firebase-android#sign_a_user_in_with_a_social_provider_numbered

Categories

Resources