gpt4 book ai didi

android - Facebook Android SDK 4.5.0 获取邮箱地址

转载 作者:IT老高 更新时间:2023-10-28 21:44:52 27 4
gpt4 key购买 nike

我目前正在创建一个测试应用程序来测试使用最新的 facebook SDK 来更新我们现有的应用程序问题是我需要获取我知道的电子邮件地址,这取决于用户是否在他的帐户中提供了一个。现在我用来测试的帐户确实提供了一个,但由于未知原因,facebook SDK 只提供了 user_id 和帐户的全名,没有别的。我对此感到困惑,因为 SDK3 及更高版本提供的信息比更新的 SDK4 更多,而且我不知道如何获取电子邮件,因为到目前为止我看到的所有答案都没有提供我的电子邮件。到目前为止,这是我的代码:

登录按钮

@OnClick(R.id.btn_login)
public void loginFacebook(){
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "email"));
}

LoginManager 回调:

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

@Override
public void onCancel() {
Toast.makeText(getBaseContext(),"Login Cancelled", Toast.LENGTH_SHORT).show();
}

@Override
public void onError(FacebookException e) {
Toast.makeText(getBaseContext(),"Problem connecting to Facebook", Toast.LENGTH_SHORT).show();
}
});

以及对用户个人资料的请求:

public void requestUserProfile(LoginResult loginResult){
GraphRequest.newMeRequest(
loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject me, GraphResponse response) {
if (response.getError() != null) {
// handle error
} else {
try {
String email = response.getJSONObject().get("email").toString();
Log.e("Result", email);
} catch (JSONException e) {
e.printStackTrace();
}
String id = me.optString("id");
// send email and id to your web server
Log.e("Result1", response.getRawResponse());
Log.e("Result", me.toString());
}
}
}).executeAsync();
}

JSON 响应仅返回我的帐户的 ID 和全名,但不包括电子邮件。我错过了什么吗?

最佳答案

您需要向 facebook 询问参数才能获取您的数据。在这里,我发布了获取 Facebook 数据的函数。关键在这一行:

parameters.putString("fields", "id, first_name, last_name, email,gender, birthday, location"); // Parámetros que pedimos a facebook

希望对你有帮助。

btnLoginFb.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {

@Override
public void onSuccess(LoginResult loginResult) {

System.out.println("onSuccess");
progressDialog = new ProgressDialog(LoginActivity.this);
progressDialog.setMessage("Procesando datos...");
progressDialog.show();
String accessToken = loginResult.getAccessToken().getToken();
Log.i("accessToken", accessToken);

GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {

@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.i("LoginActivity", response.toString());
// Get facebook data from login
Bundle bFacebookData = getFacebookData(object);
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id, first_name, last_name, email,gender, birthday, location"); // Parámetros que pedimos a facebook
request.setParameters(parameters);
request.executeAsync();
}

@Override
public void onCancel() {
System.out.println("onCancel");
}

@Override
public void onError(FacebookException exception) {
System.out.println("onError");
Log.v("LoginActivity", exception.getCause().toString());
}
});



private Bundle getFacebookData(JSONObject object) {

try {
Bundle bundle = new Bundle();
String id = object.getString("id");

try {
URL profile_pic = new URL("https://graph.facebook.com/" + id + "/picture?width=200&height=150");
Log.i("profile_pic", profile_pic + "");
bundle.putString("profile_pic", profile_pic.toString());

} catch (MalformedURLException e) {
e.printStackTrace();
return null;
}

bundle.putString("idFacebook", id);
if (object.has("first_name"))
bundle.putString("first_name", object.getString("first_name"));
if (object.has("last_name"))
bundle.putString("last_name", object.getString("last_name"));
if (object.has("email"))
bundle.putString("email", object.getString("email"));
if (object.has("gender"))
bundle.putString("gender", object.getString("gender"));
if (object.has("birthday"))
bundle.putString("birthday", object.getString("birthday"));
if (object.has("location"))
bundle.putString("location", object.getJSONObject("location").getString("name"));

return bundle;
}
catch(JSONException e) {
Log.d(TAG,"Error parsing JSON");
}
return null;
}

关于android - Facebook Android SDK 4.5.0 获取邮箱地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32196682/

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