I have an Android and iOS application that uses Firebase cloud functions with Stripe to process payments.
On the client side I handle the token operation and then write to the realtime database. When done writing the addPaymentSource cloud function triggers which stores that payment source for future transactions.
Interesting enough the process of creating a token on iOS and then saving that output to my server works as expected. My problem comes along when trying to duplicate my iOS implementation into my Android application. The Firebase cloud function triggers as expected but it is outputting an error to my server.
Error found in server:
"The source hash must include an 'object' key indicating what type of source to create."
Client code:
public void tokenizePaymentFields(){
Stripe stripe = new Stripe(getApplicationContext(), stripePublishableKey);
final Card stripeCard = new Card(validCard.getCardNumber()
,Integer.valueOf(validCard.getExpiredDate().substring(0,2)),Integer.valueOf(validCard.getExpiredDate().substring(3,5)),validCard.getCvvCode());
if (!stripeCard.validateCard()) {
Toast.makeText(getApplicationContext(),
"There was an error validating your card.",
Toast.LENGTH_LONG
).show();
return;
}
stripe.createToken(
stripeCard,
new TokenCallback() {
public void onSuccess(Token token) {
// Send token to your server
pushToServer(token);
}
public void onError(Exception error) {
// Show localized error message
activitySubmitCreditCardBinding.progressCircle.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(),
error.getLocalizedMessage(),
Toast.LENGTH_LONG
).show();
}
}
);
}
Stripe(Firebase Cloud Functions):
https://github.com/firebase/functions-samples/tree/master/stripe
Rather than sending the entire token object to your server, you should simply send the token's id, like this:
public void onSuccess(Token token) {
// Send token to your server
pushToServer(token.getId());
}
In your server-side (Firebase) code, the charge creation request expects only a token ID in the source parameter, not a full token object.
Related
I have a firebase project which allows users to login only if they are already exists in identifier in authentication .
I already added few users using my web app with mobile numbers.
Now, in android I have used the signInWithPhoneAuthCredential method to get the users login.
But in this method, it allows any users to login even if the user is first time entering the mobile number.
Is there any method to restrict this ?
Sample Code :
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = task.getResult().getUser();
// ...
} else {
// Sign in failed, display a message and update the UI
Log.w(TAG, "signInWithCredential:failure", task.getException());
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
// The verification code entered was invalid
}
}
}
});
}
According to the docs:
signInWithPhoneNumber:
Asynchronously signs in using a phone number. This method sends a code via SMS to the given phone number, and returns a firebase.auth.ConfirmationResult. After the user provides the code sent to their phone, call firebase.auth.ConfirmationResult.confirm with the code to sign the user in.
This is the default behavior in all applications that uses phone number as login. There is no method in the firebase docs that can restrict this.
you have to use firebase database also with this code to save user info, that way you can achieve your use case
I am making an Android App that will utilize the Google AutoML Vision API. I am looking for a way to get a permanent access token or generate them in code so that I do not need to use gcloud everytime I want to use my app. How would I go about doing this?
I have created the AutoML model, set up my service account, and coded my app in Android Studio so that it makes the request to the API using Volley. The problem is, they require you to generate and pass an access token using gcloud. I can generate the token and put it in my code but it only lasts for an hour and then it expires. The REST API requires the access token as shown below.
curl -X POST -H "Content-Type: application/json" \
-H "Authorization: Bearer $(gcloud auth application-default print-access-
token)"
I have looked into different ways around this problem. For example, there are some Google Client Libraries for Java and Google Cloud Applications that show how to add the service account credentials into the code. I am confused how I would add the Json key file into the code when running it from a phone. I have also read that Firebase could be used but I am unfamiliar about what the process for that would be.
Currently, I will open up gcloud on my computer, generate the access token, paste it into my code and run the app as follows with the header and this returns the desired results for up to an hour until the access code expires.
#Override
public Map<String, String> getHeaders() throws AuthFailureError{
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer " + accesstoken);
return headers;
}
I would like this to be a stand alone application that can run on an Android phone. What is the best way to go about doing this?
UPDATE:
I was able to add the file into Android Studio and then use some functions to get an access token and it appears to work in the Emulator. I am not sure how secure this method is though because the json file with the key needs to be kept private.
InputStream is = getAssets().open("app.json");
GoogleCredentials credentials =
GoogleCredentials.fromStream(i).createScoped(Lists.newArrayList(scope));
credentials.refreshIfExpired();
AccessToken accesstoken = credentials.getAccessToken();
Add firebase to you android project. https://firebase.google.com/docs/android/setup You will create a project in Firebase and download a json file for configuration and add it in app directory. Add also dependencies in gradle files.
On Firebase console go to ML Kit section and create a AUTML model with your photos.
Train the model
When the training is finished you can download your model and downloaded 3 files in your assets/model directory. And it is ready to use. By this way you will use Firebase AutoML SDK and you dont need to generate the token.
Use your model and do predictions from application.
Steps are :
Prepare image for prediction
Prepare the model
Get the image labeler
Process the image for classification
public void findLabelsWithAutoML() {
Bitmap bitmap = null;
File file = new File(currentPhotoPath);
System.out.println("file "+file);
try {
bitmap = MediaStore.Images.Media
.getBitmap(getContentResolver(), Uri.fromFile(file));
} catch (Exception e) {
e.printStackTrace();
}
FirebaseVisionImageMetadata metadata = new FirebaseVisionImageMetadata.Builder()
.setWidth(480) // 480x360 is typically sufficient for
.setHeight(360) // image recognition
.setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21)
.setRotation(FirebaseVisionImageMetadata.ROTATION_0)
.build();
FirebaseVisionImage firebaseVisionImage = FirebaseVisionImage.fromBitmap(bitmap);
System.out.println("firebaseVisionImage :"+firebaseVisionImage);
FirebaseAutoMLLocalModel localModel = new FirebaseAutoMLLocalModel.Builder()
.setAssetFilePath("model/manifest.json")
.build();
FirebaseVisionOnDeviceAutoMLImageLabelerOptions labelerOptions = new FirebaseVisionOnDeviceAutoMLImageLabelerOptions.Builder(localModel)
.setConfidenceThreshold(0.65F) // Evaluate your model in the Firebase console
// to determine an appropriate value.
.build();
FirebaseVisionImageLabeler firebaseVisionImageLabeler = null;
try {
firebaseVisionImageLabeler = FirebaseVision.getInstance().getOnDeviceAutoMLImageLabeler(labelerOptions);
} catch (Exception e) {
e.printStackTrace();
}
firebaseVisionImageLabeler.processImage(firebaseVisionImage)
.addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionImageLabel>>() {
#Override
public void onSuccess(List<FirebaseVisionImageLabel> labels) {
for (FirebaseVisionImageLabel label : labels) {
System.out.println("label " + label.getText() + " score: " + (label.getConfidence() * 100));
}
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
//
}
});
}
I am trying to get the device token once the person has logged into the app and send back the device token to our server. The login process is done in async.
According to this post , you can get the token in activities but is it possible to get in async?
I have tried to put the code in async but does not work. (I changed the MainActivity.this to (Activity)mycontext, where I have defined mycontext as a Context)
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( (Activitiy) mycontext, new OnSuccessListener<InstanceIdResult>() {
#Override
public void onSuccess(InstanceIdResult instanceIdResult) {
String newToken = instanceIdResult.getToken();
Log.e("newToken",newToken);
}
});
Thank you very much
thanks to Shashanth , the solution is just to get the token in one activity and pass that token value to async
I want to generate and store token everytime users login. I tried for my code below, but it always generate same token when I login with another account.
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
AlertDialog.dismiss();
if (!task.isSuccessful()) {
if (password.length() < 6) {
new SweetAlertDialog(LoginActivity.this, SweetAlertDialog.ERROR_TYPE)
.setTitleText("Oops...")
.setContentText("Enter minimum 6 charachters !! ")
.show();
} else {
passwordInput.setText("");
new SweetAlertDialog(LoginActivity.this, SweetAlertDialog.ERROR_TYPE)
.setTitleText("Oops...")
.setContentText("Authentication failed !!")
.show();
}
} else {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
FirebaseUser users = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("users/"+ users.getUid());
String token = FirebaseInstanceId.getInstance().getToken();
Log.e("tokenid",""+token);
mDatabase.child("token_id").setValue(token);
finish();
}
}
});
}
});
please help, thanks..
When using the following line of code:
String token = FirebaseInstanceId.getInstance().getToken();
You are getting the Firebase Instance ID token, which is also known as FCM token, on the client side. You need to know that in Firebase there two different tokens:
A FCM Token/Instance ID token that identifies an installation of a specific application on a specific user device. It doesn't identify in any way a particular user.
An Auth ID token identifies a specific user of a specific application. It doesn't identify in any way a particular device.
The two tokens are quite different and serve different purposes. Please see the official documentation for Android regarding on how to retrieve ID tokens on clients.
The identity of the user is remembered indefinitely (or until you sign out). But their credentials (or rather, whether their token is still valid) are re-checked every hour.
FCM generates a registration token for the client app instance, hence It may happen that you'll get the same token for different users in your app. You can use forceRefresh to generate a new token every time. Register new token everytime user logins to any device and save it in DB and update with a new token on new login this way you will have a new token for each user on every login (If this fits your requirement)
Here is a good answer to understand how it works Firebase FCM force onTokenRefresh() to be called
Use UUID.randomUUID().toString()
You can read more here.
java docs - and here =)
since today I encountered the following issue with GCM "subscribe to topics".
Nexus 6, Android 6.0.1, Google Play Services 9.0.83
Using google-play-services:8.3.0 in app.
Step 1
I follow the documentation from Google for getting the token through the instance id.
After getting the token I successfully subscribe to the "topics/global" topic and store the token in the shared preferences.
protected void register() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
try {
// [START register_for_gcm]
// Initially this call goes out to the network to retrieve the token, subsequent calls
// are local.
// R.string.gcm_defaultSenderId (the Sender ID) is typically derived from google-services.json.
// See https://developers.google.com/cloud-messaging/android/start for details on this file.
// [START get_token]
InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
// [END get_token]
Log.i(TAG, "GCM Registration Token: " + token);
// TODO: Implement this method to send any registration to your app's servers.
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
sharedPreferences.edit().putString("token", token).apply();
// You should store a boolean that indicates whether the generated token has been
// sent to your server. If the boolean is false, send the token to your server,
// otherwise your server should have already received the token.
sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, true).apply();
// [END register_for_gcm]
} catch (Exception e) {
Log.d(TAG, "Failed to complete token refresh", e);
// If an exception happens while fetching the new token or updating our registration data
// on a third-party server, this ensures that we'll attempt the update at a later time.
sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false).apply();
}
// Notify UI that registration has completed, so the progress indicator can be hidden.
Intent registrationComplete = new Intent(QuickstartPreferences.REGISTRATION_COMPLETE);
LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
Step 2
After some time / on user interaction I want to subscribe to additional topics.
I fetch the token from the shared preferences and try to subscribe like before, but this time it fails with the "java.io.IOException: InternalServerError".
The exception is catched of course, but I do not know how to proceed now.
private void subscribeTopics() throws IOException {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String token = sharedPreferences.getString("token", null);
if(token == null) {
Log.e(TAG, "No token");
return;
}
GcmPubSub pubSub = GcmPubSub.getInstance(this);
for (String topic : TOPICS) {
pubSub.subscribe(token, "/topics/" + topic, null); // <--- FAILS HERE
}
Log.d(TAG, "Subscribed to topics.");
}
This process worked for the last 5 months without issues. Suddenly, since this morning, the subscription to additional topics (step 2) fails.
Any idea if the switch to Firebase Cloud Messaging (FCM) brought breaking changes?
Currently all my client apps are not usable.
Fast help is really appreciated.
I am part of the Google Cloud Messaging team.
We identified an issue in our backed that affected a small percentage of the topic subscriptions during the last 24 hours.
The issue has already been fixed, and the subscriptions should work correctly on all devices.
Please let us know if you are still experiencing this error.
Thanks Steffen for reporting the issue.