【问题标题】:how to get email from Facebook sdk 4.0如何从 Facebook sdk 4.0 获取电子邮件
【发布时间】:2016-05-12 09:48:18
【问题描述】:

我正在使用 android 登录 Facebook 登录后我想从 Facebook 保存数据我想从 Facebook 保存以下数据

  1. 名字
  2. 中间名
  3. 姓氏
  4. 用户名
  5. 头像
  6. 电子邮件
  7. 个人资料名称

我尝试了以下代码

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.");
            }
        });

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

我想要什么

  1. 我想获取用户电子邮件
  2. 我想为 HTTP CALL 创建一个新的 json 数组来保存用户帐户中的上述数据。

【问题讨论】:

  • 我认为您的问题与内容无关

标签: android facebook android-studio facebook-sdk-4.0


【解决方案1】:

试试这个对我有用

 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获取头像

 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;
}

【讨论】:

  • 我不能用这个我是android初学者请编辑我的代码
  • 阅读 facebook 文档中的“声明性字段”,这正是他在代码中添加的内容。确保您了解正在发生的事情,并且您可以轻松地编辑您的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多