获得格式错误的访问令牌“t ype”:“OAuthException”,“代码”:190 [英] Getting a Malformed access token "t ​ype":"OAuthException","code":190

查看:162
本文介绍了获得格式错误的访问令牌“t ype”:“OAuthException”,“代码”:190的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

在创建Facebook实例时,我传递这个id,如下所示

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

facebook = new Facebook(APP_ID);

使用此实例,我可以通过编程方式登录到我的FB帐户上的消息。
登录后,我得到一个access_token。

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.

我正在使用访问令牌来获取相册ID使用 facebook.request (https://graph.facebook.com/me/albums?access_token=+ facebook.getAccessToken());

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

现在我得到 {error:{message:格式错误的访问令牌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.

我的代码如下:

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

Rlayout.facebook_dialog是弹出的对话框,询问是否应该有消息在Facebook上分享或不共享。如果是,调用以下方法。

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

稍后我做一个`private void getImagesFromUserAlbum() {
facebook.getAccessToken();

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

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

//`

错误
{error:{message:格式错误的访问令牌ACCESSTOKEN?access_token = ACCESSTOKEN,type:OAuthException,code:190}}

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

感谢您的帮助。

上面的代码现在是工作副本。感谢Bartek。

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

推荐答案

如果你看看错误页面,当您获得错误190 时,您应该授权/重新授权用户。

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.

这篇关于获得格式错误的访问令牌“t ype”:“OAuthException”,“代码”:190的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆