简体   繁体   English

使用facebook图表api发布时出现OAuthException 2500错误

[英]Error OAuthException 2500 while posting with facebook graph api

I'm writting app integrated with facebook. 我正在写与facebook集成的应用程序。 I want to post to wall without post dialog. 我想在没有帖子对话的情况下发布到墙上。 I try to use code from this answer , but I got an error 我尝试使用这个答案中的代码 ,但是我收到了一个错误

{"error":
    {"message":"An active access token must be used to query information about the current user.",
     "type":"OAuthException",
     "code":2500
    }
}

I login user with this code 我使用此代码登录用户

    public void authorize() {
    mFacebook.authorize(mActivity, new String[] { "publish_stream" }, new DialogListener() {
        @Override
        public void onComplete(Bundle values) {
            SharedPreferences.Editor editor = mPrefs.edit();
            editor.putString("access_token", mFacebook.getAccessToken());
            editor.putLong("access_expires", mFacebook.getAccessExpires());
            editor.commit();

            mLoginStateView.setImageResource(mAuthorizedDrawableRes);
        }

        @Override
        public void onFacebookError(FacebookError error) {}

        @Override
        public void onError(DialogError e) {}

        @Override
        public void onCancel() {}
    });
}

Please, explain me what am I doing wrong? 请解释一下我做错了什么?

[ADDED] [添加]

If I try to post with mFacebook.dialog(currentActivity, "stream.publish", params, new UpdateStatusListener()); 如果我尝试使用mFacebook.dialog(currentActivity, "stream.publish", params, new UpdateStatusListener()); it works. 有用。 Please, help me! 请帮我!

A successful authorization would return an access token to your app, which you can then use to perform actions to the Facebook API. 成功的授权将向您的应用返回访问令牌,然后您可以使用该令牌对Facebook API执行操作。 The error message displayed means you do not have a valid access token which means you probably did not authenticate the app correctly. 显示的错误消息表示您没有有效的访问令牌,这意味着您可能没有正确验证应用程序。 I would put some logging on the onFacebookError and onError methods to see what the problem is. 我会在onFacebookErroronError方法上进行一些日志记录,以查看问题所在。

Oh, I've done it. 哦,我做到了。 But it looks like a bug or magic... LogCat after authorize method (Facebook's log is enabled) 但它看起来像一个bug或魔法...授权方法后的LogCat(Facebook的日志已启用)

Facebook-authorize: Login Success! access_token=AAAHL4f6XMS0BAHNJKpoGAUAeGDlynRt1s5XPdBPRWGfILGOTZB4OSEGi4HBPLZBXDWqK2RIO8disDtzxHBYSvL3bZAnHkU5hAVK9oqWhAZDZD expires=1356040438476

But then I try post with 但后来我尝试发帖

String response = mFacebook.request("me/feed", parameters, "POST");

Function request from Facebook.java : 来自Facebook.java的功能请求:

public String request(String graphPath, Bundle params, String httpMethod)
        throws FileNotFoundException, MalformedURLException, IOException {
params.putString("format", "json");
if (isSessionValid()) {
    params.putString(TOKEN, getAccessToken());
}
String url = (graphPath != null) ? GRAPH_BASE_URL + graphPath : RESTSERVER_URL;
return Util.openUrl(url, httpMethod, params);
}

I see in debugger, that Facebook's function isSessionValid() returns false and mAccessToken = null . 我在调试器中看到,Facebook的函数isSessionValid()返回falsemAccessToken = null I don't know why it is so, and why it returns true sometimes afterwards (yes, I understand that it is impossible). 我不知道为什么会这样,为什么有时它会返回true (是的,我明白这是不可能的)。 Explaine me, please, if you know. 如果你知道,请解释我。

This code solved my problem (it just re-sets access token) 这段代码解决了我的问题(它只是重新设置了访问令牌)

if (!mFacebook.isSessionValid()){
    String token = mPrefs.getString("access_token", null);
    long expires = mPrefs.getLong("access_expires", 0);
    mFacebook.setAccessToken(token);
    mFacebook.setAccessExpires(expires);
}
Bundle parameters = new Bundle();
parameters.putString("message", msg);
parameters.putString("description", "test test test");
String response = mFacebook.request("me/feed", parameters, "POST");
if (response == null || response.equals("") || response.equals("false")) {
    Log.e(Const.LOG_TAG, "Blank response");
} else {
    Log.d(Const.LOG_TAG, response);
}

I also had the same problem while retrieving albums from facebook. 从facebook上检索专辑时我也遇到了同样的问题。 I tested my access_token via facebook debugger and it was valid. 我通过facebook调试器测试了我的access_token,它是有效的。 Then I found the error, the final url in com.facebook.android.Util.openUrl(...) was: 然后我发现错误, com.facebook.android.Util.openUrl(...)的最终网址是:
Invalid-URL1: 无效-URL1:
https:// graph.facebook.com//me/albums?fields=id,name,cover_photo,type,count?access_token={...token..}&format=json&metadata=1 https:// graph.facebook.com//me/albums?fields=id,name,cover_photo,type,count?access_token={...token..}&format=json&metadata=1
Where it should be: 它应该在哪里:
Valid - URL2: 有效 - URL2:
https:// graph.facebook.com//me/albums?fields=id,name,cover_photo,type,count&access_token={...token..}&format=json&metadata=1 https:// graph.facebook.com//me/albums?fields=id,name,cover_photo,type,count&access_token={...token..}&format=json&metadata=1

Note that in URL1 ? 请注意,在URL1? is added two times which is not valid so I changed the code as follows 添加两次无效,所以我更改了代码如下
Code Changes: Previous 代码更改: 上一个

com.facebook.android.Util.openUrl(...)

    if (method.equals("GET")) {
        url = url + "?" + encodeUrl(params);
    }

Now 现在

com.facebook.android.Util.openUrl(...)

    if (method.equals("GET")) {
        if(url.contains ( "?" )==false)
        {
            url = url + "?" + encodeUrl(params);
        }
        else
        {
            url = url + "&" + encodeUrl(params);
        }
    }

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

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