facebook错误:OAuthException,errorMessage:在android中验证访问令牌时出错

时间:2013-07-25 07:13:31

标签: android facebook facebook-android-sdk

我正在我的Android应用程序上进行facebook集成。当我发布墙贴时,facebook会给我这个错误:

{Response:  responseCode: 400, graphObject: null, error: {HttpStatus: 400, errorCode: 190, errorType: OAuthException, errorMessage: Error validating access token: User 100002309500077 has not authorized application 390611174384274.}, isFromCache:false}

解决此问题的步骤:

  1. 在新鲜的应用程序中facebook工作正常。 (我确实在Facebook上登录并允许发布,墙上的内容将在Facebook上成功发布)。
  2. 但在此之后我打开我的Facebook帐户并从我的帐户中删除该Facebook应用程序。

    现在我再次点击我的Android应用程序的贴墙按钮。此时facebbok没有要求我的许可,继续使用墙贴的代码,并在上面发布了错误。

    现在,如果我再次点击Facebook按钮上的分享,那么它会给我一个例外

     java.lang.UnsupportedOperationException: Session: an attempt was made to open an already opened session.
    at com.facebook.Session.open(Session.java:947)
    at com.facebook.Session.openForRead(Session.java:385)
    at com.quytech.androidclient.MainWindow.onClickFacebookRequest(MainWindow.java:2605)
    at com.quytech.androidclient.MainWindow.onOptionsItemSelected(MainWindow.java:291)
    at com.actionbarsherlock.app.SherlockActivity.onMenuItemSelected(SherlockActivity.java:197)
    at com.actionbarsherlock.ActionBarSherlock.callbackOptionsItemSelected(ActionBarSherlock.java:600)
    at com.actionbarsherlock.internal.ActionBarSherlockCompat.onMenuItemSelected(ActionBarSherlockCompat.java:533)
    at com.actionbarsherlock.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:738)
    at com.actionbarsherlock.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:148)
    at com.actionbarsherlock.internal.ActionBarSherlockCompat.onMenuItemClick(ActionBarSherlockCompat.java:607)
    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:154)
    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:885)
    at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:545)
    at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
    at android.view.View$PerformClick.run(View.java:9293)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:150)
    at android.app.ActivityThread.main(ActivityThread.java:4310)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    at dalvik.system.NativeStart.main(Native Method)
    

    我正在使用的代码:

        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);
        }
    

    任何人都可以告诉我如何处理这种情况。提前谢谢。

1 个答案:

答案 0 :(得分:0)

在您的代码中,您要求会话进行阅读

session.openForRead(new Session.OpenRequest(this).setCallback(callback));

并传递写入权限,这就是发生异常的原因。