FB login Android

前端 未结 1 2020
北海茫月
北海茫月 2020-12-20 06:54

After implementing the following , OnCancel method is being called :



        
相关标签:
1条回答
  • 2020-12-20 07:41

    If you are using facebook's default login button than no need to set onclick event. you can use it like this.

    loginButton = (LoginButton) findViewById(R.id.login_button);
    
    List < String > permissionNeeds = Arrays.asList("user_photos", "email",
    	"user_birthday", "public_profile", "AccessToken");
    loginButton.registerCallback(callbackManager,
    new FacebookCallback < LoginResult > () {@Override
    	public void onSuccess(LoginResult loginResult) {
    
    		System.out.println("onSuccess");
    
    		String accessToken = loginResult.getAccessToken()
    			.getToken();
    		Log.i("accessToken", accessToken);
    
    		GraphRequest request = GraphRequest.newMeRequest(
    		loginResult.getAccessToken(),
    		new GraphRequest.GraphJSONObjectCallback() {@Override
    			public void onCompleted(JSONObject object,
    			GraphResponse response) {
    				Log.i("LoginActivity", response.toString());
    				try {
    					id = object.getString("id");
    					try {
    						URL profile_pic = new URL(
    							"http://graph.facebook.com/" + id + "/picture?type=large");
    						Log.i("profile_pic",
    						profile_pic + "");
    
    					} catch (MalformedURLException e) {
    						e.printStackTrace();
    					}
    					String name = object.getString("name");
    					String email = object.getString("email");
    					String gender = object.getString("gender");
    					String birthday = object.getString("birthday");
    				} catch (JSONException e) {
    					e.printStackTrace();
    				}
    			}
    		});
    		Bundle parameters = new Bundle();
    		parameters.putString("fields",
    			"id,name,email,gender, birthday");
    		request.setParameters(parameters);
    		request.executeAsync();
    	}
    
    	@Override
    	public void onCancel() {
    		System.out.println("onCancel");
    	}
    
    	@Override
    	public void onError(FacebookException exception) {
    		System.out.println("onError");
    		Log.v("LoginActivity", exception.getCause().toString());
    	}
    });

    if you using customized facebook button than just check my answer here facebook login with customized button

    0 讨论(0)
提交回复
热议问题