Facebook Android SDK 4.5.0 获取邮箱地址
Facebook Android SDK 4.5.0 get email address
我目前正在创建一个测试应用程序来测试使用最新的 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.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 响应 returns 只有我帐户的 ID 和全名,但不包括电子邮件。我错过了什么吗?
不是获取 graphResponse
的值,而是使用 JSONObject me
访问电子邮件
userEmail = jsonObject.getString("email");
您需要向 facebook 请求参数才能获取您的数据。在这里,我 post 我获取 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;
}
希望对你有帮助
/*** setupFacebook stuff like make login with facebook and get the userId,Name and Email*/
private void setupFacebookStuff() {
Log.e(TAG, "key hash= " + Utils.getKeyHash(SplashActivity.this, getPackageName()));
// This should normally be on your application class
FacebookSdk.sdkInitialize(SplashActivity.this);
accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
currentAccessToken.getToken();
}
};
loginManager = LoginManager.getInstance();
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
preference.setUserLogin(true);
final GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
Log.e("id", "" + object);
if (object.has(getString(R.string.fbParamId))) {
final String userId = object.optString(getString(R.string.fbParamId));
final String userPicture = "https://graph.facebook.com/" + object.optString(getString(R.string.fbParamId)) + "/picture?type=large";
preference.setUserId(userId);
preference.setUserPictureUrl(userPicture);
}
if (object.has(getString(R.string.fbParamUserName))) {
final String userName = object.optString(getString(R.string.fbParamUserName));
preference.setUserName(userName);
}
if (object.has(getString(R.string.fbParamEmail))) {
final String userEmail = object.optString(getString(R.string.fbParamEmail));
preference.setUserName(userEmail);
Log.e("useremail", userEmail);
}
callMainActivity(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
final Bundle parameters = new Bundle();
parameters.putString("fields", "name,email,id");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
Toast.makeText(getBaseContext(), "Login Cancelled", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(FacebookException error) {
Toast.makeText(getBaseContext(), "Problem connecting to Facebook", Toast.LENGTH_SHORT).show();
Log.e(TAG, "Facebook login error " + error);
}
});
}
您还可以使用 GraphResponse 对象来获取值
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>()
{
@Override
public void onSuccess(LoginResult loginResult)
{
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.v("LoginActivity", response.toString());
try {
// Application code
String email = response.getJSONObject().getString("email");
txtStatus.setText("Login Success \n" + email);
}catch(Exception e){
e.printStackTrace();;
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender,birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel()
{
txtStatus.setText("==============Login Cancelled=============");
}
@Override
public void onError(FacebookException exception)
{
txtStatus.setText("==============Login Error=================");
exception.printStackTrace();
}
});
输入这段代码:
btnLoginFb.setReadPermissions(Arrays.asList("email"));
之前
RegisterCallback
应该可以解决问题。
我想获得电子邮件权限会有所帮助..
LoginManager.getInstance().logInWithReadPermissions(
fragmentOrActivity,
Arrays.asList("email"));
Information
如果您授予任何获取电子邮件地址的权限。
那么,Facebook 不可能每次都给你电子邮件地址或其他一些详细信息。
当 Facebook 用户未使用他的电子邮件地址注册时,Facebook 无法向您提供他的电子邮件地址。
如果用户使用电子邮件地址登录 Facebook,那么 Facebook 将向您提供电子邮件。
Facebook SDK 仅提供 public 详细信息。
使用电子邮件地址和 phone 号码检查 Facebook 登录。
我目前正在创建一个测试应用程序来测试使用最新的 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.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 响应 returns 只有我帐户的 ID 和全名,但不包括电子邮件。我错过了什么吗?
不是获取 graphResponse
的值,而是使用 JSONObject me
userEmail = jsonObject.getString("email");
您需要向 facebook 请求参数才能获取您的数据。在这里,我 post 我获取 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;
}
希望对你有帮助
/*** setupFacebook stuff like make login with facebook and get the userId,Name and Email*/
private void setupFacebookStuff() {
Log.e(TAG, "key hash= " + Utils.getKeyHash(SplashActivity.this, getPackageName()));
// This should normally be on your application class
FacebookSdk.sdkInitialize(SplashActivity.this);
accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
currentAccessToken.getToken();
}
};
loginManager = LoginManager.getInstance();
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
preference.setUserLogin(true);
final GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
Log.e("id", "" + object);
if (object.has(getString(R.string.fbParamId))) {
final String userId = object.optString(getString(R.string.fbParamId));
final String userPicture = "https://graph.facebook.com/" + object.optString(getString(R.string.fbParamId)) + "/picture?type=large";
preference.setUserId(userId);
preference.setUserPictureUrl(userPicture);
}
if (object.has(getString(R.string.fbParamUserName))) {
final String userName = object.optString(getString(R.string.fbParamUserName));
preference.setUserName(userName);
}
if (object.has(getString(R.string.fbParamEmail))) {
final String userEmail = object.optString(getString(R.string.fbParamEmail));
preference.setUserName(userEmail);
Log.e("useremail", userEmail);
}
callMainActivity(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
final Bundle parameters = new Bundle();
parameters.putString("fields", "name,email,id");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
Toast.makeText(getBaseContext(), "Login Cancelled", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(FacebookException error) {
Toast.makeText(getBaseContext(), "Problem connecting to Facebook", Toast.LENGTH_SHORT).show();
Log.e(TAG, "Facebook login error " + error);
}
});
}
您还可以使用 GraphResponse 对象来获取值
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>()
{
@Override
public void onSuccess(LoginResult loginResult)
{
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.v("LoginActivity", response.toString());
try {
// Application code
String email = response.getJSONObject().getString("email");
txtStatus.setText("Login Success \n" + email);
}catch(Exception e){
e.printStackTrace();;
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender,birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel()
{
txtStatus.setText("==============Login Cancelled=============");
}
@Override
public void onError(FacebookException exception)
{
txtStatus.setText("==============Login Error=================");
exception.printStackTrace();
}
});
输入这段代码:
btnLoginFb.setReadPermissions(Arrays.asList("email"));
之前
RegisterCallback
应该可以解决问题。
我想获得电子邮件权限会有所帮助..
LoginManager.getInstance().logInWithReadPermissions(
fragmentOrActivity,
Arrays.asList("email"));
Information
如果您授予任何获取电子邮件地址的权限。 那么,Facebook 不可能每次都给你电子邮件地址或其他一些详细信息。
当 Facebook 用户未使用他的电子邮件地址注册时,Facebook 无法向您提供他的电子邮件地址。
如果用户使用电子邮件地址登录 Facebook,那么 Facebook 将向您提供电子邮件。
Facebook SDK 仅提供 public 详细信息。
使用电子邮件地址和 phone 号码检查 Facebook 登录。