gpt4 book ai didi

java - 如何使用 facebook sdk 从 facebook 个人资料中获取 id 和姓名?

转载 作者:行者123 更新时间:2023-12-01 14:48:32 25 4
gpt4 key购买 nike

我知道这种方法是使用 JSON,并且我已经阅读了很多有关如何执行此操作的信息,但我仍然无法弄清楚必须将 JSON 代码放在哪里,以便我可以获得 id 和名称,因为我的一切尝试无效(为空)。你们能给我一个建议,告诉我应该在哪里索取身份证件和姓名,以便我能真正得到它们吗?这是我的主类(class)中与 facebook 相关的所有内容:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
this.facebookConnector.getFacebook().authorizeCallback(requestCode, resultCode, data);
}

@Override
protected void onResume() {
super.onResume();
updateLoginStatus();
}

public void updateLoginStatus() {
loginStatus.setText("Logged into Facebook as "+ facebookConnector.getUserName() + facebookConnector.getFacebook().isSessionValid());
}

public void postMessage() {

if (facebookConnector.getFacebook().isSessionValid()) {
postMessageInThread();
} else {
SessionEvents.AuthListener listener = new SessionEvents.AuthListener() {

@Override
public void onAuthSucceed() {
postMessageInThread();
}

@Override
public void onAuthFail(String error) {

}
};
SessionEvents.addAuthListener(listener);
facebookConnector.login();

}
}

private void postMessageInThread() {
Thread t = new Thread() {
public void run() {

try {
facebookConnector.postMessageOnWall("test");
mFacebookHandler.post(mUpdateFacebookNotification);
} catch (Exception ex) {
Log.e(TAG, "Error sending msg",ex);
}
}
};
t.start();
}

private void clearCredentials() {
try {
facebookConnector.getFacebook().logout(getApplicationContext());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

我上次尝试过

 AsyncFacebookRunner myAsyncRunner = new AsyncFacebookRunner(facebook);
myAsyncRunner.request("me", new meRequestListener());

然后

 public class meRequestListener implements RequestListener {

public void onComplete(String response, Object state) {

fbName = response;
}

当我得到字符串时

public String getUserName () {
return fbName;
}

它仍然是空的。请帮忙。

最佳答案

你可以使用这个:(此代码使用 Facebook SDK 3.02)

用户 ID:

final Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
// If the session is open, make an API call to get user data
// and define a new callback to handle the response
Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
// If the response is successful
if (session == Session.getActiveSession()) {
if (user != null) {
user_ID = user.getId();//user id
profileName = user.getName();//user's profile name
userNameView.setText(user.getName());
}
}
}
});
Request.executeBatchAsync(request);
}

user_IDprofileName 是字符串。

关于java - 如何使用 facebook sdk 从 facebook 个人资料中获取 id 和姓名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15134978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com