java 获取格式错误的访问令牌 "t ?ype":"OAuthException","code":190

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14409685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:10:55  来源:igfitidea点击:

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

javaandroidfacebookfacebook-graph-api

提问by namrathu1

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.

我正在编写一个 android 应用程序来获取 Facebook 用户相册和照片并在我的 Android 应用程序中显示。我创建了一个带有 APP_ID 281846961912565Facebook 应用程序

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

在创建 Facebook 实例时,我按如下方式传递此 ID

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.

使用这个实例,我能够以编程方式登录到我的 FB 帐户在 facebook 墙上的消息上发布。登录后,我得到一个 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());

我正在使用访问令牌来获取专辑 ID 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}}

现在我得到 {"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.

R.layout.facebook_dialog 是弹出的对话框,询问是否应在 facebook 上共享消息。如果是,则调用以下方法。

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

后来当我做一个`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}}

我收到错误 {"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.

上面的代码现在是工作副本。感谢巴特克。

回答by Bartek

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

如果您查看文档中的错误页面,您会看到当您获得时,error 190您应该授权/重新授权用户。

I suspect that this happened to you because you first logged in, then added the permissionsto 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.

我怀疑这发生在您身上,因为您首先登录,然后访问相册的权限添加到您的应用程序但没有注销并重新登录。因此,您需要获取一个新的访问令牌,它将授予新的权限到您的应用程序。

回答by Manish Prajapati

Please check is there &expires in your access token if yes then remove it because it is not part of access_token and try after that.

请检查您的访问令牌中是否有 &expires 如果是,则将其删除,因为它不是 access_token 的一部分,然后再尝试。