如何检索Fb配置文件电子邮件

时间:2016-09-17 17:15:42

标签: java android facebook facebook-graph-api

我正在运行android studio 2.1.3, 并拥有Facebook SDK v4.15.0

我正在尝试创建一个登录页面,用户可以在其中, 注册/登录我的数据库中的帐户, 要么 使用那里的fb帐户登录(这将在我的数据库上创建/检索帐户)

我能够使用Profile轻松检索ID, 我也想要电子邮件,生日,名字,姓氏和朋友列表。

如果有人可以帮我破解电子邮件,那么它应该是直截了当的。

过去3天我一直在搜索论坛帖子和网络教程,虽然有很多信息,但我找不到任何有用的东西

package com.user1.riffraff;


import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphRequestAsyncTask;
import com.facebook.GraphResponse;
import com.facebook.Profile;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;


/**
 * A simple {@link Fragment} subclass.
 */
public class LoginFragment extends Fragment {

    LoginButton loginButton;
    CallbackManager callbackManager;

    AccessTokenTracker accessTokenTracker;
    AccessToken accessToken;

    public LoginFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        FacebookSdk.sdkInitialize(getContext());

        callbackManager = CallbackManager.Factory.create();
        View view = inflater.inflate(R.layout.fragment_login, container, false);
        loginButton = (LoginButton) view.findViewById(R.id.login_button);
        loginButton.setReadPermissions(Arrays.asList("email", "user_friends", "user_birthday"));
// If using in a fragment
        loginButton.setFragment(this);
        // Other app specific specialization

        callbackManager = CallbackManager.Factory.create();
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {

            @Override
            public void onSuccess(LoginResult loginResult) {

                requestUserProfile(loginResult);
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException e) {

            }
        });
        return view;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }


    public void requestUserProfile(LoginResult loginResult) {

        accessToken =loginResult.getAccessToken();

        GraphRequest request = GraphRequest.newMeRequest(
                accessToken,
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(
                            JSONObject object,
                            GraphResponse response) {
                        if (response.getError()!=null)
                        {
                            Log.e("error","Error in Response "+ response);
                        }
                        else
                        {
                            String setEmail=object.optString("email");
                            String setName=object.optString("name");
                            String  setId=object.optString("id");
                        }


                        Log.e("GraphResponse", "-------------" + response.toString());
                    }
                });
       // Bundle parameters = new Bundle();
       // parameters.putString("fields", "id,name,email");
       // request.setParameters(parameters);
       // request.executeAsync();


    }
    @Override
    public void onDestroy () {
        super.onDestroy();
       // accessTokenTracker.stopTracking();
    }
}

0 个答案:

没有答案