您好,我在没有登录按钮的情况下使用Facebook创建登录。它工作正常,但在关闭facbook webkit登录表单时,它会再次打开,持续显示请求权限。给我解决方案。
private void FacebookLogin() {
// TODO Auto-generated method stub
final Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(
this, Arrays.asList("email"));
Session openActiveSession = Session.openActiveSession(this, true,
new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state,
Exception exception) {
if (session.isOpened()) {
session.requestNewReadPermissions(newPermissionsRequest);
Request getMe = Request.newMeRequest(session,
new GraphUserCallback() {
@Override
public void onCompleted(GraphUser user,
Response response) {
if (user != null) {
org.json.JSONObject graphResponse = response
.getGraphObject()
.getInnerJSONObject();
String email = graphResponse
.optString("email");
String id = graphResponse
.optString("id");
}
}
});
getMe.executeAsync();
} else {
if (!session.isOpened())
Log.d("FACEBOOK", "!session.isOpened()");
else
Log.d("FACEBOOK", "isFetching");
}
}
});
}
@Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
// TODO Auto-generated method stub
super.onActivityResult(arg0, arg1, arg2);
Session.getActiveSession().onActivityResult(this, arg0, arg1, arg2);
}
答案 0 :(得分:0)
试试这个
public class LoginFragment extends Fragment{
boolean isFetching = false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.login_fragment, null);
Button button = (Button) view.findViewById(R.id.login_button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
performFacebookLogin();
}
});
return view;
}
private void performFacebookLogin()
{
Log.d("FACEBOOK", "performFacebookLogin");
final Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(getActivity(), Arrays.asList("email"));
Session openActiveSession = Session.openActiveSession(getActivity(), true, new Session.StatusCallback()
{
@Override
public void call(Session session, SessionState state, Exception exception)
{
Log.d("FACEBOOK", "call");
if (session.isOpened() && !isFetching)
{
Log.d("FACEBOOK", "if (session.isOpened() && !isFetching)");
isFetching = true;
session.requestNewReadPermissions(newPermissionsRequest);
Request getMe = Request.newMeRequest(session, new GraphUserCallback()
{
@Override
public void onCompleted(GraphUser user, Response response)
{
Log.d("FACEBOOK", "onCompleted");
if (user != null)
{
Log.d("FACEBOOK", "user != null");
org.json.JSONObject graphResponse = response.getGraphObject().getInnerJSONObject();
String email = graphResponse.optString("email");
String id = graphResponse.optString("id");
String facebookName = user.getUsername();
if (email == null || email.length() < 0)
{
Toast.makeText(getActivity(),
"An email address is required for your account, we could not find an email associated with this Facebook account. Please associate a email with this account or login the oldskool way.", Toast.LENGTH_LONG).show();
return;
}
}
}
});
getMe.executeAsync();
}
else
{
if (!session.isOpened())
Log.d("FACEBOOK", "!session.isOpened()");
else
Log.d("FACEBOOK", "isFetching");
}
}
});
}
}
现在在片段布局中添加一个按钮
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login With Facebook" />
它适用于我,只有一次弹出请求权限尝试并让我知道
答案 1 :(得分:0)
我发现了两种方法。
第一个 - 你添加到XML facebook按钮并隐藏(设置visability = GONE)它。初始化此按钮并在您自定义事件调用facebookButton.performClick();
上第二种方式 - 使用这部分代码:
callbackManager = CallbackManager.Factory.create();
List<String> permission = new ArrayList<String>();
permission.add("email");
LoginManager loginManager = LoginManager.getInstance();
loginManager.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
if (facebookDialog.isShowing()) {
loadingProgressBar.setVisibility(View.VISIBLE);
singViaFacebook.setText(R.string.dialog_facebook_loggin);
}
loadProfile();
}
@Override
public void onCancel() {
lockLoginButton(true);
startTimer();
}
@Override
public void onError(FacebookException exception) {
lockLoginButton(true);
startTimer();
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
当你想要调用弹出窗口时调用这段代码:
loginManager.logInWithReadPermissions(this, permission)
适用于Facebook SDK [4,5]
答案 2 :(得分:0)
首先声明回调管理器,
私有CallbackManager callbackManager;
私人AccessToken accessToken;
将此代码放在OnCreate()::
中callbackManager = CallbackManager.Factory.create(); LoginManager.getInstance()。registerCallback(callbackManager,new FacebookCallback(){
@Override
public void onSuccess(LoginResult loginResult) {
accessToken = loginResult.getAccessToken();
getFacebookUserProfile(accessToken);
}
@Override
public void onCancel() {
Toast.makeText(StartUpActivity.this, "Login with facebook canceled.", Toast.LENGTH_LONG).show();
}
@Override
public void onError(FacebookException error) {
Toast.makeText(StartUpActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
}
});
然后点击按钮,如
btnFacebook.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
accessToken = AccessToken.getCurrentAccessToken();
if (accessToken != null) {
getFacebookUserProfile(accessToken);
} else {
LoginManager.getInstance().logInWithReadPermissions(StartUpActivity.this, Arrays.asList("public_profile", "email"));
}
}
});
然后覆盖,
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CallbackManagerImpl.RequestCodeOffset.Login.toRequestCode()) {
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
然后使用此功能获取用户个人资料,
private void getFacebookUserProfile(AccessToken accessToken) {
GraphRequest request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
authenticateUser(object);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id, name, first_name,last_name, email");
request.setParameters(parameters);
request.executeAsync();
}
希望它适合你