调用Facebook Graph API的newMeRequest提示需Page Access Token如何解决
错误原因
你遇到的210报错是字段使用错误导致的:
profile_pic是Facebook公共主页(Page)节点的专属字段,在用户(User)节点的查询中携带该字段时,API会误判你需要查询主页相关数据,因此要求提交Page Access Token,触发权限报错。
解决方法
获取普通用户头像请使用picture字段,该字段仅需要你已经申请的public_profile权限即可调用,无需额外权限,也不需要页面访问令牌。
如果需要指定头像尺寸,可以在字段后追加参数:
picture.type(large):调用预设的大尺寸头像picture.width(400).height(400):自定义头像的宽高值
修改后的代码示例
String userId = Profile.getCurrentProfile().getId(); Bundle parameters = new Bundle(); // 将原profile_pic替换为picture,可根据需求指定尺寸 parameters.putString("fields", "email,name,picture.type(large),id"); GraphRequest request = GraphRequest.newMeRequest( AccessToken.getCurrentAccessToken(), (jsonObj, response) -> { if (response.getError() != null) { Log.e("User err", response.getError().toString()); } else { Log.e("User Json :", response.getJSONObject().toString()); UserEntity userEntity; try { userEntity = new UserEntity(jsonObj.getString("email"), jsonObj.getString("id")); userEntity.setUsername(jsonObj.getString("name")); // picture字段为嵌套结构,需要调整解析逻辑 String avatarUrl = jsonObj.getJSONObject("picture").getJSONObject("data").getString("url"); userEntity.setUserPhotoUri(avatarUrl); loggedUser.setValue(userEntity); insertUserEntity(userEntity); } catch (JSONException e) { e.printStackTrace(); } } } ); request.setParameters(parameters); request.executeAsync();
内容的提问来源于stack exchange,提问作者Omar Shawky
相关产品推荐
相关产品推荐

