其他分享
首页 > 其他分享> > android – 如何从Facebook sdk 4.0获取电子邮件

android – 如何从Facebook sdk 4.0获取电子邮件

作者:互联网

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

>名字
>中间名
>姓氏
>用户ID
>个人资料图片
>电子邮件
>个人资料名称

我尝试了以下代码

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 one rror(FacebookException e) {
                // info = ("Login attempt failed.");
            }
        });

从上面的代码我成功获得其他所有但无法收到电子邮件.

我想要的是

>我想收到用户的电子邮件
>我想为HTTP CALL创建一个新的json数组,以便从用户帐户保存上述数据.

解决方法:

试试这对我有用

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

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

        @Override
        public void onCancel() {

        }

        @Override
        public void one rror(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;
}

标签:facebook-sdk-4-0,android,android-studio,facebook
来源: https://codeday.me/bug/20191003/1849774.html