为什么facebook登录在开发期间工作,而不是在部署期间?

时间:2013-02-14 06:57:17

标签: android facebook

当我通过Eclipse通过ADB安装我的应用程序时,然后facebook登录以及发布到用户的墙上工作(使用facebook SDK 3.0),但是当我打包我的应用程序然后安装该应用程序时,那么facebook登录不起作用。我通过登录按钮获取片段,当我按下登录按钮时,它会显示应用程序想要访问我的公开个人资料和好友列表的对话框。当我单击确定时,它会返回到具有登录按钮的片段。

我的问题是:在部署期间我应该如何更改facebook SDK集成?

我的片段代码(删除了不相关的部分):

public class Fragment_shareFacebook extends SherlockFragment {
Context context;    //context and fields:
View v;
private Button shareButton;
private MainActvityCommunicatorIntentBasedLeaveManagement intentleaveset;
private EditText toshareText;
private TextView textInstructionsOrLink, sharedText, fb_share_title_preview, fb_share_text_preview, sharesubsubtitle;
private ViewSwitcher switcher; 
private LinearLayout fb_preview_keeper, mainlayoutholderfbshare, loggedinscreen;
String subject, content;
private boolean switched;
private UiLifecycleHelper uiHelper;
private static final int REAUTH_ACTIVITY_CODE = 100;
private UserSettingsFragment usersetting;
private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
private Session.StatusCallback callback = 
    new Session.StatusCallback() {
    @Override
    public void call(Session session, 
            SessionState state, Exception exception) {
        onSessionStateChange(session, state, exception);
    }
};

/**
 * empty constructor
 */
public Fragment_shareFacebook(){

}


@Override
/**
 * initialize the intentleaveset to inform main that we have an intent based leave standing by
 */
public void onAttach(Activity activity) {
    super.onAttach(activity);
    context = getActivity();
    intentleaveset=(MainActvityCommunicatorIntentBasedLeaveManagement) context;
}

@Override
/**
 * get the content that needs to be shared, also retrieve the pendingPublishReauthorization if necessary
 */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setHasOptionsMenu(true);
    uiHelper = new UiLifecycleHelper(this.getActivity(), callback);
    uiHelper.onCreate(savedInstanceState);
    subject = getArguments().getString("subject");
    content = getArguments().getString("text");


}
@Override
public void onResume() {
    super.onResume();
    uiHelper.onResume();
    Session session = Session.getActiveSession();
    this.onSessionStateChange(session, session.getState(), null);
}

@Override
public void onPause() {
    super.onPause();
    uiHelper.onPause();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==REAUTH_ACTIVITY_CODE){
        uiHelper.onActivityResult(requestCode, resultCode, data);
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
    uiHelper.onDestroy();
}

@Override 
public void onDestroyView(){
    //note: we have to remove the fragment again!
    try{
        FragmentTransaction transaction = this.getActivity().getSupportFragmentManager().beginTransaction();
        transaction.remove(usersetting);
        transaction.commit();
    }catch(Exception e){

    }
    super.onDestroyView();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    uiHelper.onSaveInstanceState(outState);
}
//hvg: dze even disecten.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    v = inflater.inflate(R.layout.fragment_facebook_share, container, false);
    textInstructionsOrLink = (TextView) v.findViewById(R.id.textInstructionsOrLink);
    fb_share_title_preview= (TextView) v.findViewById(R.id.fbsharetitlepreview);
    fb_share_text_preview= (TextView) v.findViewById(R.id.fbsharetextpreview);
    sharesubsubtitle=(TextView) v.findViewById(R.id.sharesubsubtitle);
    loggedinscreen=(LinearLayout) v.findViewById(R.id.loggedinscreen);
    usersetting=(UserSettingsFragment) this.getActivity().getSupportFragmentManager().findFragmentById(R.id.userSettingsFragment);
    //authButton = (LoginButton) v.findViewById(R.id.authButton);
    shareButton = (Button) v.findViewById(R.id.shareButton);
    //usernamefield= (TextView) v.findViewById(R.id.selection_user_name);
    toshareText = (EditText) v.findViewById(R.id.toshareText);
    fb_preview_keeper= (LinearLayout) v.findViewById(R.id.fb_preview_keeper);
    mainlayoutholderfbshare = (LinearLayout) v.findViewById(R.id.mainlayoutholderfbshare);
    switcher=(ViewSwitcher)v.findViewById(R.id.fb_switcher);
    mainlayoutholderfbshare.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if(toshareText.isFocused()){

                    Log.i("Focussed", "--" + event.getX() + " : " + event.getY() + "--");

                    if (toshareText.isFocused()) {
                        Rect outRect = new Rect();
                        toshareText.getGlobalVisibleRect(outRect);
                        if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) {
                            toshareText.clearFocus();
                            InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
                            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                        }
                    }
                }
                Log.i("X-Y coordinate", "--" + event.getX() + " : " + event.getY() + "--");
            //Toast.makeText(getBaseContext(), "Clicked", Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    //toshareText.setText(content);
    switcher=(ViewSwitcher) v.findViewById(R.id.fb_switcher);
    sharedText=(TextView) v.findViewById(R.id.sharedText);
    if(switcher.getDisplayedChild()==0){
        switched=false;
    }else{
        switched=true;
    }
    //note that we need a custom onclicklistener to share the story to fb 
    shareButton.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            intentleaveset.setIntentleave(true);
            publishStory();

        }

    });
   //note to main: we are leaving with an intent!
    intentleaveset.setIntentleave(true);
    Log.d("fsf: leaveintent","settrue");

    Session session = Session.getActiveSession();
    SessionState state = Session.getActiveSession().getState();
    onSessionStateChange(session, state, null);
    return v;
}

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    // Only make changes if the activity is visible
    intentleaveset.setIntentleave(true);
    Log.d("switcher-init", Integer.toString(switcher.getDisplayedChild()));
    if (state.isOpened()) {
        // If the session state is open:
        // Show the authenticated fragment
        shareButton.setVisibility(View.VISIBLE);
        toshareText.setVisibility(View.VISIBLE);
        fb_preview_keeper.setVisibility(View.VISIBLE);

        LayoutParams params = fb_preview_keeper.getLayoutParams();
        params.height=LayoutParams.WRAP_CONTENT;
        params.width=LayoutParams.MATCH_PARENT;
        fb_preview_keeper.setLayoutParams(params);

        params=shareButton.getLayoutParams();
        params.height=LayoutParams.WRAP_CONTENT;
        params.width=LayoutParams.MATCH_PARENT;
        shareButton.setLayoutParams(params);
        params=toshareText.getLayoutParams();
        params.width=LayoutParams.MATCH_PARENT;
        params.height=LayoutParams.WRAP_CONTENT;
        toshareText.setLayoutParams(params);
        toshareText.setMinLines(3);
        toshareText.setScroller(new Scroller(context));
        toshareText.setVerticalScrollBarEnabled(true);
        fb_share_title_preview.setText(subject);
        fb_share_text_preview.setText(getString(R.string.facebook_share_subtitle).concat("\n \n").concat(content));
        if(!switched){
            textInstructionsOrLink.setText(getString(R.string.facebookexplanationafterlgoong));
        }else{
            textInstructionsOrLink.setText(getString(R.string.facebookexplanationaftershared));
            if(switcher.getDisplayedChild()!=1){
                switcher.showNext();
            }

            /*
            if(switcher.getDisplayedChild()==1){
                switcher.showPrevious();
            }else{
                switcher.showNext();
            }
            */
        }
        params=loggedinscreen.getLayoutParams();
        params.height=LayoutParams.WRAP_CONTENT;
        loggedinscreen.setLayoutParams(params);
        sharesubsubtitle.setFocusable(true);
        sharesubsubtitle.requestFocus();
    } else {
        // If the session state is closed:
        // Show the login fragment
        if(switcher.getDisplayedChild()==1){
            switcher.showPrevious();
        }

        switched=false;
        shareButton.setVisibility(View.INVISIBLE);
        //shareButton.setHeight(0);
        toshareText.setVisibility(View.INVISIBLE);
        //toshareText.setHeight(0);
        //LayoutParams params = switcher.getLayoutParams();
        //params.height=0;
        //params.width=params.MATCH_PARENT;
        //switcher.setLayoutParams(params);
        //fb_preview_keeper.setVisibility(View.INVISIBLE);
        //params = fb_preview_keeper.getLayoutParams();
        //params.height=0;
        //params.width=params.MATCH_PARENT;
        //fb_preview_keeper.setLayoutParams(params);
        LayoutParams params=loggedinscreen.getLayoutParams();
        params.height=0;
        loggedinscreen.setLayoutParams(params);
        sharedText.setText("");
        textInstructionsOrLink.setText(this.getResources().getString(R.string.facebookexplainbeforelogon));

    }
}


private void publishStory() {
    Session session = Session.getActiveSession();

    if (session != null){

        // Check for publish permissions    
        List<String> permissions = session.getPermissions();
        if (!isSubsetOf(PERMISSIONS, permissions)) {
            Session.NewPermissionsRequest newPermissionsRequest = new Session
                    .NewPermissionsRequest(this, PERMISSIONS)
                    .setRequestCode(REAUTH_ACTIVITY_CODE);
            session.requestNewPublishPermissions(newPermissionsRequest);
            //session.reauthorizeForPublish(newPermissionsRequest);
            return;
        }

        Bundle postParams = new Bundle();
        postParams.putString("name", subject);
        postParams.putString("caption", getString(R.string.facebook_share_subtitle));
        postParams.putString("description", content);
        if(toshareText.getText()!=null){
            postParams.putString("message",  toshareText.getText().toString());
        }else{
            postParams.putString("message",  " ");
        }
        postParams.putString("link", "http://www.thewonderweeks.com");
        postParams.putString("picture", "www.thewonderweeks.com/apple-touch-icon-114x114.png");


        Request.Callback callback= new Request.Callback() {
            public void onCompleted(Response response) {
                JSONObject graphResponse;
                try{
                    graphResponse = response
                                           .getGraphObject()
                                           .getInnerJSONObject();
                }catch (Exception e){
                    if(e.getMessage()!=null){
                        Log.d("errorinFBcallback", e.getMessage());
                    }
                    return ;
                }
                String postId = null;
                try {
                    postId = graphResponse.getString("id");
                } catch (JSONException e) {
                    Log.i("errormessagefacebookfragmenthtingy",
                        "JSON error "+ e.getMessage());
                }
                FacebookRequestError error = response.getError();
                if (error != null) {
                    Toast.makeText(getActivity()
                         .getApplicationContext(),
                         error.getErrorMessage(),
                         Toast.LENGTH_SHORT).show();
                    } else {
                        Log.d("post id of posted material", postId);
                        shareButton.setVisibility(View.INVISIBLE);
                        if(switcher.getDisplayedChild()!=1){
                            switcher.showNext();
                        }
                        switched=true;
                        sharedText.setText(getString(R.string.successhare).concat(" ").concat(toshareText.getText().toString()));
                        textInstructionsOrLink.setText(getString(R.string.facebookexplanationaftershared));
                }
            }
        };

        Request request = new Request(session, "me/feed", postParams, 
                              HttpMethod.POST, callback);

        RequestAsyncTask task = new RequestAsyncTask(request);
        task.execute();
    }

}

1 个答案:

答案 0 :(得分:2)

我假设您关注了facebook开发者教程。如果是这样,您可以使用此命令生成密钥

keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64

您还必须使用发布密钥库创建密钥并将其注册到Facebook。