Android Sdk for Android不要求电子邮件许可

时间:2013-10-17 20:41:00

标签: android facebook permissions facebook-login

我正在我的Android应用程序中使用他们的教程和文档实现一个登录流程,只要在StackOverflow中有几个答案。

我有一个带有Button的UserRegistrationActivity(不是“LoginButton”)。 我想我要求正确的权限(basic_info,电子邮件)。 我得到一个facebook对话框询问权限,但是电子邮件没有列在其中,所以当我得到access_token时,我无法访问用户电子邮件服务器端。

我的代码:

package com.test.user;

import java.util.Arrays;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener; 
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;
import com.facebook.Session;
import com.facebook.SessionState;
import com.test.user.domain.User;
import com.test.user.services.UserLoginService;

public class UserRegistrationActivity extends SherlockActivity implements  OnClickListener,ConnectionCallbacks,
OnConnectionFailedListener  {
private static final String TAG = "UserRegistrationActivity";
private static final int REQUEST_CODE_RESOLVE_ERR = 9000;
private ProgressDialog mConnectionProgressDialog;
private PlusClient mPlusClient;
private ConnectionResult mConnectionResult;
private Session session;
private Session.StatusCallback statusCallback =
new SessionStatusCallback();
SharedPreferences settings;
UserRegistrationActivity _activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.activity_user_registration);
    settings = getSharedPreferences(TestApplication.PREFS_NAME, 0);

    setupActionBar();
    _activity = this;
}

public void onClickLogin(View v) {
    getSherlock().setProgressBarIndeterminateVisibility(true);
    Session session = Session.getActiveSession();
    if (session != null && !session.isOpened() && !session.isClosed()) {
        session.openForRead(new Session.OpenRequest(this)
        .setPermissions(Arrays.asList("basic_info","email"))
        .setCallback(statusCallback));
        } else {
        Session.openActiveSession(this, true, statusCallback);
    }
}

@Override
protected void onResume() {
    super.onResume();

}
private void setupActionBar() {
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
}
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case android.R.id.home:
        // app icon in action bar clicked; go home
        finish();
        return true;

        default:
        return super.onOptionsItemSelected(item);
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.user_registration, (com.actionbarsherlock.view.Menu) menu);
    return super.onCreateOptionsMenu(menu);
}


@Override
public void onDisconnected() {
    Log.d(TAG, "disconnected");
}
@Override
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
    if(requestCode == 2){
        if(responseCode ==RESULT_OK){
            if(intent.getExtras().containsKey("user_id")){
                String userId = intent.getExtras().getString("user_id");
                Intent returnIntent = new Intent();
                returnIntent.putExtra("user_id", userId);
                setResult(RESULT_OK,returnIntent);
                getSherlock().setProgressBarIndeterminateVisibility(true);
                finish();
                }else{
                getSherlock().setProgressBarIndeterminateVisibility(true);
            }
        }

    }else{
        Session.getActiveSession().onActivityResult(this, requestCode, responseCode, intent);
    }

}
private class SessionStatusCallback implements Session.StatusCallback {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        if(!TextUtils.isEmpty(session.getAccessToken())){
            SharedPreferences.Editor editor = settings.edit();
            editor.putString("user_token","@FB@:"+session.getAccessToken());
            editor.putString("user_token_type","FACEBOOK");
            // Commit the edits!
            editor.commit();

            new UserLoginService(_activity).execute("@FB@:"+session.getAccessToken());

        }
    }
}
public void onResultFromAsyncTask(User result) {
    if(result!=null){
        Intent returnIntent = new Intent();
        returnIntent.putExtra("user_id", result.getId());
        setResult(RESULT_OK,returnIntent);
        getSherlock().setProgressBarIndeterminateVisibility(true);
        finish();
    }
}

}

3 个答案:

答案 0 :(得分:2)

它并不是要求“电子邮件”权限,因为它始终是“else”情况,它使用空权限列表调用openActiveSession方法(默认情况下只获取“basic_info”)。

尝试将onClickLogin更改为:

Session session = Session.getActiveSession();
if (session == null) {
    session = new Session(this);
    Session.setActiveSession(session);
}
if (!session.isOpened() && !session.isClosed())) {
    session.openForRead(new Session.OpenRequest(this)
      .setPermissions(Arrays.asList("basic_info","email"))
      .setCallback(statusCallback));
}

答案 1 :(得分:1)

请求电子邮件权限的更快方法是使用Session.openActiveSession()调用的可选第三个参数,如下所示:

Session.openActiveSession(getActivity(), true, Arrays.asList("basic_info","email") ,new Session.StatusCallback() {...});

答案 2 :(得分:0)

使用以下代码与您合作

private void FaceBookSignIn()
   {
   facebookBtn.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View arg0) {
       Session currentSession = Session.getActiveSession();
        if (currentSession == null || currentSession.getState().isClosed()) {
            Session session = new Session.Builder(context).build();
            Session.setActiveSession(session);
            currentSession = session;
        }

        if (currentSession.isOpened()) {
               Session.openActiveSession(YourActivity.this, false, new Session.StatusCallback() {

                      @Override
                      public void call(final Session session, SessionState state,
                              Exception exception) {

                          if (session.isOpened()) {

                              Request.executeMeRequestAsync(session,
                                      new Request.GraphUserCallback() {

                                          @Override
                                          public void onCompleted(GraphUser user,
                                                  Response response) {
                                              if (user != null) {



                                                 UserID=user.getId();
                                                 firstName=user.getFirstName();
                                                 Lastname=user.getLastName();
                                                 Gender=user.getProperty("gender").toString();
                                                 Email=user.getProperty("email").toString();
                                                 DateofBirth=user.getBirthday();



                                              }
                                          }
                                      });
                          }
                      }
                  });
              }
            // Do whatever u want. User has logged in

         else if (!currentSession.isOpened()) {
            // Ask for username and password
            OpenRequest op = new Session.OpenRequest((Activity) context);

            op.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
            op.setCallback(null);

            List<String> permissions = new ArrayList<String>();
            permissions.add("publish_stream");
            permissions.add("user_likes");
            permissions.add("email");
            permissions.add("user_birthday");
            op.setPermissions(permissions);

            Session session = new Session.Builder(SignIn_Rewards.this).build();
            Session.setActiveSession(session);
            session.openForPublish(op);
        }



   }
  }) ;

      }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (Session.getActiveSession() != null)
          Session.getActiveSession().onActivityResult(this, requestCode,
                  resultCode, data);

      Session currentSession = Session.getActiveSession();
      if (currentSession == null || currentSession.getState().isClosed()) {
          Session session = new Session.Builder(context).build();
          Session.setActiveSession(session);
          currentSession = session;
      }

      if (currentSession.isOpened()) {
          Session.openActiveSession(this, true, new Session.StatusCallback() {

              @Override
              public void call(final Session session, SessionState state,
                      Exception exception) {

                  if (session.isOpened()) {

                      Request.executeMeRequestAsync(session,
                              new Request.GraphUserCallback() {

                                  @Override
                                  public void onCompleted(GraphUser user,
                                          Response response) {
                                      if (user != null) {

                                          fromFB=true;

                                         UserID=user.getId();
                                         firstName=user.getFirstName();
                                         Lastname=user.getLastName();
                                         Gender=user.getProperty("gender").toString();
                                         Email=user.getProperty("email").toString();
                                         DateofBirth=user.getBirthday();


                                      }
                                  }
                              });
                  }
              }
          });
      }
  }