简体   繁体   English

Facebook登录显示Android中的错误消息

[英]Facebook login shows error message in Android

I am working on Facebook integration on my Android app. 我正在Android应用程序上进行Facebook集成。 It's working fine on some device, but on some device it's not working and give me error message: 在某些设备上可以正常工作,但是在某些设备上却无法正常工作,并显示错误消息:

在此处输入图片说明

Below is the code : 下面是代码:

 private UiLifecycleHelper uiHelper;

    private Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            onSessionStateChange(session, state, exception);
        }
    };



    private void onSessionStateChange(Session session, SessionState state, Exception exception) {
        if (   exception instanceof FacebookOperationCanceledException ||
                exception instanceof FacebookAuthorizationException) 
        {
                new AlertDialog.Builder(MainWindow.this)
                    .setTitle("cancel")
                    .setMessage("your permission has expired.")
                    .setPositiveButton("ok", null)
                    .show();

        } 

    }




private void onClickFacebookRequest() 
 {
        if (session.isOpened()) 
        {
            sendRequests();

        } else {
            StatusCallback callback = new StatusCallback() {
                public void call(Session session, SessionState state, Exception exception) {
                    if (exception != null) {
                        new AlertDialog.Builder(MainWindow.this)
                                .setTitle(R.string.login_failed_dialog_title)
                                .setMessage(exception.getMessage())
                                .setPositiveButton(R.string.ok_button, null)
                                .show();
                     session = createSession();
                    }
                }
            };
            pendingRequest = true;


            session.openForRead(new Session.OpenRequest(this).setCallback(callback));
        }
    }
 private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
 private static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization";

 private void sendRequests() 
 {

     List<String> permissions = quytechApps.getSession().getPermissions();
        if (!isSubsetOf(PERMISSIONS, permissions)) {
            pendingRequest = true;
            Session.NewPermissionsRequest newPermissionsRequest = new Session
                    .NewPermissionsRequest(this, PERMISSIONS);
            session.requestNewPublishPermissions(newPermissionsRequest);
            return;
        }

        showValidationDialog("Please Wait.posting Data on Facebook");
        Bitmap image = BitmapFactory.decodeResource(this.getResources(), R.drawable.splash_screen_final4);

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();

        Bundle postParams=new Bundle();
        postParams.putByteArray("photo",byteArray);
        postParams.putString("message", "Hi Friends I am using Twinqli Chat App.");

        Request request = new Request(Session.getActiveSession(), "me/photos", postParams, HttpMethod.POST, new Request.Callback()
        {

            @Override
            public void onCompleted(Response response) {
                // TODO Auto-generated method stub
                // showPublishResult(getString(R.string.photo_post), response.getGraphObject(), response.getError());
            if(response.getError() == null)
            {
                Log.d("GraphApiSample.java Sucesses","sucess");
                dismissValidatingDialog();
            }
            else
            {
                dismissValidatingDialog();
                session.closeAndClearTokenInformation();
                //quytechApps.getSession().
                //quytechApps.setSession(null);
//                  Log.d("GraphApiSample.java",""+response.getError().getErrorMessage());
            }

            }

        });
        request.executeAsync();
 }

 private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
        for (String string : subset) {
            if (!superset.contains(string)) {
                return false;
            }
        }
        return true;
    }

 static final String applicationId = "390611174384274";
 boolean pendingRequest;
 static final String PENDING_REQUEST_BUNDLE_KEY = "com.facebook.samples.graphapi:PendingRequest";

 private Session createSession() 
 {
        Session activeSession = Session.getActiveSession();
        if (activeSession == null || activeSession.getState().isClosed()) 
        {
            activeSession = new Session.Builder(this).setApplicationId(applicationId).build();
            Session.setActiveSession(activeSession);
        }
        return activeSession;
    }

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (qsession.onActivityResult(this, requestCode, resultCode, data) &&
                pendingRequest &&
                session.getState().isOpened()) {
            sendRequests();
        }
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        pendingRequest = savedInstanceState.getBoolean(PENDING_REQUEST_BUNDLE_KEY, pendingRequest);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putBoolean(PENDING_REQUEST_BUNDLE_KEY, pendingRequest);
    }

Can anyone tell me what's wrong in my code? 谁能告诉我我的代码有什么问题吗?

This problem normally occurs when the facebook app in your phone is not updated so please update it and then check. 当您的手机中的Facebook应用程序更新时,通常会出现此问题,因此请对其进行更新然后检查。 It worked for me hope it will work for you also. 它对我有用,希望它也对您有用。

EDIT: To check the hash key put this in your onCreate Method 编辑:要检查哈希键,将其放在您的onCreate方法中

 PackageInfo info;
try {
    info = getPackageManager().getPackageInfo("com.example.yourpackagename", PackageManager.GET_SIGNATURES);
    for (Signature signature : info.signatures) {
        MessageDigest md;
        md = MessageDigest.getInstance("SHA");
        md.update(signature.toByteArray());
        String something = new String(Base64.encode(md.digest(), 0));
        //String something = new String(Base64.encodeBytes(md.digest()));
        Log.e("hash key", something);
    }
} catch (NameNotFoundException e1) {
    Log.e("name not found", e1.toString());
} catch (NoSuchAlgorithmException e) {
    Log.e("no such an algorithm", e.toString());
} catch (Exception e) {
    Log.e("exception", e.toString());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM