2016-08-30 65 views
1

试图让我的Android应用程序获取已登录的Facebook用户的电子邮件,生日和地点。前两个字段可以成功检索,但Graph API的响应中缺少位置,我找不到原因。GraphRequest(Android Facebook SDK)缺少位置数据

我登录这些权限:

LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this, Arrays.asList("public_profile", "user_friends", "email", "user_birthday", "user_location")); 

的GraphRequest执行如下:

GraphRequest request = GraphRequest.newMeRequest(
    loginResult.getAccessToken(), 
    new GraphRequest.GraphJSONObjectCallback() { 
     @Override 
     public void onCompleted(JSONObject object, GraphResponse response) { 

      try { 
       Log.d("jsonTest171", object.toString(4)); 

       String email = object.getString("email"); 
       UserData.emailAddress = email; 

       String birthday = object.getString("birthday"); 
       UserData.birthday = birthday; 

       String location = object.getJSONObject("location").getString("name"); 
       UserData.location = location; 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
Bundle parameters = new Bundle(); 
parameters.putString("fields", "id,email,location,birthday"); 
request.setParameters(parameters); 
request.executeAsync(); 

Facebook的用户应用程序设置为允许所有3个必填字段,as shown here

这是包括在上面的代码从调试线输出的logcat:

{ 
    "id": "101936745620608", 
    "email": "[email protected]", 
    "birthday": "12\/20\/1983" 
} 

为什么是所请求的位置字段在图形响应不存在?

+0

您是否验证过用户实际上授予了pe应用程序的空闲? (调试令牌,或者调用'/ me/permissions'来验证。) – CBroe

+0

是的,用户已经允许所有请求的权限。我用/ me/permissions /进行了双重检查,并将user_location设置为授予。 – SVM

+1

你可以在Graph API Explorer(使用你的应用程序ID)中尝试请求,https://developers.facebook.com/tools/explorer-那里有什么结果?它是否显示任何调试消息? – CBroe

回答

3

location代替要求在包参数,如location{location}

parameters.putString("fields", "id,email,birthday,location{location}"); 

响应的位置的部分看起来像:

"location":{ 
    "location":{ 
    "city":"", 
    "country":"", 
    "latitude":0, 
    "longitude":0 
    }, 
    "id":"" 
} 

所以,你可以像你的回调方法检索它们:

JSONObject location = object.getJSONObject("location").getJSONObject("location"); 
String city = location.getString("city"); 
String country = location.getString("country");