android–Firebase Auth获取更多用户信息(年龄,性别)

android–Firebase Auth获取更多用户信息(年龄,性别),第1张

概述我正在为我的Android应用使用Firebase身份验证.用户可以使用多个提供商(Google,Facebook,Twitter)登录.成功登录后,有没有办法使用Firebaseapi从这些提供商处获取用户性别/出生日期?解决方法:很遗憾,Firebase在成功登录后没有任何内置功能来获取用户的性别/生日.您必须自己从每个

我正在为我的Android应用使用Firebase身份验证.用户可以使用多个提供商(Google,Facebook,Twitter)登录.

成功登录后,有没有办法使用Firebase API从这些提供商处获取用户性别/出生日期?

解决方法:

很遗憾,Firebase在成功登录后没有任何内置功能来获取用户的性别/生日.您必须自己从每个提供程序中检索这些数据.

以下是使用Google People API从Google获取用户性别的方法

public class SignInActivity extends AppCompatActivity implements        Googleapiclient.ConnectionCallbacks,        Googleapiclient.OnConnectionFailedListener,        VIEw.OnClickListener {    private static final int RC_SIGN_IN = 9001;    private Googleapiclient mGoogleapiclient;    private FirebaseAuth mAuth;    private FirebaseAuth.AuthStateListener mAuthListener;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_Google_sign_in);        // We can only get basic information using FirebaseAuth        mAuth = FirebaseAuth.getInstance();        mAuthListener = new FirebaseAuth.AuthStateListener() {            @OverrIDe            public voID onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {                FirebaseUser user = firebaseAuth.getCurrentUser();                if (user != null) {                    // User is signed in to Firebase, but we can only get                     // basic info like name, email, and profile photo url                    String name = user.getdisplayname();                    String email = user.getEmail();                    Uri photoUrl = user.getPhotoUrl();                    // Even a user's provIDer-specific profile information                    // only reveals basic information                    for (UserInfo profile : user.getProvIDerData()) {                        // ID of the provIDer (ex: Google.com)                        String provIDerID = profile.getProvIDerID();                        // UID specific to the provIDer                        String profileUID = profile.getUID();                        // name, email address, and profile photo Url                        String profiledisplayname = profile.getdisplayname();                        String profileEmail = profile.getEmail();                        Uri profilePhotoUrl = profile.getPhotoUrl();                    }                } else {                    // User is signed out of Firebase                }            }        };        // Google sign-in button Listener        findVIEwByID(R.ID.Google_sign_in_button).setonClickListener(this);        // Configure GoogleSignInoptions        GoogleSignInoptions gso = new GoogleSignInoptions.Builder(GoogleSignInoptions.DEFAulT_SIGN_IN)                .requestIDToken(getString(R.string.server_clIEnt_ID))                .requestServerAuthCode(getString(R.string.server_clIEnt_ID))                .requestemail()                .requestScopes(new Scope(PeopleScopes.USERINFO_PROfile))                .build();        // Build a Googleapiclient with access to the Google Sign-In API and the        // options specifIEd by gso.        mGoogleapiclient = new Googleapiclient.Builder(this)                .enableautoManage(this, this)                .addOnConnectionFailedListener(this)                .addConnectionCallbacks(this)                .addAPI(Auth.Google_SIGN_IN_API, gso)                .build();    }    @OverrIDe    public voID onClick(VIEw v) {        switch (v.getID()) {            case R.ID.Google_sign_in_button:                signIn();                break;        }    }    private voID signIn() {        Intent signInIntent = Auth.GoogleSignInAPI.getSignInIntent(mGoogleapiclient);        startActivityForResult(signInIntent, RC_SIGN_IN);    }    @OverrIDe    public voID onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        // Result returned from launching the Intent from GoogleSignInAPI.getSignInIntent(...);        if (requestCode == RC_SIGN_IN) {            GoogleSignInResult result = Auth.GoogleSignInAPI.getSignInResultFromIntent(data);            if (result.isSuccess()) {                // Signed in successfully                GoogleSignInAccount acct = result.getSignInAccount();                // execute AsyncTask to get gender from Google People API                new GetGendersTask().execute(acct);                // Google Sign In was successful, authenticate with Firebase                firebaseAuthWithGoogle(acct);            }        }    }    class GetGendersTask extends AsyncTask<GoogleSignInAccount, VoID, List<Gender>> {        @OverrIDe        protected List<Gender> doInBackground(GoogleSignInAccount... GoogleSignInAccounts) {            List<Gender> genderList = new ArrayList<>();            try {                httpTransport httpTransport = new NethttpTransport();                JacksonFactory JsonFactory = JacksonFactory.getDefaultInstance();                //Redirect URL for web based applications.                // Can be empty too.                String redirectUrl = "urn:IEtf:wg:oauth:2.0:oob";                // Exchange auth code for access token                GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodetokenRequest(                        httpTransport,                        JsonFactory,                        getApplicationContext().getString(R.string.server_clIEnt_ID),                        getApplicationContext().getString(R.string.server_clIEnt_secret),                        GoogleSignInAccounts[0].getServerAuthCode(),                        redirectUrl                ).execute();                GoogleCredential credential = new GoogleCredential.Builder()                        .setClIEntSecrets(                            getApplicationContext().getString(R.string.server_clIEnt_ID),                             getApplicationContext().getString(R.string.server_clIEnt_secret)                        )                        .setTransport(httpTransport)                        .setJsonFactory(JsonFactory)                        .build();                credential.setFromTokenResponse(tokenResponse);                People peopleService = new People.Builder(httpTransport, JsonFactory, credential)                        .setApplicationname("My Application name")                        .build();                // Get the user's profile                Person profile = peopleService.people().get("people/me").execute();                genderList.addAll(profile.getGenders());            }            catch (IOException e) {                e.printstacktrace();            }            return genderList;        }        @OverrIDe        protected voID onPostExecute(List<Gender> genders) {            super.onPostExecute(genders);            // iterate through the List of Genders to            // get the gender value (male, female, other)            for (Gender gender : genders) {                String genderValue = gender.getValue();            }        }    }}

您可以在Accessing Google APIs找到更多信息

总结

以上是内存溢出为你收集整理的android – Firebase Auth获取更多用户信息(年龄,性别)全部内容,希望文章能够帮你解决android – Firebase Auth获取更多用户信息(年龄,性别)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1102155.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存