gpt4 book ai didi

android - 如何从 Facebook SDK 4.5 中的 Android 应用程序获取用户的个人资料图片、主要电子邮件、Facebook 链接?

转载 作者:太空狗 更新时间:2023-10-29 14:54:51 26 4
gpt4 key购买 nike

我在我的应用程序中使用最新的 Android Facebook SDK。

public class MainActivity extends AppCompatActivity {
public static CallbackManager callbackmanager;
private AccessTokenTracker mTokenTracker;
private ProfileTracker mProfileTracker;
Button fb_login;

private boolean fb_signincllicked = false;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());

setContentView(R.layout.activity_main);


fb_login = (Button) findViewById(R.id.fb_login_button);
fb_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onFblogin(true);
}
});
}

public void onFblogin(boolean fb_btn) {

fb_signincllicked = fb_btn;

callbackmanager = CallbackManager.Factory.create();
// setupTokenTracker();
// setupProfileTracker();
// mTokenTracker.startTracking();
// mProfileTracker.startTracking();
// Set permissions
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "email"));

LoginManager.getInstance().registerCallback(callbackmanager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// AccessToken accessToken = loginResult.getAccessToken();
// Profile profile = Profile.getCurrentProfile();
final Set<String> deniedPermissions = loginResult.getRecentlyDeniedPermissions();

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

if (response.getError() != null) {
// handle error
} else {
try {

// if (deniedPermissions.contains("email")) {
// LoginManager.getInstance().logInWithReadPermissions(Login_Activity.this, Arrays.asList("email"));
// }

JSONObject jsonObject = new JSONObject();
if (jsonObject.has("picture")) {
String profilePicUrl = jsonObject.getJSONObject("picture").getJSONObject("data").getString("url");
System.out.println("111015:profilePicUrl" + profilePicUrl);

} else {
System.out.println("111015:profilePicUrl" + "No Data");

}
jsonObject.put("full_name", json.getString("name"));
Toast.makeText(getApplicationContext(), json.get("id").toString() + json.getString("name"), Toast.LENGTH_LONG).show();
jsonObject.put("device_id", Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID));
jsonObject.put("fb_id", json.getString("id"));
jsonObject.put("signuptype", "1");

Intent i = new Intent(getBaseContext(), Profile.class);
i.putExtra("prof_name", json.getString("name"));
i.putExtra("fb_id", json.getString("id"));

startActivity(i);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}).executeAsync();
}

@Override
public void onCancel() {
Log.d("Cancel", "On cancel");
}

@Override
public void onError(FacebookException error) {
Log.d("Error", error.toString());
}
});
}


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

if (fb_signincllicked) {
callbackmanager.onActivityResult(requestCode, resultCode, data);
}


}
@Override
public void onStop() {
super.onStop();
// mTokenTracker.stopTracking();
// mProfileTracker.stopTracking();
}
// private void setupTokenTracker() {
// mTokenTracker = new AccessTokenTracker() {
// @Override
// protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
// Log.d("VIVZ", "" + currentAccessToken);
// }
// };
// }
//
// private void setupProfileTracker() {
// mProfileTracker = new ProfileTracker() {
// @Override
// protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
// Log.d("VIVZ", "" + currentProfile);
// }
// };
// }
}

完全登录成功后(成功)我想去另一个 Activity 。我想显示以下内容,

  1. 自定义图片 View 中的 Facebook 个人资料图片(不在图片中查看 Facebook 提供的)。
  2. 主邮箱
  3. 电话号码(如果用户在 Face Book 中提供)
  4. 用户的 Facebook 链接。

请帮帮我。提前致谢。

最佳答案

在您的 onActivityCreated() 上放置此代码:

 @Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
updateWithToken(AccessToken.getCurrentAccessToken());
}

然后这是我的代码,我目前正在使用它来进行 facebook 登录:

private void updateWithToken(AccessToken currentAccessToken) {

mTokenTracker.startTracking();
mProfileTracker.startTracking();

if (currentAccessToken != null) {

String accessToken = currentAccessToken
.getToken();
Log.i("accessToken", accessToken);

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

Log.i("LoginActivity",
response.toString());
try {
String id = object.getString("id");
try {
URL profile_pic = new URL(
"http://graph.facebook.com/" + id + "/picture?type=large");
Log.i("profile_pic",
profile_pic + "");

} catch (MalformedURLException e) {
e.printStackTrace();
}
if (!sessionManagerFacebook.isLoggedIn()) {
sessionManagerFacebook.createLoginSession(object.getString("email"), getResources().getConfiguration().locale.toString());
if (cameFromQuestionExtra != null && cameFromQuestionExtra.getString("cameFromQuestion").equals("yes")) {

getActivity().setResult(1);
getActivity().finish();
}
//startActivity(new Intent(getActivity(), MainActivity.class));
} else {
user = sessionManagerFacebook.getUserDetails();
startActivity(new Intent(getActivity(), MainActivity.class));
}

getActivity().finish();

} catch (JSONException e) {
e.printStackTrace();
}
}
});

Bundle parameters = new Bundle();
parameters.putString("fields",
"id,name,email,gender");
request.setParameters(parameters);
request.executeAsync();


}


}

在您的第二个 Activity 中,您只需创建新的配置文件变量:

Profile  profile = Profile.getCurrentProfile();
if (profile != null) {

Toast.makeText(MainActivity.this, profile.getName(), Toast.LENGTH_SHORT).show();
loadImages(profile.getProfilePictureUri(120, 120).toString());
profileName.setText(profile.getName());

mDrawer.getMenu().findItem(R.id.nav_myAccount).setVisible(false);
Bundle extra = getIntent().getExtras();
String email;
if (extra != null) {
email = extra.getString("email");
profileEmail.setText(email);
Toast.makeText(MainActivity.this, email, Toast.LENGTH_SHORT).show();
} else if (sessionManagerFacebook.isLoggedIn()) {
profileEmail.setText(userFacebook.get(SessionManagerFacebook.KEY_EMAIL));
Toast.makeText(MainActivity.this, userFacebook.get(SessionManagerFacebook.KEY_EMAIL), Toast.LENGTH_SHORT).show();

}

}

https://developers.facebook.com/docs/reference/android/current/class/Profile/

关于android - 如何从 Facebook SDK 4.5 中的 Android 应用程序获取用户的个人资料图片、主要电子邮件、Facebook 链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32516968/

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