验证成功后添加要查看的内容
Add things to view after successfull authentication
我有一个应用程序可以让用户通过 facebook 登录。我正在使用 Facebook SDK:
public class LoginFragment extends Fragment {
private TextView mTextDetails;
private CallbackManager mCallbackManager;
private static final String TAG = "Facebook";
private LoginButton login;
private FacebookCallback<LoginResult> mCallback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.i(TAG, " logged in...");
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile(); //Access the profile who Is the person login in
if(profile != null) {
//Start to add things to the View here for the logged in user, but how?
}
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException e) {
Log.i(TAG, " Error");
}
};
public LoginFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
mCallbackManager = CallbackManager.Factory.create();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.login, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
//Ask the use for permission to access friends
loginButton.setReadPermissions("user_friends");
//Because we work with fragments
loginButton.setFragment(this);
loginButton.registerCallback(mCallbackManager, mCallback);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
}
用户登录后,我想显示属于已登录用户的视图内容。
但是在代码中我可以从哪里开始显示登录用户的视图内容?
在 ~19 行检查 if(profile != null) 后不久。
因为你已经有accessToken了,用户数据经过验证,防止为null。
参考link,可以实现onCompleted(),别忘了保存用户信息
// When session is changed, this method is called from callback method
private void onSessionStateChange(Session session, SessionState state,
Exception exception)
{
final TextView name = (TextView) getView().findViewById(R.id.name);
final TextView gender = (TextView) getView().findViewById(R.id.gender);
final TextView location = (TextView) getView().findViewById(R.id.location);
if (state.isOpened()) {
Log.i(TAG, "Logged in...");
// make request to the /me API to get Graph user
Request.newMeRequest(session, new Request.GraphUserCallback() {
// callback after Graph API response with user
// object
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
// Set view visibility to true
otherView.setVisibility(View.VISIBLE);
// Set User name
name.setText("Hello " + user.getName());
// Set Gender
gender.setText("Your Gender: "
+ user.getProperty("gender").toString());
location.setText("Your Current Location: "
+ user.getLocation().getProperty("name")
.toString());
}
}
}).executeAsync();
} else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
otherView.setVisibility(View.GONE);
}
}
看来你差不多完成了,你需要做的是在用户登录成功后在onSuccess回调中编写另一个请求来获取用户信息,例如获取用户名和电子邮件
private FacebookCallback mCallback = new FacebookCallback() {
@Override
public void onSuccess(LoginResult loginResult) {
// get user name and email
this.getUserInfo();
}
}
...
}
private void getUserInfo()
{
final GraphRequest.GraphJSONObjectCallback callback = new GraphRequest.GraphJSONObjectCallback()
{
@Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse)
{
if (jsonObject == null)
{
return;
}
String name = jsonObject.optString("name");
String email = jsonObject.optString("email");
// Do something with these data here
}
};
GraphRequest graphRequest = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), callback);
Bundle params = graphRequest.getParameters();
params.putString("fields", "name,email");
graphRequest.setParameters(params);
graphRequest.executeAsync();
}
记得加上这段代码,根据你想要得到的
// 默认权限为 public_profile
loginButton.setReadPermissions("email");
您可以在此处了解如何使用 Graph API
https://developers.facebook.com/docs/graph-api
我有一个应用程序可以让用户通过 facebook 登录。我正在使用 Facebook SDK:
public class LoginFragment extends Fragment {
private TextView mTextDetails;
private CallbackManager mCallbackManager;
private static final String TAG = "Facebook";
private LoginButton login;
private FacebookCallback<LoginResult> mCallback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.i(TAG, " logged in...");
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile(); //Access the profile who Is the person login in
if(profile != null) {
//Start to add things to the View here for the logged in user, but how?
}
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException e) {
Log.i(TAG, " Error");
}
};
public LoginFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
mCallbackManager = CallbackManager.Factory.create();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.login, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
//Ask the use for permission to access friends
loginButton.setReadPermissions("user_friends");
//Because we work with fragments
loginButton.setFragment(this);
loginButton.registerCallback(mCallbackManager, mCallback);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
}
用户登录后,我想显示属于已登录用户的视图内容。
但是在代码中我可以从哪里开始显示登录用户的视图内容?
在 ~19 行检查 if(profile != null) 后不久。
因为你已经有accessToken了,用户数据经过验证,防止为null。
参考link,可以实现onCompleted(),别忘了保存用户信息
// When session is changed, this method is called from callback method
private void onSessionStateChange(Session session, SessionState state,
Exception exception)
{
final TextView name = (TextView) getView().findViewById(R.id.name);
final TextView gender = (TextView) getView().findViewById(R.id.gender);
final TextView location = (TextView) getView().findViewById(R.id.location);
if (state.isOpened()) {
Log.i(TAG, "Logged in...");
// make request to the /me API to get Graph user
Request.newMeRequest(session, new Request.GraphUserCallback() {
// callback after Graph API response with user
// object
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
// Set view visibility to true
otherView.setVisibility(View.VISIBLE);
// Set User name
name.setText("Hello " + user.getName());
// Set Gender
gender.setText("Your Gender: "
+ user.getProperty("gender").toString());
location.setText("Your Current Location: "
+ user.getLocation().getProperty("name")
.toString());
}
}
}).executeAsync();
} else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
otherView.setVisibility(View.GONE);
}
}
看来你差不多完成了,你需要做的是在用户登录成功后在onSuccess回调中编写另一个请求来获取用户信息,例如获取用户名和电子邮件
private FacebookCallback mCallback = new FacebookCallback() {
@Override
public void onSuccess(LoginResult loginResult) {
// get user name and email
this.getUserInfo();
}
}
...
}
private void getUserInfo()
{
final GraphRequest.GraphJSONObjectCallback callback = new GraphRequest.GraphJSONObjectCallback()
{
@Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse)
{
if (jsonObject == null)
{
return;
}
String name = jsonObject.optString("name");
String email = jsonObject.optString("email");
// Do something with these data here
}
};
GraphRequest graphRequest = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), callback);
Bundle params = graphRequest.getParameters();
params.putString("fields", "name,email");
graphRequest.setParameters(params);
graphRequest.executeAsync();
}
记得加上这段代码,根据你想要得到的
// 默认权限为 public_profile
loginButton.setReadPermissions("email");
您可以在此处了解如何使用 Graph API https://developers.facebook.com/docs/graph-api