[英]How to get user email and birthday from Facebook API Version v2.4
[英]In Android, Facebook API V2.4 is not returning email id whereas V2.3 is returning. How to get email id in V2.4?
當我為API V2.3編寫以下代碼時,這為我提供了所有詳細信息,包括電子郵件ID。 現在,相同的代碼沒有給我發送電子郵件ID。 我該怎么辦才能獲得電子郵件ID?
oncreate(..)
{
.
.
EMAIL_PERMISSION = new ArrayList<String>();
EMAIL_PERMISSION.add("email");
uiLifecycleHelper = new UiLifecycleHelper(this, statusCallback);
uiLifecycleHelper.onCreate(savedInstanceState);
Session.openActiveSession(this, true, EMAIL_PERMISSION,
statusCallback);
// callback when session changes state
Session.StatusCallback statusCallback = new StatusCallback()
{
@Override
public void call(Session session, SessionState state, Exception
exception)
{
// Checking whether the session is opened or not
if (state.isOpened())
{
} else
{
if (state.isClosed())
{
}
Log.d(TAG, state.toString());
}
}
};
// Method to get user facebook profile
void getUserFacebookProfile(Session session, final boolean finish)
{
// Checking whether the session is opened or not
if (session.isOpened())
{
// Sending request to the facebook to get user facebook profile
Request.newMeRequest(session, new GraphUserCallback()
{
@Override
public void onCompleted(GraphUser user, Response response)
{
if (user != null)
{
// To get network user id
String networkUserid = user.getId();
// To get user first name
String fname = user.getFirstName();
// To get user last name
String lname = user.getLastName();
// To get user middle name
String mname = user.getMiddleName();
// String email = user.getProperty("email").toString();
String email = response.getGraphObject().getProperty("email")
.toString();
}
現在,上面的代碼為我提供了所有詳細信息,包括V2.3的電子郵件ID,現在我無法獲取電子郵件ID。 請讓我知道解決方案。 謝謝。
public class LoginFacebook {
CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
public void openFB() {
LoginManager.getInstance().logInWithReadPermissions(activity,
Arrays.asList("read_stream", "user_photos", "email", "user_location"));
// Login Callback registration
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(final LoginResult loginResult) {
new GraphRequest(AccessToken.getCurrentAccessToken(),
"/me", null, HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(
GraphResponse response) {
/* handle the result */
try {
//GET USER INFORMATION
JSONObject json = response.getJSONObject();
String email = json.getString("email");
String fullName = json.getString("name");
String accessToken = loginResult.getAccessToken().getToken();
int type = 1;
String lastUpdate = json.getString("updated_time");
String user_id = json.getString("id");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).executeAsync();
GetSnsPost getSnsPost = GetSnsPost.getInstance(activity);
getSnsPost.getFacebookPosts();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException exception) {
}
});
}
public void loginFacebook(View v){
openFB();
}
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
由於會話很久以前已被棄用,因此我不再使用它。 我通過這種方式獲取用戶信息。 希望這段代碼能解決您的問題;)
Bundle參數= new Bundle(); params.putString(“ fields”,“ id,name,email,birthday,first_name,last_name”);
新的GraphRequest(AccessToken.getCurrentAccessToken(),AccessToken.getCurrentAccessToken()。getUserId(),params,HttpMethod.GET,新的GraphRequest.Callback(){
@Override
public void onCompleted(
GraphResponse response) {
System.out.println("\n J S O N :"
+ response.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).executeAsync();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.