如何从Facebook sdk 4.0获取电子邮件 [英] how to get email from Facebook sdk 4.0

查看:145
本文介绍了如何从Facebook sdk 4.0获取电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Android上登录Facebook登录后我想从Facebook保存数据我想从Facebook保存以下数据

I'm working on android Login with Facebook After login i want to save the data from Facebook i want to save the following data from Facebook


  1. 名字

  2. 中间名

  3. 姓氏

  4. 用户ID

  5. 个人资料图片

  6. 电子邮件

  7. 个人资料名称

  1. First Name
  2. Middle Name
  3. Last Name
  4. User id
  5. profile picture
  6. Email
  7. Profile Name

我尝试了一些以下代码:

I tried some code which following

loginButton = (LoginButton)findViewById(R.id.login_button);

        loginButton = (LoginButton) findViewById(R.id.login_button);

        loginButton.setReadPermissions(Arrays.asList("public_profile", "email", "user_friends"));

        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                AccessToken accessToken = loginResult.getAccessToken();
                Profile profile = Profile.getCurrentProfile();

                String userid = profile.getId();
                String userfirstname = profile.getFirstName();
                String middlename = profile.getMiddleName();
                String userlastname = profile.getLastName();
                Uri userimage = profile.getProfilePictureUri(30, 40);
                String name = profile.getName();
            }

            @Override
            public void onCancel() {
                //info = ("Login attempt canceled.");
            }

            @Override
            public void onError(FacebookException e) {
                // info = ("Login attempt failed.");
            }
        });

从上面的代码我成功获得了其他所有但不能收到电子邮件。

From above code i successfully get other all but can't get email.

我想要


  1. 我想要获取用户电子邮件

  2. 我想为 HTTP CALL 创建一个新的json数组来保存用户帐户的上述数据。

  1. I want to get user email
  2. I want to create a new json array for HTTP CALL to save the above data from user account.


推荐答案

尝试这对我来说

 login.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {

            if (AccessToken.getCurrentAccessToken() != null) {
                RequestData();
            }
        }

        @Override
        public void onCancel() {

        }

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




private void RequestData() {

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

            final JSONObject json = response.getJSONObject();



            try {
                if(json != null){
                    text = "<b>Name :</b> "+json.getString("name")+"<br><br><b>Email :</b> "+json.getString("email")+"<br><br><b>Profile link :</b> "+json.getString("link");
                    /*details_txt.setText(Html.fromHtml(text));
                    profile.setProfileId(json.getString("id"));*/

                    Log.e(TAG, json.getString("name"));
                    Log.e(TAG, json.getString("email"));
                    Log.e(TAG, json.getString("id"));
                    //web.loadData(text, "text/html", "UTF-8");

                }







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


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

//从ID获取个人资料图片

//Get Profile Picture from id

 public static Bitmap getFacebookProfilePicture(String userID){
    try {
        URL imageURL = new URL("https://graph.facebook.com/" + userID + "/picture?type=large");
        Log.e(TAG,imageURL.toString());
        try {
            bitmap = BitmapFactory.decodeStream(imageURL.openConnection().getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

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

    return bitmap;
}

这篇关于如何从Facebook sdk 4.0获取电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆