Android Facebook Oauthexception具有有效的访问令牌

时间:2012-07-16 19:46:54

标签: android facebook-graph-api

我正在尝试与Facebook沟通做简单的事情。目前我可以将用户登录并作为他们发布到他们的墙上。但无论出于何种原因,我似乎无法访问公共信息,例如他们的名字。我一直都会遇到这个错误:

{“error”:{“message”:“语法错误\”字符串的预期结束而不是\“?\”。\“在字符4处:name?access_token = MYACCESSTOKEN”,“type”:“OAuthException” ,“代码”:2500}}

这是电话:

SampleRequestListener srl = new SampleRequestListener();
AsyncFacebookRunner afr = new AsyncFacebookRunner(facebook);
afr.request("http://graph.facebook.com/me?fields=name", (RequestListener)srl);

该调用是在经过验证的会话中进行的(在DialogListener的onComplete部分中为.Authorize)。使用我的access_token和与上面完全相同的字符串,我可以在http://developers.facebook.com/tools/explorer/

处获得正常工作的请求

在RequestListener.onComplete

中解析响应时发生错误
JSONObject json = Util.parseJson(response);
final String name = json.getString("name");
System.out.println("Hi, my name is " + name);

感谢您的时间。欢迎所有的意见。

更新*

有两件事正在发生。在facebook API中,Util.openUrl附加了一个“?”在字段名称和access_token之间(如下面指出的答案)。这看起来很奇怪。我想知道我是否删除了旧版本的API或其他东西。你会认为这将被正确设置。

另外,我错误地调用了该方法: 这样:

afr.request("http://graph.facebook.com/me?fields=name", (RequestListener)srl);

应该是:

afr.request("me?fields=name", (RequestListener)srl);

2 个答案:

答案 0 :(得分:3)

如果您正在使用com.facebook.Request类,那么只需使用以下形式的构造函数:Request(会话会话,String graphPath,Bundle参数,HttpMethod httpMethod,Callback回调)并在"参数&#中传递参数34;参数。

就像:

if (GlobalApplication.accessToken != null && !GlobalApplication.accessToken.isExpired()) {
                    /* make the API call */
                    Bundle b = new Bundle();
                    b.putString("fields", "id,created_time,description,embed_html,name,source,picture");
                    new GraphRequest(GlobalApplication.accessToken, "/me/videos",
                            b, HttpMethod.GET, new GraphRequest.Callback() {
                        @Override
                        public void onCompleted(GraphResponse response) {
                            /* handle the result */
                            Log.i("", "");
                            String string = response.toString();

                            JSONObject object = response.getJSONObject();
                            JSONArray array = response.getJSONArray();
                            Log.i("", "");
                        }
                    }).executeAsync();

答案 1 :(得分:1)

看起来发送的实际请求类似于

/me?fields=name?access_token=MYACCESSTOKEN

那当然是错的;它应该是第二个参数之前的&符号而不是问号。

您必须在代码中查找添加访问令牌作为参数的位置。此时,在附加access_token参数之前,应检查此URL是否已包含问号。