gpt4 book ai didi

java - 如何使用 Facebook SDK android 获取 Facebook 照片、全名、性别

转载 作者:IT老高 更新时间:2023-10-28 20:31:30 28 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,其中任何使用我们的应用程序登录到 Facebook 的 Android 用户,我需要从 Facebook 中提取他的照片、他的性别和他的全名。我正在为此使用 Facebook SDK。

在 Facebook SDK 的帮助下,我可以登录 Facebook,但我不知道如何从 facebook 中提取他的照片、性别和全名?

以下是我用来登录 Facebook 的代码。我关注了这个 tutorial

public class SessionLoginFragment extends Fragment {

private static final String URL_PREFIX_FRIENDS = "https://graph.facebook.com/me/friends?access_token=";

private TextView textInstructionsOrLink;
private Button buttonLoginLogout;
private Session.StatusCallback statusCallback = new SessionStatusCallback();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);

buttonLoginLogout = (Button) view.findViewById(R.id.buttonLoginLogout);
textInstructionsOrLink = (TextView) view.findViewById(R.id.instructionsOrLink);

Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);

Session session = Session.getActiveSession();
if (session == null) {
if (savedInstanceState != null) {
session = Session.restoreSession(getActivity(), null, statusCallback,
savedInstanceState);
}
if (session == null) {
session = new Session(getActivity());
}
Session.setActiveSession(session);
if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
}
}

updateView();

return view;
}

@Override
public void onStart() {
super.onStart();
Session.getActiveSession().addCallback(statusCallback);
}

@Override
public void onStop() {
super.onStop();
Session.getActiveSession().removeCallback(statusCallback);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(getActivity(), requestCode, resultCode, data);
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Session session = Session.getActiveSession();
Session.saveSession(session, outState);
}

private void updateView() {
Session session = Session.getActiveSession();
if (session.isOpened()) {
Log.d("Hello", URL_PREFIX_FRIENDS + session.getAccessToken());
Intent i = new Intent(getActivity(), ThesisProjectAndroid.class);
startActivity(i);

} else {
Log.d("Hello", "Login Failed");
textInstructionsOrLink.setText(R.string.instructions);
buttonLoginLogout.setText(R.string.login);
buttonLoginLogout.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
onClickLogin();
}
});
}
}

private void onClickLogin() {
Session session = Session.getActiveSession();
if (!session.isOpened() && !session.isClosed()) {
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
} else {
Session.openActiveSession(getActivity(), this, true, statusCallback);
}
}

private void onClickLogout() {
Session session = Session.getActiveSession();
if (!session.isClosed()) {
session.closeAndClearTokenInformation();
}
}

private class SessionStatusCallback implements Session.StatusCallback {
@Override
public void call(Session session, SessionState state, Exception exception) {
updateView();
}
}
}

谁能告诉我在上面的类中我需要在哪里进行更改以获得我需要的所有三个信息。据我所知,如果我能够获得该人的 facebook 唯一 ID,我可以获得我猜想的所有信息。有什么想法吗?

最佳答案

使用新的 api 和您的 facebook 自定义按钮,您可以使用以下代码:

把gradle下面放到你的gradle文件中:

 compile 'com.facebook.android:facebook-android-sdk:4.20.0'

Newer sdk does not need initializaion .

private CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(LoginActivity.this);//Is now depricated
setContentView(R.layout.activity_login);
callbackManager = CallbackManager.Factory.create();
}

onActivityResult:

        @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}

按钮点击:

   @Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.btn_f_sign_in_login:
LoginManager.getInstance().logInWithReadPermissions(
this,
Arrays.asList("user_friends", "email", "public_profile"));

LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
setFacebookData(loginResult);
}

@Override
public void onCancel() {
}

@Override
public void onError(FacebookException exception) {
}
});
break;
}
}

设置Facebook数据:

     private void setFacebookData(final LoginResult loginResult)
{
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
// Application code
try {
Log.i("Response",response.toString());

String email = response.getJSONObject().getString("email");
String firstName = response.getJSONObject().getString("first_name");
String lastName = response.getJSONObject().getString("last_name");
String gender = response.getJSONObject().getString("gender");



Profile profile = Profile.getCurrentProfile();
String id = profile.getId();
String link = profile.getLinkUri().toString();
Log.i("Link",link);
if (Profile.getCurrentProfile()!=null)
{
Log.i("Login", "ProfilePic" + Profile.getCurrentProfile().getProfilePictureUri(200, 200));
}

Log.i("Login" + "Email", email);
Log.i("Login"+ "FirstName", firstName);
Log.i("Login" + "LastName", lastName);
Log.i("Login" + "Gender", gender);


} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,email,first_name,last_name,gender");
request.setParameters(parameters);
request.executeAsync();
}

获取下载您应用的 Facebook 好友:

替换parameters.putString("fields", "id,email,first_name,last_name");

with parameters.putString("fields", "id,email,first_name,last_name,friends");

添加以下逻辑获取好友数据

                if (object.has("friends")) {
JSONObject friend = object.getJSONObject("friends");
JSONArray data = friend.getJSONArray("data");
for (int i=0;i<data.length();i++){
Log.i("idddd",data.getJSONObject(i).getString("id"));
}
}

关于java - 如何使用 Facebook SDK android 获取 Facebook 照片、全名、性别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14542193/

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