2017-08-01 13 views
0

私はFacebookのログインを実装していますが、データを別の登録アクティビティに持ちたいので、国、都市、州(すべて)のユーザーが必要です。私は研究しましたが、このデータをどのように持ってくるかをまだ理解できませんでした。アンドロイドのFacebookのログインでユーザーの国を取得するにはどうすればよいですか?

private void facebookLogin() { 
    mAuth = FirebaseAuth.getInstance(); 
    mCallbackManager = CallbackManager.Factory.create(); 
    //Login com facebook arrumar um lugar melhor e mais organizado.. 
    facebook.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() { 
     @Override 
     public void onSuccess(LoginResult loginResult) { 
      Log.d(TAG, "facebook:onSuccess:" + loginResult); 
      GraphRequest graphRequest = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() { 
       @Override 
       public void onCompleted(JSONObject object, GraphResponse response) { 
        Log.d("JSON", "" + response.getJSONObject().toString()); 
        try { 
         nome = object.optString("first_name"); 
         sobrenome = object.optString("last_name"); 
         email = object.optString("email"); 
         aniversario = object.optString("user_birthday"); 
         idFB = object.optString("id"); 
         sexo = object.getString("gender"); 
         paisLogin = object.getJSONObject("location").getString("country"); //???????????? 
         cidade = object.getJSONObject("location").getString("city");  //????????????????? 
         SaveSharedPreferences.setIdFacebook(getContext(),idFB); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }); 
      Bundle parameters = new Bundle(); 
      parameters.putString("fields","id,first_name,last_name,email,location,gender"); 
      graphRequest.setParameters(parameters); 
      graphRequest.executeAsync(); 
      AuthCredential credential = FacebookAuthProvider.getCredential(loginResult.getAccessToken().getToken()); 
      handleFacebookAccessToken(credential); 
      //handleFacebookAccessToken(loginResult.getAccessToken()); 
     } 

     @Override 
     public void onCancel() { 
      Log.d(TAG, "facebook:onCancel"); 
      // ... 
     } 

     @Override 
     public void onError(FacebookException error) { 
      Log.d(TAG, "facebook:onError", error); 
      // ... 
     } 
    }); 

どのように私は、このデータを取得することができます:ここで

は、私が使用しているコードのですか?

+0

'' user_location' location' – jagapathi

+0

は私が市、州、国を望んでいません。 User_locationは私にこのデータを与えません。 –

答えて

0

これは私の作品(法getLocationUser(locationID)を参照してください):

private void facebookLogin() { 
    mAuth = FirebaseAuth.getInstance(); 
    mCallbackManager = CallbackManager.Factory.create(); 
    //Login com facebook arrumar um lugar melhor e mais organizado.. 
    facebook.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() { 
     @Override 
     public void onSuccess(LoginResult loginResult) { 
      Log.d(TAG, "facebook:onSuccess:" + loginResult); 
      GraphRequest graphRequest = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() { 
       @Override 
       public void onCompleted(JSONObject object, GraphResponse response) { 
        Log.d("JSON", "" + response.getJSONObject().toString()); 
        try { 
         nome = object.optString("first_name"); 
         sobrenome = object.optString("last_name"); 
         email = object.optString("email"); 
         aniversario = object.optString("user_birthday"); 
         idFB = object.optString("id"); 
         sexo = object.getString("gender"); 
         locationID = object.getJSONObject("location").getString("id"); 
         getLocationUser(locationID); <<<<----- 
         SaveSharedPreferences.setIdFacebook(getContext(),idFB); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }); 
      Bundle parameters = new Bundle(); 
      parameters.putString("fields","id,first_name,last_name,email,location,gender"); 
      graphRequest.setParameters(parameters); 
      graphRequest.executeAsync(); 
      AuthCredential credential = FacebookAuthProvider.getCredential(loginResult.getAccessToken().getToken()); 
      handleFacebookAccessToken(credential); 
      //handleFacebookAccessToken(loginResult.getAccessToken()); 
     } 

     @Override 
     public void onCancel() { 
      Log.d(TAG, "facebook:onCancel"); 
      // ... 
     } 

     @Override 
     public void onError(FacebookException error) { 
      Log.d(TAG, "facebook:onError", error); 
      // ... 
     } 
    }); 
} 

private void getLocationUser(String id) { 
    Bundle params = new Bundle(); 
    params.putString("location", "id"); 
    new GraphRequest(
      AccessToken.getCurrentAccessToken(), 
      id+"/?fields=location", 
      params, 
      HttpMethod.GET, 
      new GraphRequest.Callback() { 
       public void onCompleted(GraphResponse response) { 
        Log.e("Response 2", response + ""); 
        try { 
         paisLogin = (String) response.getJSONObject().getJSONObject("location").get("country"); 
         cidade = (String) response.getJSONObject().getJSONObject("location").get("city"); 
         UF = (String) response.getJSONObject().getJSONObject("location").get("state"); 
         Log.e("Location", paisLogin); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } 
      } 
    ).executeAsync(); 
} 
関連する問題