0

在Android中進行Firebase身份驗證後,如何從Facebook和Google獲取性別和年齡風格? FirebaseUser不具有照片,顯示名稱,電子郵件,提供者標識和令牌標識以外的字段。如何使用Facebook和Google的Firebase Auth爲諸如性別和年齡等用戶設置額外的個人資料字段?

我已經在string.xml中添加了Facebook和Google的生日範圍string.xml,其定義見自述文件Firebase GitHub,但無法弄清楚額外字段的檢索情況。我沒有那種Android經驗,所以我想我做錯了什麼。

任何幫助,將不勝感激。謝謝!

+0

爲Facebook的使用方法,Jitty公佈。但谷歌使用這種方法:http://stackoverflow.com/questions/42406660/how-to-get-gender-and-birthday-from-google-provider-using-firebaseauth/42456960#42456960 – Sabeeh

回答

1

對於Facebook來說:

從火力得到的Facebook的accessToken是非常簡單的。我正在使用Firebase身份驗證用戶界面。使用Facebook進行身份驗證後,您將獲得來自Firebase用戶對象的基本信息,如顯示名稱,電子郵件,提供商詳細信息但是如果你想要更多的信息如性別,生日facebook Graph API就是解決方案。一旦用戶通過Facebook進行身份驗證,您就可以獲得像這樣的訪問令牌。

AccessToken.getCurrentAccessToken() 

但有時它會給你NULL值而不是有效的訪問令牌。確保你之前已經初始化了Facebook SDK。

public class MyApplication extends Application { 
    @Override 
    public void onCreate() { 
    super.onCreate(); 
    FacebookSdk.sdkInitialize(this); 
    } 
} After initialization use graphAPI 

if(AccessToken.getCurrentAccessToken()!=null) { 

    System.out.println(AccessToken.getCurrentAccessToken().getToken()); 

    GraphRequest request = GraphRequest.newMeRequest(
      AccessToken.getCurrentAccessToken(), 
      new GraphRequest.GraphJSONObjectCallback() { 
       @Override 
       public void onCompleted(JSONObject object, GraphResponse response) { 
        // Application code 
        try { 
         String email = object.getString("email"); 
         String gender = object.getString("gender"); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }); 
    Bundle parameters = new Bundle(); 
    parameters.putString("fields", "id,name,email,gender,birthday"); 
    request.setParameters(parameters); 
    request.executeAsync(); 
} 
else 
{ 
    System.out.println("Access Token NULL"); 
} 

對於谷歌:

在你的活動

private static final int RC_SIGN_IN = 8888;  

public void loadGoogleUserDetails() { 
     try { 
      // Configure sign-in to request the user's ID, email address, and basic profile. ID and 
      // basic profile are included in DEFAULT_SIGN_IN. 
      GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
        .requestEmail() 
        .build(); 

      // Build a GoogleApiClient with access to GoogleSignIn.API and the options above. 
      mGoogleApiClient = new GoogleApiClient.Builder(this) 
        .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() { 
         @Override 
         public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
          System.out.println("onConnectionFailed"); 
         } 
        }) 
        .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
        .build(); 

      Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); 
      startActivityForResult(signInIntent, RC_SIGN_IN); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 




@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()) { 
       GoogleSignInAccount acct = result.getSignInAccount(); 
       // Get account information 
       String PhotoUrl = acct.getPhotoUrl().toString(); 

      } 
     } 
    } 
相關問題