[英]Facebook Graph request Android name from user ID
我正在尝试从其用户ID获取用户的名称。 在这个链接上,他们说我们可以通过一个简单的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 }}
因此,似乎我没有正确使用图形路径。 我仍在寻找文档和Google,如果我找到答案,我会给出代码!
确保您具有读取权限登录。 如果您已登录,则可以尝试:
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();
如果您在登录时遇到问题,也许此链接可以为您提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.