Getting a Malformed access token "t ​ype":"OAuthException","code":190

19,457

If you look at the Errors page in the documentation you will see that when you get error 190 you should authorise/reauthorise the user.

I suspect that this happened to you because you first logged in, then added the permissions to access the albums to your application BUT did not log out and log back in. Hence, you need to obtain a new access token which will grant the new permissions to your application.

Share:
19,457
namrathu1
Author by

namrathu1

Updated on June 26, 2022

Comments

  • namrathu1
    namrathu1 almost 2 years

    I am writing an android application to get the Facebook user albums and photos and display in my Android application. I have created a Facebook App with APP_ID 281846961912565.

    While creating the Facebook instance, I am passing this id as follows

    facebook = new Facebook(APP_ID);
    

    Using this instance, I am able to login to my FB account post on messages on facebook wall programatically. After logging in, I get an access_token.

    I'm using the access token to get the album ids using facebook.request("https://graph.facebook.com/me/albums?access_token="+facebook.getAccessToken());

    Now I get {"error":{"message":"Malformed access token ACCESSTOKENACCESSTOKEN?access_token=ACCESSTOKENACCESSTOKEN","t‌​ype":"OAuthException","code":190}}

    Can any of you please help me resolve this issue and point out what i am doing wrong.

    My code is as follows:

        private static final String[] PERMISSIONS = new String[] { "publish_stream","user_photos" };
    public boolean saveCredentials(Facebook facebook) {
        Editor editor = getApplicationContext().getSharedPreferences(KEY,
                Context.MODE_PRIVATE).edit();
        editor.putString(TOKEN, facebook.getAccessToken());
        editor.putLong(EXPIRES, facebook.getAccessExpires());
        return editor.commit();
    }
    
    public boolean restoreCredentials(Facebook facebook) {
        SharedPreferences sharedPreferences = getApplicationContext()
                .getSharedPreferences(KEY, Context.MODE_PRIVATE);
        facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
        facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
        return facebook.isSessionValid();
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        facebook = new Facebook(APP_ID);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.facebook_dialog);
        String facebookMessage = getIntent().getStringExtra("facebookMessage");
        if (facebookMessage == null) {
            facebookMessage = "Test wall post";
        }
        messageToPost = facebookMessage;
    }
    

    R.layout.facebook_dialog is the dialog which pops up asking if a message should be shared on facebook or not. If yes the following method is called.

        public void share(View button) {
        if (!facebook.isSessionValid()) {
            loginAndPostToWall();
        } else {
            postToWall(messageToPost);
        }
    }
       public void loginAndPostToWall() {
        facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH,
                new LoginDialogListener());
    }
       class LoginDialogListener implements DialogListener {
        public void onComplete(Bundle values) {
            saveCredentials(facebook);
            if (messageToPost != null) {
                postToWall(messageToPost);
            }
        }
    
        public void onFacebookError(FacebookError error) {
            showToast("Authentication with Facebook failed!");
            finish();
        }
    
        public void onError(DialogError error) {
            showToast("Authentication with Facebook failed!");
            finish();
        }
    
        public void onCancel() {
            showToast("Authentication with Facebook cancelled!");
            finish();
        }
    }
    
       public void postToWall(String message) {
        Bundle parameters = new Bundle();
        parameters.putString("message", message);
        parameters.putString("description", "topic share");
        try {
            facebook.request("me");
            String response = facebook.request("me/feed", parameters, "POST");
            Log.d("Tests", "got response: " + response);
            if (response == null || response.equals("")
                    || response.equals("false")) {
                showToast("Blank response.");
            } else {
                showToast("Message posted to your facebook wall!");
            }
                        getImagesFromUserAlbum();
            finish();
    
    
        } catch (Exception e) {
            showToast("Failed to post to wall!");
            e.printStackTrace();
            finish();
        }
    }
    

    Later when I do a `private void getImagesFromUserAlbum() { facebook.getAccessToken();

        JSONArray albumss = null;
        String response = null;
        try {
            response = facebook.request("me/albums");
    

    // `

    I get the error {"error":{"message":"Malformed access token ACCESSTOKEN?access_token=ACCESSTOKEN","type":"OAuthException","code":190}}

    Thanks for your help.

    The code above is now the working copy. Thanks to Bartek.