在生成签名APK后,本机firebase phone auth无法正常工作

时间:2018-05-14 17:42:01

标签: android firebase react-native

我使用firebase phone auth构建反应本机应用程序 之后我通过

在android中生成了签名的apk
  

cd android&& ./gradlew assembleRelease

我在手机上测试了我生成的apk,当我输入手机时没有发生任何事情并且它返回我的错误

  

错误:此应用无权使用firebase身份验证。请确认在firebase控制台中配置了正确的软件包名称和sha-1。

BUT 当我运行没有apk的应用程序时,

  

react-native run-android

并且身份验证工作正常,我注册了用户。

 confirmPhone = async (phoneNumber) => {
        const phoneWithAreaCode = phoneNumber.replace(/^0+/,'+972');
        return new Promise((res, rej) => {
            firebase.auth().verifyPhoneNumber(phoneWithAreaCode)
                .on('state_changed', async (phoneAuthSnapshot) => {
                    console.log('phone-->',phoneAuthSnapshot)
                    switch (phoneAuthSnapshot.state) {
                    case firebase.auth.PhoneAuthState.AUTO_VERIFIED:
                        await this.confirmCode(phoneAuthSnapshot.verificationId, phoneAuthSnapshot.code, phoneAuthSnapshot)
                        res(phoneAuthSnapshot)

                        break

                    case firebase.auth.PhoneAuthState.CODE_SENT:
                        UserStore.setVerificationId(phoneAuthSnapshot.verificationId)
                        res(phoneAuthSnapshot)
                        break

                    case firebase.auth.PhoneAuthState.AUTO_VERIFY_TIMEOUT: 
                        UserStore.setVerificationId(phoneAuthSnapshot.verificationId)
                        UserStore.setErrorCodeAuthentication('SMS code has expired')
                        res(phoneAuthSnapshot)


                    case firebase.auth.PhoneAuthState.ERROR:
                        // console.log(phoneAuthSnapshot)
                        // if(NavigationStore.CurrentRoute == 'Login'){
                        //     UserStore.setErrorCodeAuthentication('Please enter valid phone number')
                        // }else
                        //     UserStore.setErrorCodeAuthentication('Pin code invalid')
                        rej(phoneAuthSnapshot)
                        break

                    }
                })
        })
    }

    confirmCode = async (verificationId, code, phoneAuthSnapshot) => {
           try{
            const credential = await firebase.auth.PhoneAuthProvider.credential(UserStore.verificationId, code)
            UserStore.setCodeInput(code)
            UserStore.setUserCredentials(credential)
            AppStore.setAlreadyRegister(true)
            await this.authenticate(credential)
            return credential
           } catch(e){
               throw new Error(e)
           }


    }

    authenticate = async (credential) => {
         await firebase.auth().signInAndRetrieveDataWithCredential(credential)

    }`

2 个答案:

答案 0 :(得分:2)

如果你还没有弄清楚,我也有同样的问题。

  1. 打开 Android Studio
  2. 点击右侧的 Gradle任务栏
  3. 从[您的应用名称]>双击 signingReport 任务> android> signingReport
  4. 记下生成的 Variant:Relase SHA-1。它应该是该任务输出的第一个。
  5. 将此SHA-1附加到 firebase控制台中的SHA-1列表中。你现在应该有2个,一个用于调试,一个用于发布。
  6. 下载新鲜的 google-services.json 并将其放入 android / app 目录。
  7. 重建发布应用

答案 1 :(得分:1)

转到您签名的 apk 所在的文件位置。

1.Opem cmd 并写入

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.layers.experimental import preprocessing
from matplotlib import pyplot as plt
import numpy as np

x_train = tf.random.normal(shape=(1000,10,1), dtype = tf.float32)
x_test = tf.random.normal(shape=(100,10,1), dtype = tf.float32)
y_train = tf.one_hot(tf.random.uniform(shape=[1000],minval=0, maxval=2, dtype=tf.int32),8)
y_test = tf.one_hot(tf.random.uniform(shape=[100],minval=0, maxval=2, dtype=tf.int32),8)
tf.print(y_train)


model=tf.keras.Sequential()
model.add(layers.Conv1D(256, kernel_size=5, strides=1, padding='same', activation='relu', input_shape=(x_train.shape[1], 1)))
model.add(layers.MaxPooling1D(pool_size=5, strides = 2, padding = 'same'))

model.add(layers.Conv1D(256, kernel_size=5, strides=1, padding='same', activation='relu'))
model.add(layers.MaxPooling1D(pool_size=5, strides = 2, padding = 'same'))

model.add(layers.Conv1D(128, kernel_size=5, strides=1, padding='same', activation='relu'))
model.add(layers.MaxPooling1D(pool_size=5, strides = 2, padding = 'same'))
model.add(layers.Dropout(0.2))

model.add(layers.Conv1D(64, kernel_size=5, strides=1, padding='same', activation='relu'))
model.add(layers.MaxPooling1D(pool_size=5, strides = 2, padding = 'same'))

model.add(layers.Flatten())
model.add(layers.Dense(units=32, activation='relu'))
model.add(layers.Dropout(0.3))

model.add(layers.Dense(units=8, activation='softmax'))
model.compile(optimizer = 'adam' , loss = 'categorical_crossentropy' , metrics = ['accuracy'])

model.summary()

rlrp = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.4, verbose=0, patience=2, min_lr=0.0000001)
reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, verbose=0, mode='auto', min_delta=0.0001, cooldown=0, min_lr=0)
model.fit(x_train, y_train, batch_size=64, epochs=50, validation_data=(x_test, y_test), callbacks=[rlrp])
#history=model.fit(x_train, y_train, callbacks=[rlrp])

encoder = OneHotEncoder()
Y = encoder.fit_transform(np.array(Y).reshape(-1,1)).toarray()

# splitting data
x_train, x_test, y_train, y_test = train_test_split(X, Y, random_state=0, shuffle=True)
x_train.shape, y_train.shape, x_test.shape, y_test.shape

# scaling our data with sklearn's Standard scaler
scaler = StandardScaler()
x_train = scaler.fit_transform(x_train)
x_test = scaler.transform(x_test)
x_train.shape, y_train.shape, x_test.shape, y_test.shape

# making our data compatible to model.
x_train = np.expand_dims(x_train, axis=2)
x_test = np.expand_dims(x_test, axis=2)
x_train.shape, y_train.shape, x_test.shape, y_test.shape, x_train.shape[1]

2.复制 SHA1 并附加到 firebase 控制台。

它在我的情况下有效