Facebook Graph请求用户ID

时间:2017-06-01 12:58:51

标签: android facebook facebook-graph-api request

我试图从用户ID中获取用户名。 在this链接上,他们说我们可以通过简单的HTTP请求来执行此操作:

http://graph.facebook.com/4  

但似乎这种方法已经过时,因为我无法得到任何东西,因为:

"An access token is requir…o request this resource."

无论如何,我尝试在Android上使用他们关于图形api的文档,我这样做了:

    GraphRequestAsyncTask request1 = new GraphRequest(
        AccessToken.getCurrentAccessToken(),
        "/"+id1+"/name",
        null,
        HttpMethod.GET,

        new GraphRequest.Callback() {
            public void onCompleted(GraphResponse response) {
                /* handle the result */
            }
        }
    ).executeAsync();
    name1.setText(request1.toString());

但在name1 TextView上,我收到一条消息告诉我

{RequestAsyncTask:
    connection:null,requests:[{Request:
    accesToken:{AccessToken
        token:ACCESS_TOKEN_REMOVED permissions:
        [public_profile]},
        graphPath:/100017723671435/name, graphObject: null,
        httpMethod:Get,
        parameters:Bundle[{}]}]}

我真的不明白该怎么做,我的数据库上有一堆facebook ID,我只想从ID中获取名称,以便在屏幕上显示。

该应用的当前用户不是我想要获取信息的个人资料!

编辑:我似乎误解了/*handle the result*/评论,我这样做了这句话:

name1.setText(response.toString());

但我的TextView上仍有错误:

{Response: 
    responseCode:400,
    graphObject:null,
    error: { HttpStatus:400,
             errorCode: 2500,
             errorType: OAuthException,
             errorMessage: Unknown path components: /first_name }}

所以我似乎没有正确使用图形路径。我仍在查看文档和谷歌,如果我找到答案,我会给出代码!

1 个答案:

答案 0 :(得分:0)

确保您已使用读取权限登录。 如果您已登录,可以尝试:

GraphRequest.Callback gCallback = new GraphRequest.Callback() {
        @Override
        public void onCompleted(GraphResponse response) {
            Log.d(TAG, "onCompleted: ");
            if (response != null && response.getJSONObject() != null && response.getJSONObject().has("first_name"))
            {
                try {
                    name1.setText(response.getJSONObject().getString("first_name"));
                } catch (JSONException e) {
                    Log.e(TAG, "onCompleted: ",e );
                }
            }
        }
    };
    new GraphRequest(AccessToken.getCurrentAccessToken(),"/me?fields=id,name,gender,email,first_name,last_name", null,HttpMethod.GET, gCallback).executeAsync();

如果您在登录时遇到问题,也许this link可以为您提供帮助。